/**
  *  Handles creating new default templates.
  *
  * @param string $template_pack This corresponds to a template pack class reference
  *                              which will contain information about where to obtain the templates.
  * @return mixed (array|bool) 	success array or false.
  */
 protected function _create_new_templates($template_pack)
 {
     $this->_set_templates($template_pack);
     //necessary properties are set, let's save the default templates
     if (empty($this->_GRP_ID)) {
         $main_template_data = array('MTP_messenger' => $this->_messenger->name, 'MTP_message_type' => $this->_message_type->name, 'MTP_is_override' => 0, 'MTP_deleted' => 0, 'MTP_is_global' => 1, 'MTP_user_id' => EEH_Activation::get_default_creator_id(), 'MTP_is_active' => 1);
         //let's insert the above and get our GRP_ID, then reset the template data array to just include the GRP_ID
         $grp_id = $this->_message_template_group_model->insert($main_template_data);
         if (empty($grp_id)) {
             return $grp_id;
         }
         $this->_GRP_ID = $grp_id;
     }
     $template_data = array('GRP_ID' => $this->_GRP_ID);
     foreach ($this->_contexts as $context => $details) {
         foreach ($this->_fields as $field => $field_type) {
             if ($field != 'extra') {
                 $template_data['MTP_context'] = $context;
                 $template_data['MTP_template_field'] = $field;
                 $template_data['MTP_content'] = $this->_templates[$context][$field];
                 $MTP = $this->_message_template_model->insert($template_data);
                 if (!$MTP) {
                     EE_Error::add_error(sprintf(__('There was an error in saving new template data for %1$s messenger, %2$s message type, %3$s context and %4$s template field.', 'event_espresso'), $this->_messenger->name, $this->_message_type->name, $context, $field), __FILE__, __FUNCTION__, __LINE__);
                     return false;
                 }
             }
         }
     }
     return array('GRP_ID' => $this->_GRP_ID, 'MTP_context' => key($this->_contexts));
 }
 /**
  *		This function is a singleton method used to instantiate the EEM_Message_Template_Group object
  *
  *		@access public
  *		@return EEM_Question instance
  */
 public static function instance()
 {
     // check if instance of EEM_Message_Template_Group already exists
     if (self::$_instance === NULL) {
         // instantiate Espresso_model
         self::$_instance = new self();
     }
     // EEM_Message_Template_Group object
     return self::$_instance;
 }
 /**
  * Test that message templates are controlled properly by caps
  * and then you can edit others if you have that cap,
  * and then you can edit others if you have that cap
  */
 function test_get_all__caps()
 {
     //remove all questions currently existing
     EEM_Message_Template::instance()->delete(array(), false);
     $this->assertEquals(0, EEM_Message_Template::instance()->count());
     EEM_Message_Template_Group::instance()->delete_permanently(EEM_Message_Template_Group::instance()->alter_query_params_so_deleted_and_undeleted_items_included(), false);
     $this->assertEquals(0, EEM_Message_Template_Group::instance()->count());
     global $current_user;
     $user = $this->factory->user->create_and_get();
     $this->assertEquals(0, EEM_Question::instance()->count(array('caps' => EEM_Base::caps_read_admin)));
     //now log in and see I can edit my own
     $current_user = $user;
     $user->add_cap('ee_read_messages');
     $mtg1 = $this->new_model_obj_with_dependencies('Message_Template_Group', array('MTP_is_global' => false, 'MTP_user_id' => $user->ID));
     $mt1 = $this->new_model_obj_with_dependencies('Message_Template', array('GRP_ID' => $mtg1->ID()));
     $mtg2_others = $this->new_model_obj_with_dependencies('Message_Template_Group', array('MTP_is_global' => false, 'MTP_user_id' => 9999));
     $mt2_others = $this->new_model_obj_with_dependencies('Message_Template', array('GRP_ID' => $mtg2_others->ID()));
     $mtg3_global = $this->new_model_obj_with_dependencies('Message_Template_Group', array('MTP_is_global' => true, 'MTP_user_id' => 9999));
     $mt3_global = $this->new_model_obj_with_dependencies('Message_Template', array('GRP_ID' => $mtg3_global->ID()));
     $i_can_edit = EEM_Message_Template::instance()->get_all(array('caps' => EEM_Base::caps_read_admin));
     $this->assertEquals($mt1, reset($i_can_edit));
     $this->assertEquals(1, count($i_can_edit));
     //now give them the ability to read others messages
     $user->add_cap('ee_read_others_messages');
     $i_can_edit = EEM_Message_Template::instance()->get_all(array('caps' => EEM_Base::caps_read_admin));
     $this->assertEquals($mt1, reset($i_can_edit));
     $this->assertEquals($mt2_others, next($i_can_edit));
     $this->assertEquals(2, count($i_can_edit));
     //now let them read global message
     $user->add_cap('ee_read_global_messages');
     $i_can_edit = EEM_Message_Template::instance()->get_all(array('caps' => EEM_Base::caps_read_admin, 'order_by' => array('MTP_ID' => 'ASC')));
     $this->assertEquals($mt1, reset($i_can_edit));
     $this->assertEquals($mt2_others, next($i_can_edit));
     $this->assertEquals($mt3_global, next($i_can_edit));
     $this->assertEquals(3, count($i_can_edit));
 }
 /**
  * Checks that we can correctly apply backend read caps where there are two
  * caps controlling access to the model: the basic cap (eg 'ee_read_registrations')
  * and the 'others' cap (eg 'ee_read_others_registrations' )
  * @group model_caps
  */
 public function test_get_all__caps_admin_read__basic_and_others()
 {
     $current_user = $this->_ensure_current_user_set();
     $mtg_mine = $this->new_model_obj_with_dependencies('Message_Template_Group', array('MTP_user_id' => $current_user->ID, 'MTP_is_global' => false));
     $mtg_others = $this->new_model_obj_with_dependencies('Message_Template_Group', array('MTP_user_id' => $current_user->ID + 1, 'MTP_is_global' => false));
     //current user can't access messages
     $this->assertEquals(0, EEM_Message_Template_Group::instance()->count(array('caps' => EEM_Base::caps_read_admin)));
     //ok now allow them to see their own messages
     $current_user->add_cap('ee_read_messages');
     $mtgs_i_can_see_now = EEM_Message_Template_Group::instance()->get_all(array('caps' => EEM_Base::caps_read_admin));
     $this->assertEquals(1, count($mtgs_i_can_see_now));
     $first_mtg_i_can_see_now = reset($mtgs_i_can_see_now);
     $this->assertEquals($mtg_mine, $first_mtg_i_can_see_now);
     //ok now allowthem to see others non-global messages (tesing global-related-caps should happen on EEM_Message_template_Group_Test)
     $current_user->add_cap('ee_read_others_messages');
     $mtgs_i_can_see_now = EEM_Message_Template_Group::instance()->get_all(array('caps' => EEM_Base::caps_read_admin, array('MTP_is_global' => false)));
     $this->assertEquals(2, count($mtgs_i_can_see_now));
     $first_mtg_i_can_see_now = reset($mtgs_i_can_see_now);
     $this->assertEquals($mtg_mine, $first_mtg_i_can_see_now);
     $last_mtg_i_can_see_now = end($mtgs_i_can_see_now);
     $this->assertEquals($mtg_others, $last_mtg_i_can_see_now);
 }
 /**
  * This creates a custom template using the incoming GRP_ID
  *
  * @param  int     $GRP_ID GRP_ID for the template_group being used as the base
  * @return  array $success             This will be an array in the format:
  *                                     			array(
  *                                     				'GRP_ID' => $new_grp_id,
  *                                     				'MTP_context' => $first_context_in_created_template
  *                                     			)
  * @access private
  */
 private function _create_custom_template_group($GRP_ID)
 {
     //defaults
     $success = array('GRP_ID' => NULL, 'MTP_context' => '');
     //get the template group to use as a template from the db.  If $GRP_ID is empty then we'll assume the base will be the global template matching the messenger and message type.
     $mtg = empty($GRP_ID) ? EEM_Message_Template_Group::instance()->get_one(array(array('MTP_messenger' => $this->_messenger->name, 'MTP_message_type' => $this->_message_type->name, 'MTP_is_global' => TRUE))) : EEM_Message_Template_Group::instance()->get_one_by_ID($GRP_ID);
     //if we don't have a mtg at this point then we need to bail.
     if (!$mtg instanceof EE_Message_Template_Group) {
         EE_Error::add_error(sprintf(__('Something went wrong with generating the custom template from this group id: %s.  This usually happens when there is no matching message template group in the db.', 'event_espresso'), $GRP_ID), __FILE__, __FUNCTION__, __LINE__);
         return $success;
     }
     //let's get all the related message_template objects for this group.
     $mtts = $mtg->message_templates();
     //now we have what we need to setup the new template
     $new_mtg = clone $mtg;
     $new_mtg->set('GRP_ID', 0);
     $new_mtg->set('MTP_is_global', FALSE);
     $template_name = defined('DOING_AJAX') && !empty($_POST['templateName']) ? $_POST['templateName'] : __('New Custom Template', 'event_espresso');
     $template_description = defined("DOING_AJAX") && !empty($_POST['templateDescription']) ? $_POST['templateDescription'] : sprintf(__('This is a custom template that was created for the %s messenger and %s message type.', 'event_espresso'), $new_mtg->messenger_obj()->label['singular'], $new_mtg->message_type_obj()->label['singular']);
     $new_mtg->set('MTP_name', $template_name);
     $new_mtg->set('MTP_description', $template_description);
     //remove ALL relations on this template group so they don't get saved!
     $new_mtg->_remove_relations('Message_Template');
     $new_mtg->save();
     $success['GRP_ID'] = $new_mtg->ID();
     $success['template_name'] = $template_name;
     //add new message templates and add relation to.
     foreach ($mtts as $mtt) {
         if (!$mtt instanceof EE_Message_Template) {
             continue;
         }
         $nmtt = clone $mtt;
         $nmtt->set('MTP_ID', 0);
         $nmtt->set('GRP_ID', $new_mtg->ID());
         //relation
         $nmtt->save();
         if (empty($success['MTP_context'])) {
             $success['MTP_context'] = $nmtt->get('MTP_context');
         }
     }
     return $success;
 }
 /**
  * This just updates the active_messengers usermeta field when a messenger or message type is activated/deactivated.
  * NOTE: deactivating will remove the messenger (or message type) from the active_messengers wp_options field so all saved settings WILL be lost for the messenger AND message_types associated with that messenger (or message type).
  *
  * @param  string  $messenger What messenger we're toggling
  * @param  boolean $deactivate if true then we deactivate
  * @param  mixed   $message_type if present what message type we're toggling
  * @return void
  */
 protected function _activate_messenger($messenger, $deactivate = FALSE, $message_type = FALSE)
 {
     global $espresso_wp_user;
     $templates = TRUE;
     $this->_set_m_mt_settings();
     if (!$deactivate) {
         //we are activating.  we can use $this->_m_mt_settings to get all the installed messengers
         $this->_active_messengers[$messenger]['settings'] = !isset($this->_active_messengers[$messenger]['settings']) ? array() : $this->_active_messengers[$messenger]['settings'];
         $this->_active_messengers[$messenger]['obj'] = $this->_m_mt_settings['messenger_tabs'][$messenger]['obj'];
         //get has_active so we can sure its kept up to date.
         $has_activated = get_option('ee_has_activated_messages');
         if (empty($has_activated[$messenger])) {
             $has_activated[$messenger] = array();
         }
         //k we need to get what default message types are to be associated with the messenger that's been activated.
         $default_types = $message_type ? (array) $message_type : $this->_active_messengers[$messenger]['obj']->get_default_message_types();
         foreach ($default_types as $type) {
             $settings_fields = $this->_m_mt_settings['message_type_tabs'][$messenger]['inactive'][$type]['obj']->get_admin_settings_fields();
             if (!empty($settings_fields)) {
                 //we have fields for this message type so let's get the defaults for saving.
                 foreach ($settings_fields as $field => $values) {
                     $settings[$field] = $values['default'];
                 }
                 //let's set the data for reloading this message type form in ajax
                 $this->_template_args['data']['mt_reload'][] = $type;
             } else {
                 $settings = array();
             }
             $this->_active_messengers[$messenger]['settings'][$messenger . '-message_types'][$type]['settings'] = $settings;
             if (!in_array($type, $has_activated[$messenger])) {
                 $has_activated[$messenger][] = $type;
             }
         }
         //any default settings for the messenger?
         $msgr_settings = $this->_active_messengers[$messenger]['obj']->get_admin_settings_fields();
         if (!empty($msgr_settings)) {
             foreach ($msgr_settings as $field => $value) {
                 $this->_active_messengers[$messenger]['settings'][$field] = $value;
             }
         }
         //update settings in database
         EEH_MSG_Template::update_active_messengers_in_db($this->_active_messengers);
         update_option('ee_has_activated_messages', $has_activated);
         //generate new templates (if necessary)
         $templates = $this->_generate_new_templates($messenger, $default_types, 0, TRUE);
         EE_Error::overwrite_success();
         //if generation failed then we need to remove the active messenger.
         if (!$templates) {
             unset($this->_active_messengers[$messenger]);
             EEH_MSG_Template::update_active_messengers_in_db($this->_active_messengers);
         } else {
             //all is good let's do a success message
             if ($message_type) {
                 EE_Error::add_success(sprintf(__('%s message type has been successfully activated with the %s messenger', 'event_espresso'), ucwords($this->_m_mt_settings['message_type_tabs'][$messenger]['inactive'][$message_type]['obj']->label['singular']), ucwords($this->_active_messengers[$messenger]['obj']->label['singular'])));
                 //if message type was invoice then let's make sure we activate the invoice payment method.
                 if ($message_type == 'invoice') {
                     EE_Registry::instance()->load_lib('Payment_Method_Manager');
                     $pm = EE_Payment_Method_Manager::instance()->activate_a_payment_method_of_type('Invoice');
                     if ($pm instanceof EE_Payment_Method) {
                         EE_Error::add_attention(__('Activating the invoice message type also automatically activates the invoice payment method.  If you do not wish the invoice payment method to be active, or to change its settings, visit the payment method admin page.', 'event_espresso'));
                     }
                 }
             } else {
                 EE_Error::add_success(sprintf(__('%s messenger has been successfully activated', 'event_espresso'), ucwords($this->_active_messengers[$messenger]['obj']->label['singular'])));
             }
         }
         $this->_template_args['data']['active_mts'] = $default_types;
     } else {
         //we're deactivating
         $MTP = EEM_Message_Template_Group::instance();
         //okay let's update the message templates that match this messenger so that they are deactivated in the database as well.
         $update_array = array('MTP_messenger' => $messenger);
         if ($message_type) {
             $update_array['MTP_message_type'] = $message_type;
         }
         $success = $MTP->update(array('MTP_is_active' => 0), array($update_array));
         $messenger_obj = $this->_active_messengers[$messenger]['obj'];
         //if this is a message type deactivation then we're only unsetting the message type otherwise unset the messenger
         if ($message_type) {
             unset($this->_active_messengers[$messenger]['settings'][$messenger . '-message_types'][$message_type]);
         } else {
             unset($this->_active_messengers[$messenger]);
         }
         EEH_MSG_Template::update_active_messengers_in_db($this->_active_messengers);
         EE_Error::overwrite_success();
         if ($message_type) {
             EE_Error::add_success(sprintf(__('%s message type has been successfully deactivated', 'event_espresso'), ucwords($this->_m_mt_settings['message_type_tabs'][$messenger]['active'][$message_type]['obj']->label['singular'])));
         } else {
             EE_Error::add_success(sprintf(__('%s messenger has been successfully deactivated', 'event_espresso'), ucwords($messenger_obj->label['singular'])));
         }
         //if messenger was html or message type was invoice then let's make sure we deactivate invoice payment method.
         if ($messenger == 'html' || $message_type == 'invoice') {
             EE_Registry::instance()->load_lib('Payment_Method_Manager');
             $count_updated = EE_Payment_Method_Manager::instance()->deactivate_payment_method('invoice');
             if ($count_updated > 0) {
                 $msg = $message_type == 'invoice' ? __('Deactivating the invoice message type also automatically deactivates the invoice payment method. In order for invoices to be generated the invoice message type must be active. If you completed this action by mistake, simply reactivate the invoice message type and then vist the payment methods admin page to reactivate the invoice payment method.', 'event_espresso') : __('Deactivating the html messenger also automatically deactivates the invoice payment method.  In order for invoices to be generated the html messenger must be be active.  If you completed this action by mistake, simply reactivate the html messenger, then visit the payment methods admin page to reactivate the invoice payment method.', 'event_espresso');
                 EE_Error::add_attention($msg);
             }
         }
     }
     return true;
 }
 /**
  * Updates all message template groups matching the incoming arguments to inactive status.
  *
  * @param string $messenger      The messenger slug. If empty then all templates matching the message type are marked inactive.  Otherwise only templates matching the messenger and message type.
  * @param string $message_type The message type slug.  If empty then all templates matching the messenger are marked inactive. Otherwise only templates matching the messenger and message type.
  *
  * @return int  count of updated records.
  */
 public static function update_to_inactive($messenger = '', $message_type = '')
 {
     $query_args = array();
     if (empty($messenger) && empty($message_type)) {
         return 0;
     }
     if (!empty($messenger)) {
         $query_args[0]['MTP_messenger'] = $messenger;
     }
     if (!empty($message_type)) {
         $query_args[0]['MTP_message_type'] = $message_type;
     }
     return EEM_Message_Template_Group::instance()->update(array('MTP_is_active' => FALSE), $query_args);
 }
 /**
  * This just updates the active_messengers usermeta field when a messenger or message type is activated/deactivated.
  * NOTE: deactivating will remove the messenger (or message type) from the active_messengers wp_options field so all saved settings WILL be lost for the messenger AND message_types associated with that messenger (or message type).
  *
  * @param  string  $messenger What messenger we're toggling
  * @param  boolean $deactivate if true then we deactivate
  * @param  mixed   $message_type if present what message type we're toggling
  * @return void
  */
 protected function _activate_messenger($messenger, $deactivate = FALSE, $message_type = FALSE)
 {
     global $espresso_wp_user;
     $success_msg = array();
     $templates = TRUE;
     $this->_set_m_mt_settings();
     if (!$deactivate) {
         //we are activating.  we can use $this->_m_mt_settings to get all the installed messengers.
         $this->_active_messengers[$messenger]['settings'] = !isset($this->_active_messengers[$messenger]['settings']) ? array() : $this->_active_messengers[$messenger]['settings'];
         $this->_active_messengers[$messenger]['obj'] = $this->_m_mt_settings['messenger_tabs'][$messenger]['obj'];
         //k we need to get what default message types are to be associated with the messenger that's been activated.
         $default_types = $message_type ? (array) $message_type : $this->_active_messengers[$messenger]['obj']->get_default_message_types();
         foreach ($default_types as $type) {
             $settings_fields = $this->_m_mt_settings['message_type_tabs'][$messenger]['inactive'][$type]['obj']->get_admin_settings_fields();
             if (!empty($settings_fields)) {
                 //we have fields for this message type so let's get the defaults for saving.
                 foreach ($settings_fields as $field => $values) {
                     $settings[$field] = $values['default'];
                 }
                 //let's set the data for reloading this message type form in ajax
                 $this->_template_args['data']['mt_reload'][] = $type;
             } else {
                 $settings = array();
             }
             $this->_active_messengers[$messenger]['settings'][$messenger . '-message_types'][$type]['settings'] = $settings;
         }
         //any default settings for the messenger?
         $msgr_settings = $this->_active_messengers[$messenger]['obj']->get_admin_settings_fields();
         if (!empty($msgr_settings)) {
             foreach ($msgr_settings as $field => $value) {
                 $this->_active_messengers[$messenger]['settings'][$field] = $value;
             }
         }
         //update settings in database
         EEH_MSG_Template::update_active_messengers_in_db($this->_active_messengers);
         //generate new templates (if necessary)
         $templates = $this->_generate_new_templates($messenger, $default_types, 0, TRUE);
         EE_Error::overwrite_success();
         //if generation failed then we need to remove the active messenger.
         if (!$templates) {
             unset($this->_active_messengers[$messenger]);
             EEH_MSG_Template::update_active_messengers_in_db($this->_active_messengers);
         } else {
             //all is good let's do a success message
             $success_msg = $message_type ? sprintf(__('%s message type has been successfully activated with the %s messenger', 'event_espresso'), ucwords($this->_m_mt_settings['message_type_tabs'][$messenger]['inactive'][$message_type]['obj']->label['singular']), ucwords($this->_active_messengers[$messenger]['obj']->label['singular'])) : sprintf(__('%s messenger has been successfully activated', 'event_espresso'), ucwords($this->_active_messengers[$messenger]['obj']->label['singular']));
         }
         $this->_template_args['data']['active_mts'] = $default_types;
     } else {
         //we're deactivating
         $MTP = EEM_Message_Template_Group::instance();
         //okay let's update the message templates that match this messenger so that they are deactivated in the database as well.
         $update_array = array('MTP_messenger' => $messenger);
         if ($message_type) {
             $update_array['MTP_message_type'] = $message_type;
         }
         $success = $MTP->update(array('MTP_is_active' => 0), array($update_array));
         $messenger_obj = $this->_active_messengers[$messenger]['obj'];
         //if this is a message type deactivation then we're only unsetting the message type otherwise unset the messenger
         if ($message_type) {
             unset($this->_active_messengers[$messenger]['settings'][$messenger . '-message_types'][$message_type]);
         } else {
             unset($this->_active_messengers[$messenger]);
         }
         EEH_MSG_Template::update_active_messengers_in_db($this->_active_messengers);
         $success_msg = $message_type ? sprintf(__('%s %s has been successfully deactivated', 'event_espresso'), ucwords($this->_m_mt_settings['message_type_tabs'][$messenger]['active'][$message_type]['obj']->label['singular']), __('Message Type', 'event_espresso')) : sprintf(__('%s %s has been successfully deactivated', 'event_espresso'), ucwords($messenger_obj->label['singular']), __('Messenger', 'event_espresso'));
     }
     EE_Error::overwrite_success();
     if ($templates) {
         EE_Error::add_success($success_msg);
     }
     return true;
 }
 /**
  * Callback for AHEE__Extend_Registrations_Admin_Page___newsletter_selected_send trigger
  *
  * @since   4.3.0
  *
  * @param 	string 	$registration_message_trigger_url
  * @param 	EE_Registration $registration
  * @param string 	$messenger
  * @param string 	$message_type
  * @return 	string
  */
 public static function registration_message_trigger_url($registration_message_trigger_url, EE_Registration $registration, $messenger = 'html', $message_type = 'invoice')
 {
     EE_Registry::instance()->load_helper('MSG_Template');
     // whitelist $messenger
     switch ($messenger) {
         case 'pdf':
             $sending_messenger = 'pdf';
             $generating_messenger = 'html';
             break;
         case 'html':
         default:
             $sending_messenger = 'html';
             $generating_messenger = 'html';
             break;
     }
     // whitelist $message_type
     switch ($message_type) {
         case 'receipt':
             $message_type = 'receipt';
             break;
         case 'invoice':
         default:
             $message_type = 'invoice';
             break;
     }
     // verify that both the messenger AND the message type are active
     if (EEH_MSG_Template::is_messenger_active($sending_messenger) && EEH_MSG_Template::is_mt_active($message_type)) {
         //need to get the correct message template group for this (i.e. is there a custom invoice for the event this registration is registered for?)
         $template_query_params = array('MTP_is_active' => TRUE, 'MTP_messenger' => $generating_messenger, 'MTP_message_type' => $message_type, 'Event.EVT_ID' => $registration->event_ID());
         //get the message template group.
         $msg_template_group = EEM_Message_Template_Group::instance()->get_one(array($template_query_params));
         //if we don't have an EE_Message_Template_Group then return
         if (!$msg_template_group instanceof EE_Message_Template_Group) {
             // remove EVT_ID from query params so that global templates get picked up
             unset($template_query_params['Event.EVT_ID']);
             //get global template as the fallback
             $msg_template_group = EEM_Message_Template_Group::instance()->get_one(array($template_query_params));
         }
         //if we don't have an EE_Message_Template_Group then return
         if (!$msg_template_group instanceof EE_Message_Template_Group) {
             return '';
         }
         // generate the URL
         $registration_message_trigger_url = EEH_MSG_Template::generate_url_trigger($sending_messenger, $generating_messenger, 'purchaser', $message_type, $registration, $msg_template_group->ID(), $registration->transaction_ID());
     }
     return $registration_message_trigger_url;
 }
 /**
  * Retrieves the message template group being used for generating messages.
  * Note: this also utilizes the EE_Message_Template_Group_Collection to avoid having to hit the db multiple times.
  *
  * @return EE_Message_Template_Group | null
  */
 protected function _get_message_template_group()
 {
     //is there a GRP_ID already on the EE_Message object?  If there is, then a specific template has been requested
     //so let's use that.
     $GRP_ID = $this->_generation_queue->get_message_repository()->current()->GRP_ID();
     if ($GRP_ID) {
         //attempt to retrieve from repo first
         $GRP = $this->_template_collection->get_by_ID($GRP_ID);
         if ($GRP instanceof EE_Message_Template_Group) {
             return $GRP;
             //got it!
         }
         //nope don't have it yet.  Get from DB then add to repo
         $GRP = EEM_Message_Template_Group::instance()->get_one_by_ID($GRP_ID);
         if ($GRP instanceof EE_Message_Template_Group) {
             $this->_template_collection->add($GRP);
         }
         return $GRP;
     }
     //whatcha still doing here?  Oh, no Message Template Group yet I see.  Okay let's see if we can get it for you.
     //defaults
     $EVT_ID = 0;
     $template_qa = array('MTP_is_active' => true, 'MTP_messenger' => $this->_current_messenger->name, 'MTP_message_type' => $this->_current_message_type->name);
     //in vanilla EE we're assuming there's only one event.
     //However, if there are multiple events then we'll just use the default templates instead of different
     // templates per event (which could create problems).
     if (count($this->_current_data_handler->events) === 1) {
         foreach ($this->_current_data_handler->events as $event) {
             $EVT_ID = $event['ID'];
         }
     }
     //before going any further, let's see if its in the queue
     $GRP = $this->_template_collection->get_by_key($this->_template_collection->get_key($this->_current_messenger->name, $this->_current_message_type->name, $EVT_ID));
     if ($GRP instanceof EE_Message_Template_Group) {
         return $GRP;
     }
     //nope still no GRP?
     //first we get the global template in case it has an override set.
     $global_template_qa = array_merge(array('MTP_is_global' => true), $template_qa);
     $global_GRP = EEM_Message_Template_Group::instance()->get_one(array($global_template_qa));
     //if this is an override, then we just return it.
     if ($global_GRP instanceof EE_Message_Template_Group && $global_GRP->get('MTP_is_override')) {
         $this->_template_collection->add($global_GRP, $EVT_ID);
         return $global_GRP;
     }
     //STILL here? Okay that means we want to see if there is event specific group and if there is we return it,
     //otherwise we return the global group we retrieved.
     if ($EVT_ID) {
         $template_qa['Event.EVT_ID'] = $EVT_ID;
     }
     $GRP = EEM_Message_Template_Group::instance()->get_one(array($template_qa));
     $GRP = $GRP instanceof EE_Message_Template_Group ? $GRP : $global_GRP;
     if ($GRP instanceof EE_Message_Template_Group) {
         $this->_template_collection->add($GRP, $EVT_ID);
         return $GRP;
     }
     //nothing, nada, there ain't no group from what you fed the machine. (Getting here is a very hard thing to do).
     return null;
 }
