/**
  * {@inheritdoc}
  */
 public function overview()
 {
     $content = array();
     $content['campaigns_table'] = array('#type' => 'table', '#header' => array(t('Title'), t('Subject'), t('Status'), t('MailChimp List'), t('MailChimp Template'), t('Created'), t('Actions')), '#empty' => '');
     $campaigns = mailchimp_campaign_load_multiple();
     $templates = mailchimp_campaign_list_templates();
     /* @var $campaign \Drupal\mailchimp_campaign\Entity\MailchimpCampaign */
     foreach ($campaigns as $campaign) {
         $campaign_id = $campaign->getMcCampaignId();
         $archive_url = Url::fromUri($campaign->mc_data['archive_url']);
         $campaign_url = Url::fromRoute('entity.mailchimp_campaign.view', array('mailchimp_campaign' => $campaign_id));
         $list_url = Url::fromUri('https://admin.mailchimp.com/lists/dashboard/overview?id=' . $campaign->list['web_id'], array('attributes' => array('target' => '_blank')));
         $send_url = Url::fromRoute('entity.mailchimp_campaign.send', array('mailchimp_campaign' => $campaign_id));
         if ($campaign->mc_data['status'] === "save") {
             $send_link = \Drupal::l(t("Send"), $send_url);
         } else {
             $send_link = t("Sent");
         }
         $actions = array(\Drupal::l(t('View Archive'), $archive_url), \Drupal::l(t('View'), $campaign_url), $send_link);
         $content['campaigns_table'][$campaign_id]['title'] = array('#markup' => \Drupal::l($campaign->mc_data['title'], $campaign_url));
         $content['campaigns_table'][$campaign_id]['subject'] = array('#markup' => $campaign->mc_data['subject']);
         $content['campaigns_table'][$campaign_id]['status'] = array('#markup' => $campaign->mc_data['status']);
         $content['campaigns_table'][$campaign_id]['list'] = array('#markup' => \Drupal::l($campaign->list['name'], $list_url));
         if (empty($campaign->mc_data['template_id'])) {
             $content['campaigns_table'][$campaign_id]['template'] = array('#markup' => "-- none --");
         } else {
             $template_url = Url::fromUri('https://admin.mailchimp.com/templates/edit?id=' . $campaign->mc_data['template_id'], array('attributes' => array('target' => '_blank')));
             $category = FALSE;
             // Templates are grouped into categories, so we go hunting for our
             // template ID in each category.
             $template_category = array();
             foreach ($templates as $category_name => $template_category) {
                 if (isset($template_category[$campaign->mc_data['template_id']])) {
                     $category = $category_name;
                     break;
                 }
             }
             if ($category) {
                 $content['campaigns_table'][$campaign_id]['template'] = array('#markup' => \Drupal::l($template_category[$campaign->mc_data['template_id']]['name'], $template_url));
             } else {
                 $content['campaigns_table'][$campaign_id]['template'] = array('#markup' => '-- template ' . \Drupal::l($campaign->mc_data['template_id'], $template_url, array('attributes' => array('target' => '_blank'))) . ' not found --');
             }
         }
         $content['campaigns_table'][$campaign_id]['created'] = array('#markup' => $campaign->mc_data['create_time']);
         $content['campaigns_table'][$campaign_id]['actions'] = array('#markup' => implode(' | ', $actions));
     }
     return $content;
 }
