/**
  * Overriding the single_row method from parent to verify whether the $item has an accessible
  * message_type or messenger object before generating the row.
  *
  * @param EE_Message_Template_Group $item
  *
  * @return string
  */
 public function single_row($item)
 {
     $message_type = $item->message_type_obj();
     $messenger = $item->messenger_obj();
     if (!$message_type instanceof EE_message_type || !$messenger instanceof EE_messenger) {
         echo '';
         return;
     }
     parent::single_row($item);
 }
    /**
     * sets up a context switcher for edit forms
     *
     * @access  protected
     * @param  EE_Message_Template_Group $template_object the template group object being displayed on the form
     * @param array $args various things the context switcher needs.
     * @return void
     */
    protected function _set_context_switcher(EE_Message_Template_Group $template_group_object, $args)
    {
        $context_details = $template_group_object->contexts_config();
        $context_label = $template_group_object->context_label();
        ob_start();
        ?>
		<div class="ee-msg-switcher-container">
			<form method="get" action="<?php 
        echo EE_MSG_ADMIN_URL;
        ?>
" id="ee-msg-context-switcher-frm">
				<?php 
        foreach ($args as $name => $value) {
            if ($name == 'context' || empty($value) || $name == 'extra') {
                continue;
            }
            ?>
						<input type="hidden" name="<?php 
            echo $name;
            ?>
" value = "<?php 
            echo $value;
            ?>
" />
						<?php 
        }
        //setup nonce_url
        wp_nonce_field($args['action'] . '_nonce', $args['action'] . '_nonce', false);
        ?>
				<select name="context">
					<?php 
        $context_templates = $template_group_object->context_templates();
        if (is_array($context_templates)) {
            foreach ($context_templates as $context => $template_fields) {
                $checked = $context == $args['context'] ? 'selected="selected"' : '';
                ?>
					<option value="<?php 
                echo $context;
                ?>
" <?php 
                echo $checked;
                ?>
><?php 
                echo $context_details[$context]['label'];
                ?>
</option>
					<?php 
            }
        }
        ?>
				</select>
				<?php 
        $button_text = sprintf(__('Switch %s', 'event_espresso'), ucwords($context_label['label']));
        ?>
				<input id="submit-msg-context-switcher-sbmt" class="button-secondary" type="submit" value="<?php 
        echo $button_text;
        ?>
">
			</form>
			<?php 
        echo $args['extra'];
        ?>
		</div> <!-- end .ee-msg-switcher-container -->
		<?php 
        $output = ob_get_contents();
        ob_clean();
        $this->_context_switcher = $output;
    }
 /**
  * @param string $context   The context for the generated message.
  * @param EE_Messages_Addressee $recipient
  * @param array  $templates  formatted array of templates used for parsing data.
  * @param EE_Message_Template_Group $message_template_group
  * @return EE_Message | bool  (false is used when no EE_Message is generated)
  */
 protected function _setup_message_object($context, EE_Messages_Addressee $recipient, $templates, EE_Message_Template_Group $message_template_group)
 {
     //stuff we already know
     $transaction_id = $recipient->txn instanceof EE_Transaction ? $recipient->txn->ID() : 0;
     $transaction_id = empty($transaction_id) && $this->_current_data_handler->txn instanceof EE_Transaction ? $this->_current_data_handler->txn->ID() : $transaction_id;
     $message_fields = array('GRP_ID' => $message_template_group->ID(), 'TXN_ID' => $transaction_id, 'MSG_messenger' => $this->_current_messenger->name, 'MSG_message_type' => $this->_current_message_type->name, 'MSG_context' => $context);
     //recipient id and type should be on the EE_Messages_Addressee object but if this is empty, let's try to grab the
     //info from the att_obj found in the EE_Messages_Addressee object.
     if (empty($recipient->recipient_id) || empty($recipient->recipient_type)) {
         $message_fields['MSG_recipient_ID'] = $recipient->att_obj instanceof EE_Attendee ? $recipient->att_obj->ID() : 0;
         $message_fields['MSG_recipient_type'] = 'Attendee';
     } else {
         $message_fields['MSG_recipient_ID'] = $recipient->recipient_id;
         $message_fields['MSG_recipient_type'] = $recipient->recipient_type;
     }
     $message = EE_Message_Factory::create($message_fields);
     //grab valid shortcodes for shortcode parser
     $mt_shortcodes = $this->_current_message_type->get_valid_shortcodes();
     $m_shortcodes = $this->_current_messenger->get_valid_shortcodes();
     //if the 'to' field is empty (messages will ALWAYS have a "to" field, then we get out because that means this
     //context is turned off) EXCEPT if we're previewing
     if (empty($templates['to'][$context]) && !$this->_generation_queue->get_message_repository()->is_preview() && !$this->_current_messenger->allow_empty_to_field()) {
         //we silently exit here and do NOT record a fail because the message is "turned off" by having no "to" field.
         return false;
     }
     $error_msg = array();
     foreach ($templates as $field => $field_context) {
         $error_msg = array();
         //let's setup the valid shortcodes for the incoming context.
         $valid_shortcodes = $mt_shortcodes[$context];
         //merge in valid shortcodes for the field.
         $shortcodes = isset($m_shortcodes[$field]) ? $m_shortcodes[$field] : $valid_shortcodes;
         if (isset($templates[$field][$context])) {
             //prefix field.
             $column_name = 'MSG_' . $field;
             try {
                 $content = $this->_shortcode_parser->parse_message_template($templates[$field][$context], $recipient, $shortcodes, $this->_current_message_type, $this->_current_messenger, $message);
                 $message->set_field_or_extra_meta($column_name, $content);
             } catch (EE_Error $e) {
                 $error_msg[] = sprintf(__('There was a problem generating the content for the field %s: %s', 'event_espresso'), $field, $e->getMessage());
                 $message->set_STS_ID(EEM_Message::status_failed);
             }
         }
     }
     if ($message->STS_ID() === EEM_Message::status_failed) {
         $error_msg = __('There were problems generating this message:', 'event_espresso') . "\n" . implode("\n", $error_msg);
         $message->set_error_message($error_msg);
     } else {
         $message->set_STS_ID(EEM_Message::status_idle);
     }
     return $message;
 }