/**
  * 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 function getSender()
 {
     $sender = Symphony::Database()->fetchCol('sender', 'SELECT sender from tbl_email_newsletters where id = \'' . $this->getId() . '\'');
     try {
         $sndr = SenderManager::create($sender[0]);
     } catch (Exception $e) {
     }
     return $sndr;
 }
 function __actionEdit($new = false)
 {
     $fields = array_merge($_POST['fields'], $_POST['settings']);
     try {
         $result = SenderManager::create($this->_context[1]);
         $fields['additional_headers'] = $result->additional_headers;
     } catch (Exception $e) {
     }
     if (empty($result) && !$new) {
         redirect(SYMPHONY_URL . '/extension/email_newsletter_manager/senders/');
         return false;
     }
     if (isset($_POST['action']['delete'])) {
         if (SenderManager::delete($this->_context[1])) {
             redirect(SYMPHONY_URL . '/extension/email_newsletter_manager/senders/');
             return;
         } else {
             $this->pageAlert(__('Could not delete. Database error.'), Alert::ERROR);
             return true;
         }
     }
     $errors = new XMLElement('errors');
     require_once TOOLKIT . '/util.validators.php';
     if (empty($fields['name'])) {
         $errors->appendChild(new XMLElement('name', __('This field can not be empty.')));
     } elseif (strlen(Lang::createHandle($fields['name'])) == 0) {
         $errors->appendChild(new XMLElement('name', __('This field must at least contain a number or a letter')));
     } else {
         try {
             if (SenderManager::save(str_replace('_', '-', $this->_context[1]), $fields)) {
                 redirect(SYMPHONY_URL . '/extension/email_newsletter_manager/senders/edit/' . Lang::createHandle($fields['name'], 225, '_') . '/saved');
                 return true;
             }
         } catch (Exception $e) {
             $this->pageAlert(__('Could not save: ' . $e->getMessage()), Alert::ERROR);
         }
     }
     $this->_XML->appendChild($errors);
 }