Ejemplo n.º 11
0
 /**
  * get and set the templates for the type and messenger from the database
  * @return void
  * @access protected
  */
 protected function _get_templates()
 {
     //defaults
     $EVT_ID = $mtpg = $global_mtpg = NULL;
     $templates = array();
     //in vanilla EE we're assuming there's only one event.  However, if there are multiple events then we'll just use the default templates instead of different templates per event (which could create problems).
     if (count($this->_data->events) === 1) {
         foreach ($this->_data->events as $event) {
             $EVT_ID = $event['ID'];
         }
     }
     //if this is a preview then we just get whatever message group is for the preview and skip this part!
     if ($this->_preview && !empty($_POST['msg_id'])) {
         $mtpg = EEM_Message_Template_Group::instance()->get_one_by_ID($_POST['msg_id']);
     } else {
         //not a preview or test send so lets continue on our way!
         $template_qa = array('MTP_is_active' => TRUE, 'MTP_messenger' => $this->_active_messenger->name, 'MTP_message_type' => $this->name, 'MTP_is_global' => TRUE);
         //this gets the current global template (message template group) for the active messenger and message type.
         $global_mtpg = EEM_Message_Template_Group::instance()->get_one(array($template_qa));
         //If the global template is NOT an override, then we'll use whatever is attached to the event (if there is an evt_ID.  If it IS an override then we just use the global_mtpg
         if (!empty($EVT_ID) && !$global_mtpg->get('MTP_is_override')) {
             $evt_qa = array('Event.EVT_ID' => $EVT_ID);
             unset($template_qa['MTP_is_global']);
             $qa = array_merge($template_qa, $evt_qa);
             $mtpg = EEM_Message_Template_Group::instance()->get_one(array($qa));
         }
         //if global template is NOT an override, and there is a 'MTP_ID' in the post global, then we'll assume a specific template has ben requested.
         if (!empty($_POST['MTP_ID']) && !$global_mtpg->get('MTP_is_override')) {
             $mtpg = EEM_Message_Template_Group::instance()->get_one_by_ID($_POST['MTP_ID']);
         }
         $mtpg = $mtpg instanceof EE_Message_Template_Group ? $mtpg : $global_mtpg;
     }
     $templates = $mtpg->context_templates();
     foreach ($templates as $context => $template_fields) {
         foreach ($template_fields as $template_field => $template_obj) {
             $this->_templates[$template_field][$context] = $template_obj->get('MTP_content');
         }
     }
 }
 /**
  * message_type_has_active_templates_for_messenger
  *
  * @param \EE_messenger    $messenger
  * @param \EE_message_type $message_type
  * @param bool             $global
  * @return bool
  */
 public static function message_type_has_active_templates_for_messenger(EE_messenger $messenger, EE_message_type $message_type, $global = false)
 {
     //is given message_type valid for given messenger (if this is not a global save)
     if ($global) {
         return true;
     }
     $active_templates = EEM_Message_Template_Group::instance()->count(array(array('MTP_is_active' => true, 'MTP_messenger' => $messenger->name, 'MTP_message_type' => $message_type->name)));
     if ($active_templates > 0) {
         return true;
     }
     EE_Error::add_error(sprintf(__('The %1$s message type is not registered with the %2$s messenger. Please visit the Messenger activation page to assign this message type first if you want to use it.', 'event_espresso'), $message_type->name, $messenger->name), __FILE__, __FUNCTION__, __LINE__);
     return false;
 }
