You are here

function mailchimp_campaign_campaign_form in Mailchimp 7.2

Same name and namespace in other branches
  1. 7.5 modules/mailchimp_campaign/includes/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/mailchimp_campaign.admin.inc, line 10
Administration pages for mailchimp_campaign module.

Code

function mailchimp_campaign_campaign_form($form, &$form_state, MailChimpCampaign $campaign = NULL) {
  $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['title'] : '',
  );
  $form['subject'] = array(
    '#type' => 'textfield',
    '#title' => t('Subject'),
    '#required' => TRUE,
    '#default_value' => $campaign ? $campaign->mc_data['subject'] : '',
  );
  $form['list_id'] = array(
    '#type' => 'select',
    '#title' => t('List'),
    '#description' => t('Select the list this campaign should be sent to.'),
    '#options' => _mailchimp_campaign_build_option_list(mailchimp_get_lists()),
    '#default_value' => $campaign ? $campaign->mc_data['list_id'] : -1,
    '#required' => TRUE,
  );
  $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['from_email'] : 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['from_name'] : variable_get('site_name'),
    '#size' => 40,
    '#maxlength' => 255,
  );
  $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_get_templates()),
    '#default_value' => $campaign ? $campaign->mc_data['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' => TRUE,
    '#collapsed' => FALSE,
    '#tree' => TRUE,
  );
  $mc_template = NULL;
  if (!empty($form_state['values']['template_id'])) {
    $mc_template = mailchimp_campaign_get_templates($form_state['values']['template_id']);
  }
  else {
    if ($campaign && $campaign->mc_template) {
      $mc_template = $campaign->mc_template;
    }
  }
  if ($mc_template) {
    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 MC template.
      $default_value = $content;
      $format = 'mailchimp_campaign';
      if ($campaign && $campaign->template['html_' . $section]) {
        $default_value = $campaign->template['html_' . $section]['value'];
        $format = $campaign->template['html_' . $section]['format'];
      }
      $form['content']['html_' . $section] = array(
        '#type' => 'text_format',
        '#format' => $format,
        '#title' => drupal_ucfirst($section),
        '#default_value' => $default_value,
      );
    }
  }
  else {
    $form['content']['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'] : '',
    );
  }

  // 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;
}