/**
  * _edit_message_template
  *
  * @access protected
  * @return void
  */
 protected function _edit_message_template()
 {
     do_action('AHEE_log', __FILE__, __FUNCTION__, '');
     //we filter the tinyMCE settings to remove the validation since message templates by their nature will not have valid html in the templates.
     add_filter('tiny_mce_before_init', array($this, 'filter_tinymce_init'), 10, 2);
     $GRP_ID = isset($this->_req_data['id']) && !empty($this->_req_data['id']) ? absint($this->_req_data['id']) : FALSE;
     $this->_set_shortcodes();
     //this also sets the _message_template property.
     $message_template_group = $this->_message_template_group;
     $c_label = $message_template_group->context_label();
     $c_config = $message_template_group->contexts_config();
     reset($c_config);
     $context = isset($this->_req_data['context']) && !empty($this->_req_data['context']) ? strtolower($this->_req_data['context']) : key($c_config);
     if (empty($GRP_ID)) {
         $action = 'insert_message_template';
         $button_both = FALSE;
         $button_text = array(__('Save', 'event_espresso'));
         $button_actions = array('something_different');
         $referrer = FALSE;
         $edit_message_template_form_url = add_query_arg(array('action' => $action, 'noheader' => TRUE), EE_MSG_ADMIN_URL);
     } else {
         $action = 'update_message_template';
         $button_both = TRUE;
         $button_text = array();
         $button_actions = array();
         $referrer = $this->_admin_base_url;
         $edit_message_template_form_url = add_query_arg(array('action' => $action, 'noheader' => TRUE), EE_MSG_ADMIN_URL);
     }
     //set active messenger for this view
     $this->_active_messenger = $this->_active_messengers[$message_template_group->messenger()]['obj'];
     $this->_active_message_type_name = $message_template_group->message_type();
     //Do we have any validation errors?
     $validators = $this->_get_transient();
     $v_fields = !empty($validators) ? array_keys($validators) : array();
     //we need to assemble the title from Various details
     $context_label = sprintf(__('(%s %s)', 'event_espresso'), $c_config[$context]['label'], ucwords($c_label['label']));
     $title = sprintf(__(' %s %s Template %s', 'event_espresso'), ucwords($message_template_group->messenger_obj()->label['singular']), ucwords($message_template_group->message_type_obj()->label['singular']), $context_label);
     $this->_template_args['GRP_ID'] = $GRP_ID;
     $this->_template_args['message_template'] = $message_template_group;
     $this->_template_args['is_extra_fields'] = FALSE;
     //let's get the EE_messages_controller so we can get template form fields
     $MSG = new EE_messages();
     $template_field_structure = $MSG->get_fields($message_template_group->messenger(), $message_template_group->message_type());
     if (!$template_field_structure) {
         $template_field_structure = FALSE;
         $template_fields = __('There was an error in assembling the fields for this display (you should see an error message)', 'event_espresso');
     }
     $message_templates = $message_template_group->context_templates();
     //if we have the extra key.. then we need to remove the content index from the template_field_structure as it will get handled in the "extra" array.
     if (is_array($template_field_structure[$context]) && isset($template_field_structure[$context]['extra'])) {
         foreach ($template_field_structure[$context]['extra'] as $reference_field => $new_fields) {
             unset($template_field_structure[$context][$reference_field]);
         }
     }
     //let's loop through the template_field_structure and actually assemble the input fields!
     if (!empty($template_field_structure)) {
         $id_prefix = 'ee-msg-edit-template-fields-';
         foreach ($template_field_structure[$context] as $template_field => $field_setup_array) {
             //if this is an 'extra' template field then we need to remove any existing fields that are keyed up in the extra array and reset them.
             if ($template_field == 'extra') {
                 $this->_template_args['is_extra_fields'] = TRUE;
                 foreach ($field_setup_array as $reference_field => $new_fields_array) {
                     foreach ($new_fields_array as $extra_field => $extra_array) {
                         //let's verify if we need this extra field via the shortcodes parameter.
                         $continue = FALSE;
                         if (isset($extra_array['shortcodes_required'])) {
                             foreach ((array) $extra_array['shortcodes_required'] as $shortcode) {
                                 if (!array_key_exists($shortcode, $this->_shortcodes)) {
                                     $continue = TRUE;
                                 }
                             }
                             if ($continue) {
                                 continue;
                             }
                         }
                         $field_id = $reference_field . '-' . $extra_field . '-content';
                         $template_form_fields[$field_id] = $extra_array;
                         $template_form_fields[$field_id]['name'] = 'MTP_template_fields[' . $reference_field . '][content][' . $extra_field . ']';
                         $css_class = isset($extra_array['css_class']) ? $extra_array['css_class'] : '';
                         $template_form_fields[$field_id]['css_class'] = !empty($v_fields) && in_array($extra_field, $v_fields) && (is_array($validators[$extra_field]) && isset($validators[$extra_field]['msg'])) ? 'validate-error ' . $css_class : $css_class;
                         $content = $message_templates[$context][$reference_field]->get('MTP_content');
                         $template_form_fields[$field_id]['value'] = !empty($message_templates) && isset($content[$extra_field]) ? stripslashes(html_entity_decode($content[$extra_field], ENT_QUOTES, "UTF-8")) : '';
                         //do we have a validation error?  if we do then let's use that value instead
                         $template_form_fields[$field_id]['value'] = isset($validators[$extra_field]) ? $validators[$extra_field]['value'] : $template_form_fields[$field_id]['value'];
                         $template_form_fields[$field_id]['db-col'] = 'MTP_content';
                         if (isset($extra_array['input']) && $extra_array['input'] == 'wp_editor') {
                             //we want to decode the entities
                             $template_form_fields[$field_id]['value'] = stripslashes(html_entity_decode($template_form_fields[$field_id]['value'], ENT_QUOTES, "UTF-8"));
                         }
                         /**/
                     }
                     $templatefield_MTP_id = $reference_field . '-MTP_ID';
                     $templatefield_templatename_id = $reference_field . '-name';
                     $template_form_fields[$templatefield_MTP_id] = array('name' => 'MTP_template_fields[' . $reference_field . '][MTP_ID]', 'label' => NULL, 'input' => 'hidden', 'type' => 'int', 'required' => FALSE, 'validation' => FALSE, 'value' => !empty($message_templates) ? $message_templates[$context][$reference_field]->ID() : '', 'css_class' => '', 'format' => '%d', 'db-col' => 'MTP_ID');
                     $template_form_fields[$templatefield_templatename_id] = array('name' => 'MTP_template_fields[' . $reference_field . '][name]', 'label' => NULL, 'input' => 'hidden', 'type' => 'string', 'required' => FALSE, 'validation' => TRUE, 'value' => $reference_field, 'css_class' => '', 'format' => '%s', 'db-col' => 'MTP_template_field');
                 }
                 continue;
                 //skip the next stuff, we got the necessary fields here for this dataset.
             } else {
                 $field_id = $template_field . '-content';
                 $template_form_fields[$field_id] = $field_setup_array;
                 $template_form_fields[$field_id]['name'] = 'MTP_template_fields[' . $template_field . '][content]';
                 $template_form_fields[$field_id]['value'] = !empty($message_templates) && is_array($message_templates[$context]) && isset($message_templates[$context][$template_field]) ? $message_templates[$context][$template_field]->get('MTP_content') : '';
                 //do we have a validator error for this field?  if we do then we'll use that value instead
                 $template_form_fields[$field_id]['value'] = isset($validators[$template_field]) ? $validators[$template_field]['value'] : $template_form_fields[$field_id]['value'];
                 $template_form_fields[$field_id]['db-col'] = 'MTP_content';
                 $css_class = isset($field_setup_array['css_class']) ? $field_setup_array['css_class'] : '';
                 $template_form_fields[$field_id]['css_class'] = !empty($v_fields) && in_array($template_field, $v_fields) && isset($validators[$template_field]['msg']) ? 'validate-error ' . $css_class : $css_class;
                 if (isset($field_setup_array['input']) && $field_setup_array['input'] == 'wp_editor') {
                     //we want to decode the entities
                     $template_form_fields[$field_id]['value'] = $template_form_fields[$field_id]['value'];
                 }
                 /**/
             }
             //k took care of content field(s) now let's take care of others.
             $templatefield_MTP_id = $template_field . '-MTP_ID';
             $templatefield_field_templatename_id = $template_field . '-name';
             //foreach template field there are actually two form fields created
             $template_form_fields[$templatefield_MTP_id] = array('name' => 'MTP_template_fields[' . $template_field . '][MTP_ID]', 'label' => NULL, 'input' => 'hidden', 'type' => 'int', 'required' => FALSE, 'validation' => TRUE, 'value' => !empty($message_templates) ? $message_templates[$context][$template_field]->ID() : '', 'css_class' => '', 'format' => '%d', 'db-col' => 'MTP_ID');
             $template_form_fields[$templatefield_field_templatename_id] = array('name' => 'MTP_template_fields[' . $template_field . '][name]', 'label' => NULL, 'input' => 'hidden', 'type' => 'string', 'required' => FALSE, 'validation' => TRUE, 'value' => $template_field, 'css_class' => '', 'format' => '%s', 'db-col' => 'MTP_template_field');
         }
         //add other fields
         $template_form_fields['ee-msg-current-context'] = array('name' => 'MTP_context', 'label' => null, 'input' => 'hidden', 'type' => 'string', 'required' => FALSE, 'validation' => TRUE, 'value' => $context, 'css_class' => '', 'format' => '%s', 'db-col' => 'MTP_context');
         $template_form_fields['ee-msg-grp-id'] = array('name' => 'GRP_ID', 'label' => null, 'input' => 'hidden', 'type' => 'int', 'required' => FALSE, 'validation' => TRUE, 'value' => $GRP_ID, 'css_class' => '', 'format' => '%d', 'db-col' => 'GRP_ID');
         $template_form_fields['ee-msg-messenger'] = array('name' => 'MTP_messenger', 'label' => null, 'input' => 'hidden', 'type' => 'string', 'required' => FALSE, 'validation' => TRUE, 'value' => $message_template_group->messenger(), 'css_class' => '', 'format' => '%s', 'db-col' => 'MTP_messenger');
         $template_form_fields['ee-msg-message-type'] = array('name' => 'MTP_message_type', 'label' => null, 'input' => 'hidden', 'type' => 'string', 'required' => FALSE, 'validation' => TRUE, 'value' => $message_template_group->message_type(), 'css_class' => '', 'format' => '%s', 'db-col' => 'MTP_message_type');
         $sidebar_form_fields['ee-msg-is-global'] = array('name' => 'MTP_is_global', 'label' => __('Global Template', 'event_espresso'), 'input' => 'hidden', 'type' => 'int', 'required' => FALSE, 'validation' => TRUE, 'value' => $message_template_group->get('MTP_is_global'), 'css_class' => '', 'format' => '%d', 'db-col' => 'MTP_is_global');
         $sidebar_form_fields['ee-msg-is-override'] = array('name' => 'MTP_is_override', 'label' => __('Override all custom', 'event_espresso'), 'input' => $message_template_group->is_global() ? 'checkbox' : 'hidden', 'type' => 'int', 'required' => FALSE, 'validation' => TRUE, 'value' => $message_template_group->get('MTP_is_override'), 'css_class' => '', 'format' => '%d', 'db-col' => 'MTP_is_override');
         $sidebar_form_fields['ee-msg-is-active'] = array('name' => 'MTP_is_active', 'label' => __('Active Template', 'event_espresso'), 'input' => 'hidden', 'type' => 'int', 'required' => FALSE, 'validation' => TRUE, 'value' => $message_template_group->is_active(), 'css_class' => '', 'format' => '%d', 'db-col' => 'MTP_is_active');
         $sidebar_form_fields['ee-msg-deleted'] = array('name' => 'MTP_deleted', 'label' => null, 'input' => 'hidden', 'type' => 'int', 'required' => FALSE, 'validation' => TRUE, 'value' => $message_template_group->get('MTP_deleted'), 'css_class' => '', 'format' => '%d', 'db-col' => 'MTP_deleted');
         $sidebar_form_fields['ee-msg-author'] = array('name' => 'MTP_user_id', 'label' => __('Author', 'event_espresso'), 'input' => 'hidden', 'type' => 'int', 'required' => FALSE, 'validation' => FALSE, 'value' => $message_template_group->user(), 'format' => '%d', 'db-col' => 'MTP_user_id');
         $sidebar_form_fields['ee-msg-route'] = array('name' => 'action', 'input' => 'hidden', 'type' => 'string', 'value' => $action);
         $sidebar_form_fields['ee-msg-id'] = array('name' => 'id', 'input' => 'hidden', 'type' => 'int', 'value' => $GRP_ID);
         $sidebar_form_fields['ee-msg-evt-nonce'] = array('name' => $action . '_nonce', 'input' => 'hidden', 'type' => 'string', 'value' => wp_create_nonce($action . '_nonce'));
         $sidebar_array = array('ee-msg-is-global', 'ee-msg-is-override', 'ee-msg-deleted', 'ee-msg-is-active');
         if (isset($this->_req_data['template_switch']) && $this->_req_data['template_switch']) {
             $sidebar_form_fields['ee-msg-template-switch'] = array('name' => 'template_switch', 'input' => 'hidden', 'type' => 'int', 'value' => 1);
         }
         //send to field generator
         $template_fields = $this->_generate_admin_form_fields($template_form_fields);
         $sidebar_fields = $this->_generate_admin_form_fields($sidebar_form_fields);
     }
     //end if ( !empty($template_field_structure) )
     //set extra content for publish box
     $this->_template_args['publish_box_extra_content'] = $sidebar_fields;
     $this->_set_publish_post_box_vars('id', $GRP_ID);
     //add preview button
     $preview_url = parent::add_query_args_and_nonce(array('message_type' => $message_template_group->message_type(), 'messenger' => $message_template_group->messenger(), 'context' => $context, 'GRP_ID' => $GRP_ID, 'action' => 'preview_message'), $this->_admin_base_url);
     $preview_button = '<a href="' . $preview_url . '" class="button-secondary messages-preview-button">' . __('Preview', 'event_espresso') . '</a>';
     //setup context switcher
     $context_switcher_args = array('page' => 'espresso_messages', 'action' => 'edit_message_template', 'id' => $GRP_ID, 'context' => $context, 'extra' => $preview_button);
     $this->_set_context_switcher($message_template_group, $context_switcher_args);
     //main box
     $this->_template_args['template_fields'] = $template_fields;
     $this->_template_args['sidebar_box_id'] = 'details';
     $this->_template_args['action'] = $action;
     $this->_template_args['context'] = $context;
     $this->_template_args['edit_message_template_form_url'] = $edit_message_template_form_url;
     $this->_template_args['learn_more_about_message_templates_link'] = $this->_learn_more_about_message_templates_link();
     $this->_template_args['before_admin_page_content'] = $this->add_context_switcher();
     $this->_template_args['before_admin_page_content'] .= $this->_add_form_element_before();
     $this->_template_args['after_admin_page_content'] = $this->_add_form_element_after();
     $this->_template_path = $this->_template_args['GRP_ID'] ? EE_MSG_TEMPLATE_PATH . 'ee_msg_details_main_edit_meta_box.template.php' : EE_MSG_TEMPLATE_PATH . 'ee_msg_details_main_add_meta_box.template.php';
     //send along EE_Message_Template_Group object for further template use.
     $this->_template_args['MTP'] = $message_template_group;
     $this->_template_args['admin_page_content'] = EEH_Template::display_template($this->_template_path, $this->_template_args, TRUE);
     //finally, let's set the admin_page title
     $this->_admin_page_title = sprintf(__('Editing %s', 'event_espresso'), $title);
     //we need to take care of setting the shortcodes property for use elsewhere.
     $this->_set_shortcodes();
     //final template wrapper
     $this->display_admin_page_with_sidebar();
 }