/**
  * Displays publish panel in content area.
  *
  * @param XMLElement $wrapper
  * @param $data
  * @param $flagWithError
  * @param $fieldnamePrefix
  * @param $fieldnamePostfix
  */
 public function displayPublishPanel(&$wrapper, $data = NULL, $flagWithError = NULL, $fieldnamePrefix = NULL, $fieldnamePostfix = NULL)
 {
     Administration::instance()->Page->addScriptToHead(URL . '/extensions/email_newsletter_manager/assets/email_newsletter_manager.publish.js', 1001);
     Administration::instance()->Page->addStylesheetToHead(URL . '/extensions/email_newsletter_manager/assets/email_newsletter_manager.publish.css', 'screen', 1000);
     $this->_field_id = $this->get('id');
     $this->_entry_id = Administration::instance()->Page->_context['entry_id'];
     $status = NULL;
     // get newsletter properties
     $newsletter_properties = array();
     if ($data['newsletter_id']) {
         $newsletter = EmailNewsletterManager::get($data['newsletter_id']);
         if (is_object($newsletter->getTemplate())) {
             $newsletter_properties['template'] = $newsletter->getTemplate()->getHandle();
         }
         if (is_object($newsletter->getSender())) {
             $newsletter_properties['sender'] = $newsletter->getSender()->getHandle();
         }
         $newsletter_properties['recipients'] = $newsletter->getRecipientGroups(false, true);
         $stats = $newsletter->getStats();
         $status = $stats['status'];
     }
     // get configured templates
     $all_templates = EmailTemplateManager::listAll();
     $templates_options = array();
     if (!empty($all_templates) && is_array($all_templates)) {
         $templates = explode(',', $this->get('templates'));
         foreach ($all_templates as $template) {
             $about = $template->about;
             $handle = $template->getHandle();
             if (in_array($handle, $templates)) {
                 $templates_options[] = array($handle, $about['name']);
             }
         }
     }
     // get configured senders
     $all_senders = SenderManager::listAll();
     $senders_options = array();
     if (!empty($all_senders) && is_array($all_senders)) {
         $senders = explode(',', $this->get('senders'));
         foreach ($all_senders as $sender) {
             if (in_array($sender['handle'], $senders)) {
                 $senders_options[] = array($sender['handle'], $sender['name']);
             }
         }
     }
     // get configured recipient groups
     $all_recipient_groups = RecipientgroupManager::listAll();
     $recipient_groups_options = array();
     if (!empty($all_recipient_groups) && is_array($all_recipient_groups)) {
         $recipient_groups = explode(',', $this->get('recipient_groups'));
         foreach ($all_recipient_groups as $recipient_group) {
             if (in_array($recipient_group['handle'], $recipient_groups)) {
                 $recipient_groups_options[] = array($recipient_group['handle'], $recipient_group['name']);
             }
         }
     }
     // build header
     $header = new XMLElement('label', $this->get('label'));
     $wrapper->appendChild($header);
     // build GUI element
     $gui = new XMLElement('div');
     $gui->setAttribute('class', 'email-newsletters-gui');
     // switch status
     switch ($status) {
         case "sending":
             $heading = new XMLElement('p', __('Sending'), array('class' => 'status sending'));
             $gui->appendChild($heading);
             $this->_addStatistics($stats, $gui);
             $this->_addInfoIfApplicable($newsletter, $gui);
             $this->_addHiddenFields($newsletter, $gui);
             $gui->appendChild(new XMLElement('button', __('Pause'), array('name' => 'action[save]', 'type' => 'submit', 'value' => 'enm-pause:' . $this->_field_id, 'class' => 'button')));
             $gui->appendChild(new XMLElement('button', __('Cancel'), array('name' => 'action[save]', 'type' => 'submit', 'value' => 'enm-stop:' . $this->_field_id, 'class' => 'button delete confirm', 'data-message' => __('Are you sure you want to cancel sending?'))));
             $gui->setAttribute('class', 'email-newsletters-gui reloadable');
             break;
         case "stopped":
             $heading = new XMLElement('p', __('Stopped'), array('class' => 'status stopped'));
             $gui->appendChild($heading);
             $this->_addStatistics($stats, $gui);
             $this->_addInfoIfApplicable($newsletter, $gui);
             $this->_addHiddenFields($newsletter, $gui);
             $gui->appendChild(new XMLElement('button', __('Restart'), array('name' => 'action[save]', 'type' => 'submit', 'value' => 'enm-restart:' . $this->_field_id, 'class' => 'button confirm', 'data-message' => __('Restarting will send duplicate emails. Are you sure you want to continue?'))));
             break;
         case "paused":
             $heading = new XMLElement('p', __('Paused'), array('class' => 'status paused'));
             $gui->appendChild($heading);
             $this->_addStatistics($stats, $gui);
             $this->_addInfoIfApplicable($newsletter, $gui);
             $this->_addHiddenFields($newsletter, $gui);
             $gui->appendChild(new XMLElement('button', __('Continue'), array('name' => 'action[save]', 'type' => 'submit', 'value' => 'enm-send:' . $this->_field_id, 'class' => 'button create')));
             $gui->appendChild(new XMLElement('button', __('Cancel'), array('name' => 'action[save]', 'type' => 'submit', 'value' => 'enm-stop:' . $this->_field_id, 'class' => 'button delete confirm', 'data-message' => __('Are you sure you want to cancel sending?'))));
             break;
         case "error-template":
             $heading = new XMLElement('p', __('Error: No email template selected.'), array('class' => 'status error'));
             $gui->appendChild($heading);
             $this->_addHiddenFields($newsletter, $gui);
             $gui->appendChild(new XMLElement('button', __('Restart'), array('name' => 'action[save]', 'type' => 'submit', 'value' => 'enm-restart:' . $this->_field_id, 'class' => 'button')));
             break;
         case "error-sender":
             $heading = new XMLElement('p', __('Error: No sender selected.'), array('class' => 'status error'));
             $gui->appendChild($heading);
             $this->_addHiddenFields($newsletter, $gui);
             $gui->appendChild(new XMLElement('button', __('Restart'), array('name' => 'action[save]', 'type' => 'submit', 'value' => 'enm-restart:' . $this->_field_id, 'class' => 'button')));
             break;
         case "error-recipients":
             $heading = new XMLElement('p', __('Error: No recipient group selected.'), array('class' => 'status error'));
             $gui->appendChild($heading);
             $this->_addHiddenFields($newsletter, $gui);
             $gui->appendChild(new XMLElement('button', __('Restart'), array('name' => 'action[save]', 'type' => 'submit', 'value' => 'enm-restart:' . $this->_field_id, 'class' => 'button')));
             break;
         case "error":
             $heading = new XMLElement('p', __('Sending failed. Check the log for details.'), array('class' => 'status error'));
             $gui->appendChild($heading);
             $this->_addStatistics($stats, $gui);
             $this->_addInfoIfApplicable($newsletter, $gui);
             $this->_addHiddenFields($newsletter, $gui);
             $gui->appendChild(new XMLElement('button', __('Continue'), array('name' => 'action[save]', 'type' => 'submit', 'value' => 'enm-send:' . $this->_field_id, 'class' => 'button create')));
             $gui->appendChild(new XMLElement('button', __('Restart'), array('name' => 'action[save]', 'type' => 'submit', 'value' => 'enm-restart:' . $this->_field_id, 'class' => 'button confirm', 'data-message' => __('Restarting will send duplicate emails. Are you sure you want to continue?'))));
             break;
         case "completed":
             $heading = new XMLElement('p', __('Completed'), array('class' => 'status completed'));
             $gui->appendChild($heading);
             $this->_addStatistics($stats, $gui);
             $this->_addInfoIfApplicable($newsletter, $gui);
             $this->_addHiddenFields($newsletter, $gui);
             $gui->appendChild(new XMLElement('button', __('Restart'), array('name' => 'action[save]', 'type' => 'submit', 'value' => 'enm-restart:' . $this->_field_id, 'class' => 'button confirm', 'data-message' => __('Restarting will send duplicate emails. Are you sure you want to continue?'))));
             break;
         default:
             $heading = new XMLElement('p', __('Ready to send'), array('class' => 'status idle'));
             $gui->appendChild($heading);
             // build selector for email templates
             if (count($templates_options) > 1) {
                 $options = array();
                 $options[] = array(NULL, NULL, __('--- please select ---'));
                 foreach ($templates_options as $template) {
                     $options[] = array($template[0], $template[0] == $newsletter_properties['template'], $template[1]);
                 }
                 $gui->appendChild(Widget::Label(__('Email Template: '), Widget::Select('fields[' . $this->get('element_name') . '][template]', $options)));
             } elseif (count($templates_options) == 1) {
                 $gui->appendChild(Widget::Input('fields[' . $this->get('element_name') . '][template]', $templates_options[0][0], 'hidden'));
             } else {
                 $gui->appendChild(new XMLElement('p', __('No email template has been configured.')));
             }
             // build selector for senders
             if (count($senders_options) > 1) {
                 $options = array();
                 $options[] = array(NULL, NULL, __('--- please select ---'));
                 foreach ($senders_options as $sender) {
                     $options[] = array($sender[0], $sender[0] == $newsletter_properties['sender'], $sender[1]);
                 }
                 $gui->appendChild(Widget::Label(__('Sender: '), Widget::Select('fields[' . $this->get('element_name') . '][sender]', $options)));
             } elseif (count($senders_options) == 1) {
                 $gui->appendChild(Widget::Input('fields[' . $this->get('element_name') . '][sender]', $senders_options[0][0], 'hidden'));
             } else {
                 $gui->appendChild(new XMLElement('p', __('No sender has been configured.')));
             }
             // build checkboxes for recipient groups
             if (count($recipient_groups_options) > 1) {
                 $p = new XMLElement('p', __('Recipient Groups: '));
                 $gui->appendChild($p);
                 $p = new XMLElement('p', NULL, array('class' => 'recipient-groups'));
                 foreach ($recipient_groups_options as $recipient_group) {
                     $label = Widget::Label();
                     $input = Widget::Input('fields[' . $this->get('element_name') . '][recipient_groups][]', $recipient_group[0], 'checkbox', !empty($recipient_group[0]) && in_array($recipient_group[0], (array) $newsletter_properties['recipients']) ? array('checked' => 'checked') : NULL);
                     $label->setValue($input->generate() . $recipient_group[1]);
                     $label->setAttribute('class', 'recipient-group');
                     $p->appendChild($label);
                 }
                 $gui->appendChild($p);
             } elseif (count($recipient_groups_options) == 1) {
                 $gui->appendChild(Widget::Input('fields[' . $this->get('element_name') . '][recipient_groups][]', $recipient_groups_options[0][0], 'hidden'));
             } else {
                 $gui->appendChild(new XMLElement('p', __('No recipient group has been configured.')));
             }
             // build 'save and send' button
             $gui->appendChild(new XMLElement('button', __('Send'), array('name' => 'action[save]', 'type' => 'submit', 'value' => 'enm-send:' . $this->_field_id, 'class' => 'button create')));
     }
     $wrapper->appendChild($gui);
 }
 public static function fetchEmailTemplates()
 {
     $options = array();
     $handles = Symphony::ExtensionManager()->listInstalledHandles();
     // Email Template Filter
     // @link http://getsymphony.com/download/extensions/view/20743/
     try {
         if (in_array('emailtemplatefilter', $handles)) {
             $driver = Symphony::ExtensionManager()->getInstance('emailtemplatefilter');
             if ($driver instanceof Extension) {
                 $templates = $driver->getTemplates();
                 $g = array('label' => __('Email Template Filter'));
                 $group_options = array();
                 foreach ($templates as $template) {
                     $group_options[] = array('etf-' . $template['id'], false, $template['name']);
                 }
                 $g['options'] = $group_options;
                 if (!empty($g['options'])) {
                     $options[] = $g;
                 }
             }
         }
     } catch (Exception $ex) {
     }
     // Email Template Manager
     // @link http://getsymphony.com/download/extensions/view/64322/
     try {
         if (in_array('email_template_manager', $handles)) {
             if (file_exists(EXTENSIONS . '/email_template_manager/lib/class.emailtemplatemanager.php') && !class_exists("EmailTemplateManager")) {
                 include_once EXTENSIONS . '/email_template_manager/lib/class.emailtemplatemanager.php';
             }
             if (class_exists("EmailTemplateManager")) {
                 $templates = EmailTemplateManager::listAll();
                 $g = array('label' => __('Email Template Manager'));
                 $group_options = array();
                 foreach ($templates as $template) {
                     $group_options[] = array('etm-' . $template->getHandle(), false, $template->getName());
                 }
                 $g['options'] = $group_options;
                 if (!empty($g['options'])) {
                     $options[] = $g;
                 }
             }
         }
     } catch (Exception $ex) {
     }
     return $options;
 }