Ejemplo n.º 13
0
 /**
  * Retrieves the variation used for generating this message.
  * @return string
  */
 public function get_template_pack_variation()
 {
     /**
      * This is deprecated functionality that will be removed eventually but included here now for backward compat.
      */
     if (!empty($this->template_variation)) {
         return $this->template_variation;
     }
     /** @type EE_Message_Template_Group $grp */
     $grp = $this->get_first_related('Message_Template_Group');
     //if no group then let's try to get the first related group by internal messenger and message type (will use global grp).
     if (!$grp instanceof EE_Message_Template_Group) {
         $grp = EEM_Message_Template_Group::instance()->get_one(array(array('MTP_messenger' => $this->messenger(), 'MTP_message_type' => $this->message_type(), 'MTP_is_global' => true)));
     }
     return $grp instanceof EE_Message_Template_Group ? $grp->get_template_pack_variation() : '';
 }
 /**
  * public facing create new templates method
  * @access public
  *
  * @return mixed (array|bool)            success array or false.
  */
 public function create_new_templates()
 {
     $template_pack = 'default';
     //if we have the GRP_ID then let's use that to see if there is a set template pack and use that for the new templates.
     if (!empty($this->_GRP_ID)) {
         $mtpg = EEM_Message_Template_Group::instance()->get_one_by_ID($this->_GRP_ID);
         $template_pack = $mtpg instanceof EE_Message_Template_Group ? $mtpg->get_template_pack_name() : 'default';
         //we also need to reset the template variation to default
         $mtpg->set_template_pack_variation('default');
     }
     return $this->_create_new_templates($template_pack);
 }
 protected function _get_admin_content_events_edit($message_types, $extra)
 {
     //defaults
     $template_args = array();
     $custom_templates = array();
     $selector_rows = '';
     //we don't need message types here so we're just going to ignore. we do, however, expect the event id here. The event id is needed to provide a link to setup a custom template for this event.
     $event_id = isset($extra['event']) ? $extra['event'] : NULL;
     $template_wrapper_path = EE_LIBRARIES . 'messages/messenger/admin_templates/event_switcher_wrapper.template.php';
     $template_row_path = EE_LIBRARIES . 'messages/messenger/admin_templates/event_switcher_row.template.php';
     //array of template objects for global and custom (non-trashed) (but remember just for this messenger!)
     $global_templates = EEM_Message_Template_Group::instance()->get_all(array(array('MTP_messenger' => $this->name, 'MTP_is_global' => TRUE, 'MTP_is_active' => TRUE)));
     $templates_for_event = EEM_Message_Template_Group::instance()->get_all_custom_templates_by_event($event_id, array('MTP_messenger' => $this->name, 'MTP_is_active' => TRUE));
     $templates_for_event = !empty($templates_for_event) ? $templates_for_event : array();
     //so we need to setup the rows for the selectors and we use the global mtpgs (cause those will the active message template groups)
     foreach ($global_templates as $mtpgID => $mtpg) {
         //verify this message type is supposed to show on this page
         $mtp_obj = $mtpg->message_type_obj();
         if (!$mtp_obj instanceof EE_message_type) {
             continue;
         }
         $mtp_obj->admin_registered_pages = (array) $mtp_obj->admin_registered_pages;
         if (!in_array('events_edit', $mtp_obj->admin_registered_pages)) {
             continue;
         }
         $stargs = array();
         $default_value = '';
         $select_values = array();
         $select_values[$mtpgID] = __('Global', 'event_espresso');
         $default_value = array_key_exists($mtpgID, $templates_for_event) && !$mtpg->get('MTP_is_override') ? $mtpgID : NULL;
         //if the override has been set for the global template, then that means even if there are custom templates already created we ignore them because of the set override.
         if (!$mtpg->get('MTP_is_override')) {
             //any custom templates for this message type?
             $custom_templates = EEM_Message_Template_Group::instance()->get_custom_message_template_by_m_and_mt($this->name, $mtpg->message_type());
             foreach ($custom_templates as $cmtpgID => $cmtpg) {
                 $select_values[$cmtpgID] = $cmtpg->name();
                 $default_value = array_key_exists($cmtpgID, $templates_for_event) ? $cmtpgID : $default_value;
             }
         }
         //if there is no $default_value then we set it as the global
         $default_value = empty($default_value) ? $mtpgID : $default_value;
         $edit_url = EEH_URL::add_query_args_and_nonce(array('page' => 'espresso_messages', 'action' => 'edit_message_template', 'id' => $default_value), admin_url('admin.php'));
         $create_url = EEH_URL::add_query_args_and_nonce(array('page' => 'espresso_messages', 'action' => 'add_new_message_template', 'GRP_ID' => $default_value), admin_url('admin.php'));
         $st_args['mt_name'] = ucwords($mtp_obj->label['singular']);
         $st_args['mt_slug'] = $mtpg->message_type();
         $st_args['messenger_slug'] = $this->name;
         $st_args['selector'] = EEH_Form_Fields::select_input('event_message_templates_relation[' . $mtpgID . ']', $select_values, $default_value, 'data-messenger="' . $this->name . '" data-messagetype="' . $mtpg->message_type() . '"', 'message-template-selector');
         //note that  message template group that has override_all_custom set will remove the ability to set a custom message template based off of the global (and that also in turn overrides any other custom templates).
         $st_args['create_button'] = $mtpg->get('MTP_is_override') ? '' : '<a data-messenger="' . $this->name . '" data-messagetype="' . $mtpg->message_type() . '" data-grpid="' . $default_value . '" target="_blank" href="' . $create_url . '" class="button button-small create-mtpg-button">' . __('Create New Custom', 'event_espresso') . '</a>';
         $st_args['create_button'] = EE_Registry::instance()->CAP->current_user_can('ee_edit_messages', 'espresso_messsages_add_new_message_template') ? $st_args['create_button'] : '';
         $st_args['edit_button'] = EE_Registry::instance()->CAP->current_user_can('ee_edit_message', 'espresso_messages_edit_message_template', $mtpgID) ? '<a data-messagetype="' . $mtpg->message_type() . '" data-grpid="' . $default_value . '" target="_blank" href="' . $edit_url . '" class="button button-small edit-mtpg-button">' . __('Edit', 'event_espresso') . '</a>' : '';
         $selector_rows .= EEH_Template::display_template($template_row_path, $st_args, TRUE);
     }
     //if no selectors present then get out.
     if (empty($selector_rows)) {
         return '';
     }
     $template_args['selector_rows'] = $selector_rows;
     return EEH_Template::display_template($template_wrapper_path, $template_args, TRUE);
 }
 /**
  * helper for permanently deleting a mtP group and all related message_templates
  *
  * @param  int    $GRP_ID The group being deleted
  * @param  bool $include_group whether to delete the Message Template Group as well.
  *
  * @return bool        boolean to indicate the success of the deletes or not.
  */
 private function _delete_mtp_permanently($GRP_ID, $include_group = true)
 {
     $success = 1;
     $MTPG = EEM_Message_Template_Group::instance();
     //first let's GET this group
     $MTG = $MTPG->get_one_by_ID($GRP_ID);
     //then delete permanently all the related Message Templates
     $deleted = $MTG->delete_related_permanently('Message_Template');
     if ($deleted === 0) {
         $success = 0;
     }
     //now delete permanently this particular group
     if ($include_group && !$MTG->delete_permanently()) {
         $success = 0;
     }
     return $success;
 }
 /**
  * constructor
  * @param EE_Messages $messages the EE_Messages object.
  * @param int $GRP_ID Optional.  If included then we're just regenerating the template
  *                    		 fields for the given group not the message template group itself
  *
  * @access public
  * @return void
  */
 public function __construct(EE_Messages $messages, $GRP_ID = 0)
 {
     $this->_EE_MSG = $messages;
     //set the model object
     $this->_EEM_data = EEM_Message_Template_Group::instance();
     $this->_set_props();
     $this->_GRP_ID = $GRP_ID;
     //make sure required props have been set
     if (empty($this->_m_name) || empty($this->_mt_name)) {
         $msg[] = __('Message Templates cannot be generated because the Messenger and Message Types haven\'t been defined for the generator.', 'event_espresso');
         $msg[] = __('You need to set the "$m_name" and "$mt_name" properties', 'event_espresso');
         throw new EE_Error(implode('||', $msg));
     }
     $this->_init();
 }
 /**
  * Handles sending selected registrations/contacts a newsletter.
  *
  * @since  4.3.0
  *
  * @return void
  */
 protected function _newsletter_selected_send()
 {
     $success = TRUE;
     //first we need to make sure we have a GRP_ID so we know what template we're sending and updating!
     if (empty($this->_req_data['newsletter_mtp_selected'])) {
         EE_Error::add_error(__('In order to send a message, a Message Template GRP_ID is needed. It was not provided so messages were not sent.', 'event_espresso'), __FILE__, __FUNCTION__, __LINE__);
         $success = FALSE;
     }
     if ($success) {
         //update Message template in case there are any changes
         $Message_Template_Group = EEM_Message_Template_Group::instance()->get_one_by_ID($this->_req_data['newsletter_mtp_selected']);
         $Message_Templates = $Message_Template_Group instanceof EE_Message_Template_Group ? $Message_Template_Group->context_templates() : array();
         if (empty($Message_Templates)) {
             EE_Error::add_error(__('Unable to retrieve message template fields from the db. Messages not sent.', 'event_espresso'), __FILE__, __FUNCTION__, __LINE__);
         }
         //let's just update the specific fields
         foreach ($Message_Templates['attendee'] as $Message_Template) {
             if ($Message_Template instanceof EE_Message_Template) {
                 $field = $Message_Template->get('MTP_template_field');
                 $content = $Message_Template->get('MTP_content');
                 $new_content = $content;
                 switch ($field) {
                     case 'from':
                         $new_content = !empty($this->_req_data['batch_message']['from']) ? $this->_req_data['batch_message']['from'] : $content;
                         break;
                     case 'subject':
                         $new_content = !empty($this->_req_data['batch_message']['subject']) ? $this->_req_data['batch_message']['subject'] : $content;
                         break;
                     case 'content':
                         $new_content = $content;
                         $new_content['newsletter_content'] = !empty($this->_req_data['batch_message']['content']) ? $this->_req_data['batch_message']['content'] : $content['newsletter_content'];
                         break;
                     default:
                         continue;
                         break;
                 }
                 $Message_Template->set('MTP_content', $new_content);
                 $Message_Template->save();
             }
         }
         //great fields are updated!  now let's make sure we just have contact objects (EE_Attendee).
         $id_type = !empty($this->_req_data['batch_message']['id_type']) ? $this->_req_data['batch_message']['id_type'] : 'registration';
         //id_type will affect how we assemble the ids.
         $ids = !empty($this->_req_data['batch_message']['ids']) ? json_decode(stripslashes($this->_req_data['batch_message']['ids'])) : array();
         $registrations_used_for_contact_data = array();
         //using switch because eventually we'll have other contexts that will be used for generating messages.
         switch ($id_type) {
             case 'registration':
                 $registrations_used_for_contact_data = EEM_Registration::instance()->get_all(array(array('REG_ID' => array('IN', $ids))));
                 break;
             case 'contact':
                 $registrations_used_for_contact_data = EEM_Registration::instance()->get_latest_registration_for_each_of_given_contacts($ids);
                 break;
         }
         do_action('AHEE__Extend_Registrations_Admin_Page___newsletter_selected_send__with_registrations', $registrations_used_for_contact_data, $Message_Template_Group->ID());
         //kept for backward compat, internally we no longer use this action.
         //@deprecated 4.8.36.rc.002
         $contacts = $id_type == 'registration' ? EEM_Attendee::instance()->get_array_of_contacts_from_reg_ids($ids) : EEM_Attendee::instance()->get_all(array(array('ATT_ID' => array('in', $ids))));
         do_action('AHEE__Extend_Registrations_Admin_Page___newsletter_selected_send', $contacts, $Message_Template_Group->ID());
     }
     $query_args = array('action' => !empty($this->_req_data['redirect_back_to']) ? $this->_req_data['redirect_back_to'] : 'default');
     $this->_redirect_after_action(FALSE, '', '', $query_args, TRUE);
 }
 /**
  * get and set the templates for the type and messenger from the database
  * @return void
  * @access protected
  */
 protected function _get_templates()
 {
     //defaults
     $EVT_ID = $mtpg = $global_mtpg = NULL;
     $template_qa = array('MTP_is_active' => TRUE, 'MTP_messenger' => $this->_active_messenger->name, 'MTP_message_type' => $this->name);
     //in vanilla EE we're assuming there's only one event.  However, if there are multiple events then we'll just use the default templates instead of different templates per event (which could create problems).
     if (count($this->_data->events) === 1) {
         foreach ($this->_data->events as $event) {
             $EVT_ID = $event['ID'];
         }
     }
     // is there a Group ID in the incoming request?
     EE_Registry::instance()->load_core('Request_Handler');
     // if not, set a default value of false
     $GRP_ID = EE_Registry::instance()->REQ->get('GRP_ID', false);
     //if this is a preview then we just get whatever message group is for the preview and skip this part!
     if ($this->_preview && $GRP_ID) {
         $mtpg = EEM_Message_Template_Group::instance()->get_one_by_ID($GRP_ID);
     } else {
         //not a preview or test send so lets continue on our way!
         //is there an evt_id?  If so let's get that. template.
         if (!empty($EVT_ID)) {
             $evt_qa = array('Event.EVT_ID' => $EVT_ID);
             $qa = array_merge($template_qa, $evt_qa);
             $mtpg = EEM_Message_Template_Group::instance()->get_one(array($qa));
         }
         //is there a 'GRP_ID' ? if so let's get that.
         //if global template is NOT an override, and there is a 'GRP_ID' in the request, then we'll assume a specific template has ben requested.
         if ($GRP_ID) {
             $mtpg = EEM_Message_Template_Group::instance()->get_one_by_ID($GRP_ID);
         }
         $template_qa['MTP_is_global'] = TRUE;
         //this gets the current global template (message template group) for the active messenger and message type.
         $global_mtpg = EEM_Message_Template_Group::instance()->get_one(array($template_qa));
         $mtpg = $mtpg instanceof EE_Message_Template_Group && !$global_mtpg->get('MTP_is_override') ? $mtpg : $global_mtpg;
     }
     if (!$mtpg instanceof EE_Message_Template_Group) {
         //get out because we can't process anything, there are no message template groups
         // and thus some sort of bad setup issues.
         return false;
     }
     $this->_GRP_ID = $mtpg->ID();
     $templates = $mtpg->context_templates();
     //set the template pack and the variation for the given message template group.
     $this->_template_pack = $mtpg->get_template_pack();
     $this->_variation = $mtpg->get_template_pack_variation();
     foreach ($templates as $context => $template_fields) {
         foreach ($template_fields as $template_field => $template_obj) {
             $this->_templates[$template_field][$context] = $template_obj->get('MTP_content');
         }
     }
 }
 /**
  * This helper returns parsed content from the parser to be used for tests using the given params.
  *
  * @param string $messenger      The slug for the messenger being tested.
  * @param string $message_type The slug for the message type being tested.
  * @param string $field                  The field being tested.
  * @param string $context             The context being tested.
  * @param array|string $append    Append content to a default template for testing with.  If
  *                                		       you want to append to multiple fields, then include an
  *                                		       array indexed by field.  Otherwise the string will be
  *                                		       appended to the field sent in with the $field param.
  * @param EE_Messages_Addressee $addressee Optionally include a
  *                                         	messages addressee object if you do not wish
  *                                         	to use the default one generated.  This is
  *                                         	useful for simulating exceptions and failures.
  *
  * @return string The parsed content.
  */
 protected function _get_parsed_content($messenger, $message_type, $field, $context, $append = '', $addressee = null)
 {
     //grab the correct template  @see EE_message_type::_get_templates()
     $mtpg = EEM_Message_Template_Group::instance()->get_one(array(array('MTP_messenger' => $messenger, 'MTP_message_type' => $message_type, 'MTP_is_global' => true)));
     $all_templates = $mtpg->context_templates();
     foreach ($all_templates as $t_context => $template_fields) {
         foreach ($template_fields as $template_field => $template_obj) {
             $templates[$template_field][$t_context] = $template_obj->get('MTP_content');
         }
     }
     //instantiate messenger and message type objects
     $msg_class = 'EE_' . str_replace(' ', '_', ucwords(str_replace('_', ' ', $messenger))) . '_messenger';
     $mt_class = 'EE_' . str_replace(' ', '_', ucwords(str_replace('_', ' ', $message_type))) . '_message_type';
     $messenger = new $msg_class();
     $message_type = new $mt_class();
     $message_type->set_messages(array(), $messenger, $context, true);
     //grab valid shortcodes and setup for parser.
     $m_shortcodes = $messenger->get_valid_shortcodes();
     $mt_shortcodes = $message_type->get_valid_shortcodes();
     //just sending in the content field and primary_attendee context/data for testing.
     $template = isset($templates[$field][$context]) ? $templates[$field][$context] : array();
     /**
      * if empty template then its possible that the requested field is inside the "content"
      * field array.
      */
     if (empty($template)) {
         $template = isset($templates['content'][$context]) ? $templates['content'][$context] : array();
         //verify the requested field is present
         if (!empty($template) && is_array($template) && !isset($template[$field])) {
             return '';
         }
     }
     //if $template is still empty then return an empty string
     if (empty($template)) {
         return '';
     }
     // any appends?
     if (!empty($append)) {
         if (is_array($template)) {
             //we've already done a check for the presence of field above.
             if (is_array($append)) {
                 foreach ($append as $a_field => $string) {
                     if (isset($template[$a_field])) {
                         $template[$a_field] = $template[$a_field] . $string;
                     }
                 }
             } else {
                 $template[$field] = $template[$field] . $append;
             }
         } else {
             //only append if $append is not an array because the $template is not an array.
             if (!is_array($append)) {
                 $template .= $append;
             }
         }
     }
     $valid_shortcodes = isset($m_shortcodes[$field]) ? $m_shortcodes[$field] : $mt_shortcodes[$context];
     $data = $addressee instanceof EE_Messages_Addressee ? $addressee : $this->_get_addressee();
     EE_Registry::instance()->load_helper('Parse_Shortcodes');
     $parser = new EEH_Parse_Shortcodes();
     return $parser->parse_message_template($template, $data, $valid_shortcodes, $message_type, $messenger, $context, $mtpg->ID());
 }