You are here

function campaignmonitor_subscribe_form in Campaign Monitor 7

Same name and namespace in other branches
  1. 6.3 campaignmonitor.module \campaignmonitor_subscribe_form()

Builds the subscription form used in blocks.

1 string reference to 'campaignmonitor_subscribe_form'
campaignmonitor_block_view in ./campaignmonitor.module
Implements hook_block_view().

File

./campaignmonitor.module, line 228
Module that plugs in Campaign Monitor functionality to your Drupal web site.

Code

function campaignmonitor_subscribe_form($form, &$form_state, $list_id) {
  global $user;

  // Get Campaign Monitor object.
  $cm = CampaignMonitor::getConnector();

  // Get lists from Campaign Monitor (or the local cache).
  $list = $cm
    ->getExtendedList($list_id);
  $list_options = variable_get('campaignmonitor_list_' . $list_id, array());

  // Set options for the form.
  $form = array(
    '#tree' => TRUE,
    '#attributes' => array(
      'class' => array(
        'campaignmonitor-subscribe-form',
        'campaignmonitor-subscribe-form-' . str_replace(' ', '-', drupal_strtolower($list['name'])),
      ),
    ),
  );

  // Try to get the e-mail address from the user object.
  if ($user->uid != 0) {
    $email = $user->mail;
  }

  // Should the name field be displayed for this user.
  if (isset($list_options['display']['name']) && $list_options['display']['name']) {

    // Token replace if the token module is present.
    if (isset($list_options['tokens']['name']) && module_exists('token') && $user->uid != 0) {
      $name = token_replace($list_options['tokens']['name'], array(), array(
        'clear' => TRUE,
      ));
    }

    // Check if the user is subscribed and get name from Campaign Monitor.
    if (!empty($email) && $cm
      ->isSubscribed($list_id, $email)) {

      // If subscribed, get name from Campaign Monitor.
      $subscriber = $cm
        ->getSubscriber($list_id, $email);
      $name = isset($subscriber['Name']) ? $subscriber['Name'] : $name;
    }
    $form['name'] = array(
      '#type' => 'textfield',
      '#title' => t('Name'),
      '#required' => TRUE,
      '#maxlength' => 200,
      '#default_value' => isset($name) ? $name : '',
    );
  }
  $form['email'] = array(
    '#type' => 'textfield',
    '#title' => t('Email'),
    '#required' => TRUE,
    '#maxlength' => 200,
    '#default_value' => isset($email) ? $email : '',
    '#element_validate' => array(
      'campaignmonitor_email_element_validate',
    ),
  );
  foreach ($list['CustomFields'] as $key => $field) {

    // Form API can't handle keys with [] in all cases.
    $form_key = str_replace(array(
      '[',
      ']',
    ), '', $key);

    // Check if field should be displayed.
    if (!isset($list_options['CustomFields']['selected'][$form_key]) || !$list_options['CustomFields']['selected'][$form_key]) {

      // Field is not selected, so continue.
      continue;
    }

    // Token replace default value, if the token module is present.
    $token = '';
    if (module_exists('token') && isset($list_options['tokens'][$form_key])) {
      $token = token_replace($list_options['tokens'][$form_key]);
    }
    switch ($field['DataType']) {
      case 'Text':
        $form['CustomFields'][$form_key] = array(
          '#type' => 'textfield',
          '#title' => check_plain($field['FieldName']),
          '#maxlength' => 200,
          '#default_value' => isset($subscriber['CustomFields'][$field['FieldName']]) ? $subscriber['CustomFields'][$field['FieldName']] : $token,
        );
        break;
      case 'MultiSelectOne':
        $options = array();
        foreach ($field['FieldOptions'] as $option) {
          $options[$option] = $option;
        }
        $form['CustomFields'][$form_key] = array(
          '#type' => 'select',
          '#title' => check_plain($field['FieldName']),
          '#options' => $options,
          '#default_value' => isset($subscriber['CustomFields'][$field['FieldName']]) ? $subscriber['CustomFields'][$field['FieldName']] : $token,
        );
        break;
      case 'MultiSelectMany':
        $options = array();
        foreach ($field['FieldOptions'] as $option) {
          $options[$option] = $option;
        }

        // If one value was selected, default is a string else an array.
        $cm_default = isset($subscriber['CustomFields'][$field['FieldName']]) ? $subscriber['CustomFields'][$field['FieldName']] : array();
        $is_array = is_array($cm_default);
        $default = array();
        foreach ($options as $value) {
          if ($is_array) {
            if (in_array($value, $cm_default)) {
              $default[$value] = $value;
            }
          }
          elseif ($cm_default == $value) {
            $default[$cm_default] = $cm_default;
          }
          else {
            $default[$value] = 0;
          }
        }
        $form['CustomFields'][$form_key] = array(
          '#type' => 'checkboxes',
          '#title' => check_plain($field['FieldName']),
          '#options' => $options,
          '#default_value' => $default,
        );
        break;
      case 'Number':
        $form['CustomFields'][$form_key] = array(
          '#type' => 'textfield',
          '#title' => check_plain($field['FieldName']),
          '#default_value' => isset($subscriber['CustomFields'][$field['FieldName']]) ? $subscriber['CustomFields'][$field['FieldName']] : $token,
        );
        break;
      case 'Date':

        // Load jQuery datepicker to ensure the right date format.
        drupal_add_library('system', 'ui.datepicker');
        $form['CustomFields'][$form_key] = array(
          '#type' => 'textfield',
          '#title' => check_plain($field['FieldName']),
          '#default_value' => isset($subscriber['CustomFields'][$field['FieldName']]) ? $subscriber['CustomFields'][$field['FieldName']] : $token,
          '#attributes' => array(
            'class' => array(
              'campaignmonitor-date',
            ),
          ),
          '#attached' => array(
            'js' => array(
              'type' => 'file',
              'data' => drupal_get_path('module', 'campaignmonitor') . '/js/campaignmonitor.js',
            ),
          ),
        );
        break;
    }
  }
  $form['list_id'] = array(
    '#type' => 'hidden',
    '#default_value' => $list_id,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Subscribe'),
  );
  return $form;
}