예제 #2
0
 /**
  * {@inheritdoc}
  */
 public function form(array $form, FormStateInterface $form_state)
 {
     $form = parent::form($form, $form_state);
     $site_config = \Drupal::config('system.site');
     // Attach campaign JS and CSS.
     $form['#attached']['library'][] = 'mailchimp_campaign/campaign-form';
     /* @var $campaign \Drupal\mailchimp_campaign\Entity\MailchimpCampaign */
     $campaign = $this->entity;
     $form_state->set('campaign', $campaign);
     $form['title'] = array('#type' => 'textfield', '#title' => t('Title'), '#description' => t('An internal name to use for this campaign. By default, the campaign subject will be used.'), '#required' => FALSE, '#default_value' => $campaign ? $campaign->mc_data['title'] : '');
     $form['subject'] = array('#type' => 'textfield', '#title' => t('Subject'), '#required' => TRUE, '#default_value' => $campaign ? $campaign->mc_data['subject'] : '');
     $mailchimp_lists = mailchimp_get_lists();
     $form['list_id'] = array('#type' => 'select', '#title' => t('List'), '#description' => t('Select the list this campaign should be sent to.'), '#options' => $this->buildOptionList($mailchimp_lists), '#default_value' => $campaign ? $campaign->mc_data['list_id'] : -1, '#required' => TRUE, '#ajax' => array('callback' => 'Drupal\\mailchimp_campaign\\Form\\MailchimpCampaignForm::listSegmentCallback'));
     if (!empty($form_state->getValue('list_id'))) {
         $list_id = $form_state->getValue('list_id');
     } elseif ($campaign && $campaign->mc_data) {
         $list_id = $campaign->mc_data['list_id'];
         if (isset($campaign->mc_data['saved_segment']['id'])) {
             $segment_id = $campaign->mc_data['saved_segment']['id'];
         }
     }
     $list_segments = array();
     if (isset($list_id)) {
         $list_segments = mailchimp_campaign_get_list_segments($list_id, 'saved');
     }
     if (!empty($list_segments)) {
         $form['list_segment_id'] = array('#type' => 'select', '#title' => t('List Segment'), '#description' => t('Select the list segment this campaign should be sent to.'), '#options' => $this->buildOptionList($list_segments, '-- Entire list --'), '#default_value' => isset($segment_id) ? $segment_id : '');
     } else {
         $form['list_segment_id'] = array();
     }
     $form['list_segment_id']['#prefix'] = '<div id="list-segments-wrapper">';
     $form['list_segment_id']['#suffix'] = '</div>';
     $form['from_email'] = array('#type' => 'textfield', '#title' => t('From Email'), '#description' => t('the From: email address for your campaign message.'), '#default_value' => !empty($campaign->mc_data) ? $campaign->mc_data['from_email'] : $site_config->get('mail'), '#size' => 40, '#maxlength' => 255, '#required' => TRUE);
     $form['from_name'] = array('#type' => 'textfield', '#title' => t('From Name'), '#description' => t('the From: name for your campaign message (not an email address)'), '#default_value' => !empty($campaign->mc_data) ? $campaign->mc_data['from_name'] : $site_config->get('name'), '#size' => 40, '#maxlength' => 255, '#required' => TRUE);
     $template_type_labels = array('user' => 'My Custom Templates', 'basic' => 'Basic Templates', 'gallery' => 'Themes');
     $form['template_id'] = array('#type' => 'select', '#title' => t('Template'), '#description' => t('Select a MailChimp user template to use. Due to a limitation in the API, only templates that do not contain repeating sections are available. If empty, the default template will be applied.'), '#options' => $this->buildOptionList(mailchimp_campaign_list_templates(), '-- Select --', $template_type_labels), '#default_value' => $campaign ? $campaign->mc_data['template_id'] : -1, '#ajax' => array('callback' => 'Drupal\\mailchimp_campaign\\Form\\MailchimpCampaignForm::templateCallback'));
     $form['content'] = array('#id' => 'content-sections', '#type' => 'fieldset', '#title' => t('Content sections'), '#description' => t('The HTML content or, if a template is selected, the content for each section.'), '#tree' => TRUE);
     $mc_template = NULL;
     if (!empty($form_state->getValue('template_id'))) {
         $mc_template = mailchimp_campaign_get_template($form_state->getValue('template_id'));
     } else {
         if ($campaign && $campaign->mc_template) {
             $mc_template = $campaign->mc_template;
         }
     }
     if (isset($list_id)) {
         $merge_vars_list = mailchimp_get_mergevars(array($list_id));
         $merge_vars = $merge_vars_list[$list_id]['merge_vars'];
     } else {
         $merge_vars = array();
     }
     $campaign_template = $campaign->getTemplate();
     $campaign_content = $form_state->getValue('content');
     $entity_type = NULL;
     if ($mc_template) {
         if (strpos($mc_template['info']['source'], 'mc:repeatable')) {
             drupal_set_message(t('WARNING: This template has repeating sections, which are not supported. You may want to select a different template.'), 'warning');
         }
         foreach ($mc_template['info']['default_content'] as $section => $content) {
             // Set the default value and text format to either saved campaign values
             // or defaults coming from the MailChimp template.
             $default_value = $content;
             $format = 'mailchimp_campaign';
             if ($campaign_template != NULL && isset($campaign_template[$section])) {
                 $default_value = $campaign_template[$section]['value'];
                 $format = $campaign_template[$section]['format'];
             }
             $form['content'][$section . '_wrapper'] = array('#type' => 'details', '#title' => SafeMarkup::checkPlain(ucfirst($section)), '#open' => FALSE);
             $form['content'][$section . '_wrapper'][$section] = array('#type' => 'text_format', '#format' => $format, '#title' => SafeMarkup::checkPlain(ucfirst($section)), '#default_value' => $default_value);
             if (isset($campaign_content[$section . '_wrapper']['entity_import']['entity_type'])) {
                 $entity_type = $campaign_content[$section . '_wrapper']['entity_import']['entity_type'];
             }
             $form['content'][$section . '_wrapper'] += $this->getEntityImportFormElements($entity_type, $section);
             if (!empty($list_id)) {
                 $form['content'][$section . '_wrapper'] += $this->getMergeVarsFormElements($merge_vars, $mailchimp_lists[$list_id]['name']);
             }
         }
     } else {
         $section = 'html';
         $form['content']['html_wrapper'] = array('#type' => 'details', '#title' => t('Content'), '#open' => FALSE);
         $form['content']['html_wrapper']['html'] = array('#type' => 'text_format', '#format' => $campaign_template != NULL ? $campaign_template['html']['format'] : 'mailchimp_campaign', '#title' => t('Content'), '#description' => t('The HTML content of the campaign.'), '#access' => empty($form_state->getValue('template_id')), '#default_value' => $campaign_template != NULL ? $campaign_template['html']['value'] : '');
         if (isset($campaign_content[$section . '_wrapper']['entity_import']['entity_type'])) {
             $entity_type = $campaign_content[$section . '_wrapper']['entity_import']['entity_type'];
         }
         $form['content'][$section . '_wrapper'] += $this->getEntityImportFormElements($entity_type, $section);
         $list_name = !empty($list_id) ? $mailchimp_lists[$list_id]['name'] : '';
         $form['content'][$section . '_wrapper'] += $this->getMergeVarsFormElements($merge_vars, $list_name);
     }
     // Message preview:
     if (!empty($form_state->getValue('mailchimp_campaign_campaign_preview'))) {
         $form['preview_wrapper'] = array('#title' => t('Campaign content preview'), '#type' => 'details', '#open' => TRUE);
         $form['preview_wrapper']['preview'] = array('#markup' => $form_state->getValue('mailchimp_campaign_campaign_preview'));
     }
     return $form;
 }