You are here

function mailchimp_campaign_campaign_form in Mailchimp 7.5

Same name and namespace in other branches
  1. 7.2 modules/mailchimp_campaign/mailchimp_campaign.admin.inc \mailchimp_campaign_campaign_form()
  2. 7.3 modules/mailchimp_campaign/includes/mailchimp_campaign.admin.inc \mailchimp_campaign_campaign_form()
  3. 7.4 modules/mailchimp_campaign/includes/mailchimp_campaign.admin.inc \mailchimp_campaign_campaign_form()

Returns a form for creating a campaign.

1 string reference to 'mailchimp_campaign_campaign_form'
mailchimp_campaign_menu in modules/mailchimp_campaign/mailchimp_campaign.module
Implements hook_menu().

File

modules/mailchimp_campaign/includes/mailchimp_campaign.admin.inc, line 10
Administration pages for mailchimp_campaign module.

Code

function mailchimp_campaign_campaign_form($form, &$form_state, MailChimpCampaign $campaign = NULL) {
  $path = drupal_get_path('module', 'mailchimp_campaign');
  drupal_add_js($path . '/js/mailchimp_campaign.utils.js', 'file');
  drupal_add_css($path . '/mailchimp_campaign.css', 'file');
  $form_state['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->settings->title : '',
  );
  $form['subject'] = array(
    '#type' => 'textfield',
    '#title' => t('Subject'),
    '#required' => TRUE,
    '#default_value' => $campaign ? $campaign->mc_data->settings->subject_line : '',
  );
  $mailchimp_lists = mailchimp_get_lists();
  $form['list_id'] = array(
    '#type' => 'select',
    '#title' => t('Audience'),
    '#description' => t('Select the audience this campaign should be sent to.'),
    '#options' => _mailchimp_campaign_build_option_list($mailchimp_lists),
    '#default_value' => $campaign ? $campaign->mc_data->recipients->list_id : -1,
    '#required' => TRUE,
    '#ajax' => array(
      'callback' => 'mailchimp_campaign_list_segment_callback',
      'method' => 'replace',
      'wrapper' => 'list-segments-wrapper',
    ),
  );
  if (!empty($form_state['values']['list_id'])) {
    $list_id = $form_state['values']['list_id'];
  }
  elseif ($campaign && $campaign->mc_data) {
    $list_id = $campaign->mc_data->recipients->list_id;
    if (isset($campaign->mc_data->recipients->segment_opts->saved_segment_id)) {
      $segment_id = $campaign->mc_data->recipients->segment_opts->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('Audience Tags'),
      '#description' => t('Select the audience tags this campaign should be sent to.'),
      '#options' => _mailchimp_campaign_build_option_list($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' => $campaign ? $campaign->mc_data->settings->reply_to : variable_get('site_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' => $campaign ? $campaign->mc_data->settings->from_name : variable_get('site_name'),
    '#size' => 40,
    '#maxlength' => 255,
  );
  $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' => _mailchimp_campaign_build_option_list(mailchimp_campaign_list_templates(FALSE, 0, 500), '-- Select --', $template_type_labels),
    '#default_value' => $campaign ? $campaign->mc_data->settings->template_id : -1,
    '#ajax' => array(
      'callback' => 'mailchimp_campaign_template_callback',
      'wrapper' => 'content-sections',
    ),
  );
  $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.'),
    '#collapsible' => FALSE,
    '#collapsed' => FALSE,
    '#tree' => TRUE,
  );
  $mc_template = NULL;
  if (!empty($form_state['values']['template_id'])) {
    $mc_template = mailchimp_campaign_get_template($form_state['values']['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];
  }
  else {
    $merge_vars = array();
  }
  if ($mc_template) {
    foreach ($mc_template->info->sections as $section => $content) {
      if (substr($section, 0, 6) == 'repeat') {
        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->sections 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 && $campaign->template[$section]) {
        $default_value = $campaign->template[$section]['value'];
        $format = $campaign->template[$section]['format'];
      }
      $form['content'][$section . '_wrapper'] = array(
        '#type' => 'fieldset',
        '#title' => check_plain(drupal_ucfirst($section)),
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
      );
      $form['content'][$section . '_wrapper'][$section] = array(
        '#type' => 'text_format',
        '#format' => $format,
        '#title' => check_plain(drupal_ucfirst($section)),
        '#default_value' => $default_value,
      );
      $entity_type = isset($form_state['values']['content'][$section . '_wrapper']['entity_import']['entity_type']) ? $form_state['values']['content'][$section . '_wrapper']['entity_import']['entity_type'] : NULL;
      $form['content'][$section . '_wrapper'] += mailchimp_campaign_get_entity_import_form_elements($entity_type, $section);
      $form['content'][$section . '_wrapper'] += mailchimp_campaign_get_merge_vars_form_elements($merge_vars, $mailchimp_lists[$list_id]->name);
    }
  }
  else {
    $section = 'html';
    $form['content']['html_wrapper'] = array(
      '#type' => 'fieldset',
      '#title' => t('Content'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    );
    $form['content']['html_wrapper']['html'] = array(
      '#type' => 'text_format',
      '#format' => $campaign ? $campaign->template['html']['format'] : 'mailchimp_campaign',
      '#title' => t('Content'),
      '#description' => t('The HTML content of the campaign.'),
      '#access' => empty($form_state['values']['template_id']),
      '#default_value' => $campaign ? $campaign->template['html']['value'] : '',
    );
    $entity_type = isset($form_state['values']['content'][$section . '_wrapper']['entity_import']['entity_type']) ? $form_state['values']['content'][$section . '_wrapper']['entity_import']['entity_type'] : NULL;
    $form['content'][$section . '_wrapper'] += mailchimp_campaign_get_entity_import_form_elements($entity_type, $section);
    $list_name = !empty($list_id) ? $mailchimp_lists[$list_id]->name : '';
    $form['content'][$section . '_wrapper'] += mailchimp_campaign_get_merge_vars_form_elements($merge_vars, $list_name);
  }

  // Message preview:
  if (isset($form_state['mailchimp_campaign_campaign_preview'])) {
    $form['preview_wrapper'] = array(
      '#title' => t('Campaign content preview'),
      '#type' => 'fieldset',
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
    );
    $form['preview_wrapper']['preview'] = array(
      '#markup' => $form_state['mailchimp_campaign_campaign_preview'],
    );
  }
  $form['actions']['preview'] = array(
    '#type' => 'submit',
    '#value' => t('Preview content'),
    '#weight' => 10,
    '#submit' => array(
      'mailchimp_campaign_campaign_preview',
    ),
  );
  $form['actions']['save'] = array(
    '#type' => 'submit',
    '#value' => t('Save as draft'),
  );
  return $form;
}