/**
  *  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));
 }
 function __construct()
 {
     //load helper
     EE_Registry::instance()->load_helper('MSG_Template');
     // get list of active messengers and active message types
     $this->_EEM_data = EEM_Message_Template::instance();
     $this->_set_active_messengers_and_message_types();
     $this->_set_installed_message_types();
 }
 /**
  * 		This function is a singleton method used to instantiate the EEM_Message_Template object
  *
  * 		@access public
  * 		@return EEM_Message_Template instance
  */
 public static function instance()
 {
     // check if instance of EEM_Message_Template already exists
     if (self::$_instance === NULL) {
         // instantiate Message Template Model
         self::$_instance = new self();
     }
     // EEM_Price 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));
 }
 /**
  * private method that handles creating new default templates
  * @return mixed (array|bool)            success array or false.
  */
 private function _create_new_templates()
 {
     $this->_set_templates();
     //allow for child classes to override.
     $this->_change_templates();
     $this->_templates = apply_filters('FHEE__' . get_class($this) . '___create_new_templates___templates', $this->_templates, $this);
     $this->_templates = apply_filters('FHEE__EE_Message_Template_Defaults___create_new_templates___templates', $this->_templates, $this);
     //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' => get_current_user_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->_EEM_data->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 = EEM_Message_Template::instance()->insert($template_data);
                 if (!$MTP) {
                     EE_Error::add_error(sprintf(__('There was an error in saving new template data for %s messenger, %s message type, %s context and %s template field.', 'event_espresso'), $this->_messenger->name, $this->_message_type->name, $context, $field), __FILE__, __FUNCTION__, __LINE__);
                     return false;
                 }
             }
         }
     }
     $success_array = array('GRP_ID' => $this->_GRP_ID, 'MTP_context' => key($this->_contexts));
     return $success_array;
 }
 protected function _insert_or_update_message_template($new = FALSE)
 {
     do_action('AHEE_log', __FILE__, __FUNCTION__, '');
     $success = 0;
     $override = FALSE;
     //setup notices description
     $messenger = !empty($this->_req_data['MTP_messenger']) ? ucwords(str_replace('_', ' ', $this->_req_data['MTP_messenger'])) : false;
     $message_type = !empty($this->_req_data['MTP_message_type']) ? ucwords(str_replace('_', ' ', $this->_req_data['MTP_message_type'])) : false;
     $context = !empty($this->_req_data['MTP_context']) ? ucwords(str_replace('_', ' ', $this->_req_data['MTP_context'])) : false;
     $item_desc = $messenger ? $messenger . ' ' . $message_type . ' ' . $context . ' ' : '';
     $item_desc .= 'Message Template';
     $query_args = array();
     $validates = '';
     //if this is "new" then we need to generate the default contexts for the selected messenger/message_type for user to edit.
     if ($new) {
         $GRP_ID = !empty($this->_req_data['GRP_ID']) ? $this->_req_data['GRP_ID'] : 0;
         if ($edit_array = $this->_generate_new_templates($messenger, $message_type, $GRP_ID)) {
             if (empty($edit_array)) {
                 $success = 0;
             } else {
                 $success = 1;
                 $edit_array = $edit_array[0];
                 $query_args = array('id' => $edit_array['GRP_ID'], 'context' => $edit_array['MTP_context'], 'action' => 'edit_message_template');
             }
         }
         $action_desc = 'created';
     } else {
         $MTPG = EEM_Message_Template_Group::instance();
         $MTP = EEM_Message_Template::instance();
         //run update for each template field in displayed context
         if (!isset($this->_req_data['MTP_template_fields']) && empty($this->_req_data['MTP_template_fields'])) {
             EE_Error::add_error(__('There was a problem saving the template fields from the form because I didn\'t receive any actual template field data.', 'event_espresso'), __FILE__, __FUNCTION__, __LINE__);
             $success = 0;
             $action_desc = '';
         } else {
             //first validate all fields!
             $validates = $MTPG->validate($this->_req_data['MTP_template_fields'], $this->_req_data['MTP_context'], $this->_req_data['MTP_messenger'], $this->_req_data['MTP_message_type']);
             //if $validate returned error messages (i.e. is_array()) then we need to process them and setup an appropriate response. HMM, dang this isn't correct, $validates will ALWAYS be an array.  WE need to make sure there is no actual error messages in validates.
             if (is_array($validates) && !empty($validates)) {
                 //add the transient so when the form loads we know which fields to highlight
                 $this->_add_transient('edit_message_template', $validates);
                 $success = 0;
                 $action_desc = '';
                 //setup notices
                 foreach ($validates as $field => $error) {
                     if (isset($error['msg'])) {
                         EE_Error::add_error($error['msg'], __FILE__, __FUNCTION__, __LINE__);
                     }
                 }
             } else {
                 foreach ($this->_req_data['MTP_template_fields'] as $template_field => $content) {
                     $set_column_values = $this->_set_message_template_column_values($template_field);
                     $where_cols_n_values = array('MTP_ID' => $this->_req_data['MTP_template_fields'][$template_field]['MTP_ID']);
                     $message_template_fields = array('GRP_ID' => $set_column_values['GRP_ID'], 'MTP_template_field' => $set_column_values['MTP_template_field'], 'MTP_context' => $set_column_values['MTP_context'], 'MTP_content' => $set_column_values['MTP_content']);
                     if ($updated = $MTP->update($message_template_fields, array($where_cols_n_values))) {
                         if ($updated === FALSE) {
                             $msg = sprintf(__('%s field was NOT updated for some reason', 'event_espresso'), $template_field);
                             EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__);
                         } else {
                             $success = 1;
                         }
                     }
                     $action_desc = 'updated';
                 }
                 //we can use the last set_column_values for the MTPG update (because its the same for all of these specific MTPs)
                 $mtpg_fields = array('MTP_user_id' => $set_column_values['MTP_user_id'], 'MTP_messenger' => $set_column_values['MTP_messenger'], 'MTP_message_type' => $set_column_values['MTP_message_type'], 'MTP_is_global' => $set_column_values['MTP_is_global'], 'MTP_is_override' => $set_column_values['MTP_is_override'], 'MTP_deleted' => $set_column_values['MTP_deleted'], 'MTP_is_active' => $set_column_values['MTP_is_active'], 'MTP_name' => !empty($this->_req_data['ee_msg_non_global_fields']['MTP_name']) ? $this->_req_data['ee_msg_non_global_fields']['MTP_name'] : '', 'MTP_description' => !empty($this->_req_data['ee_msg_non_global_fields']['MTP_description']) ? $this->_req_data['ee_msg_non_global_fields']['MTP_description'] : '');
                 $mtpg_where = array('GRP_ID' => $set_column_values['GRP_ID']);
                 $updated = $MTPG->update($mtpg_fields, array($mtpg_where));
                 if ($updated === FALSE) {
                     $msg = sprintf(__('The Message Template Group (%d) was NOT updated for some reason', 'event_espresso'), $set_column_values['GRP_ID']);
                     EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__);
                 } else {
                     //k now we need to ensure the template_pack and template_variation fields are set.
                     $template_pack = !empty($this->_req_data['MTP_template_pack']) ? $this->_req_data['MTP_template_pack'] : 'default';
                     $template_variation = !empty($this->_req_data['MTP_template_variation']) ? $this->_req_data['MTP_template_variation'] : 'default';
                     $mtpg_obj = $MTPG->get_one_by_ID($set_column_values['GRP_ID']);
                     if ($mtpg_obj instanceof EE_Message_Template_Group) {
                         $mtpg_obj->set_template_pack_name($template_pack);
                         $mtpg_obj->set_template_pack_variation($template_variation);
                     }
                     $success = 1;
                 }
             }
         }
     }
     //we return things differently if doing ajax
     if (defined('DOING_AJAX') && DOING_AJAX) {
         $this->_template_args['success'] = $success;
         $this->_template_args['error'] = !$success ? TRUE : FALSE;
         $this->_template_args['content'] = '';
         $this->_template_args['data'] = array('grpID' => $edit_array['GRP_ID'], 'templateName' => $edit_array['template_name']);
         if ($success) {
             EE_Error::overwrite_success();
             EE_Error::add_success(__('The new template has been created and automatically selected for this event.  You can edit the new template by clicking the edit button.  Note before this template is assigned to this event, the event must be saved.', 'event_espresso'));
         }
         $this->_return_json();
     }
     //was a test send triggered?
     if (isset($this->_req_data['test_button'])) {
         EE_Error::overwrite_success();
         $this->_do_test_send($this->_req_data['MTP_context'], $this->_req_data['MTP_messenger'], $this->_req_data['MTP_message_type']);
         $override = TRUE;
     }
     if (empty($query_args)) {
         $query_args = array('id' => $this->_req_data['GRP_ID'], 'context' => $this->_req_data['MTP_context'], 'action' => 'edit_message_template');
     }
     $this->_redirect_after_action($success, $item_desc, $action_desc, $query_args, $override);
 }
 /**
  * The purpose of this method is to determine if there are already generated templates in the database for the given variables.
  * @param  string $messenger     messenger
  * @param  string $message_type message type
  * @param  int $GRP_ID        GRP ID ( if a custom template) (if not provided then we're just doing global template check)
  * @param  bool  $update_to_active if true then we also toggle the template to active.
  * @return bool                true = generated, false = hasn't been generated.
  */
 public static function already_generated($messenger, $message_type, $GRP_ID = 0, $update_to_active = TRUE)
 {
     self::_set_autoloader();
     $MTP = EEM_Message_Template::instance();
     //what method we use depends on whether we have an GRP_ID or not
     $count = empty($GRP_ID) ? EEM_Message_Template::instance()->count(array(array('Message_Template_Group.MTP_messenger' => $messenger, 'Message_Template_Group.MTP_message_type' => $message_type, 'Message_Template_Group.MTP_is_global' => TRUE))) : $MTP->count(array(array('GRP_ID' => $GRP_ID)));
     if ($update_to_active) {
         self::update_to_active($messenger, $message_type);
     }
     return $count > 0 ? TRUE : FALSE;
 }
 /**
  * The purpose of this method is to determine if there are already generated templates in the database for the given variables.
  * @param  string $messenger     messenger
  * @param  string $message_type message type
  * @param  int $GRP_ID        GRP ID ( if a custom template) (if not provided then we're just doing global template check)
  * @return bool                true = generated, false = hasn't been generated.
  */
 public static function already_generated($messenger, $message_type, $GRP_ID = 0)
 {
     EEH_MSG_Template::_set_autoloader();
     //what method we use depends on whether we have an GRP_ID or not
     $count = empty($GRP_ID) ? EEM_Message_Template::instance()->count(array(array('Message_Template_Group.MTP_messenger' => $messenger, 'Message_Template_Group.MTP_message_type' => $message_type, 'Message_Template_Group.MTP_is_global' => true))) : EEM_Message_Template::instance()->count(array(array('GRP_ID' => $GRP_ID)));
     return $count > 0;
 }