You are here

function campaignmonitor_subscribe_form in Campaign Monitor 6.3

Same name and namespace in other branches
  1. 7 campaignmonitor.module \campaignmonitor_subscribe_form()
1 string reference to 'campaignmonitor_subscribe_form'
template_preprocess_campaignmonitor_block_content in ./campaignmonitor.module
Implementation of preprocess_campaignmontor_block_content(). Which adds a newsletter archive link, if the archive URL is defined and user access is granted.

File

./campaignmonitor.module, line 214
Module that plugs in Campaign Monitor functionality to your Drupal web site. For Campaign Monitor information see: http://www.campaignmonitor.com/

Code

function campaignmonitor_subscribe_form(&$form_state, $list_id) {
  global $user;
  $uid = $user->uid;
  $name = '';
  $email = '';
  $subscriber_custom_fields = array();
  $default = FALSE;
  $lists = campaignmonitor_get_available_lists();
  $list = $lists[$list_id];

  // try to get name from default key values
  $account = campaignmonitor_get_field_key_values($uid);
  $name = $account[$list->namekey];
  if ($user->uid != 0) {
    $email = $user->mail;
    if (_campaignmonitor_is_subscribed(variable_get(CM_API_KEY, ''), $list_id, $email)) {
      $default = TRUE;

      // Also if subscribed get name
      $subscriber = _campaignmonitor_get_subscriber(variable_get(CM_API_KEY, ''), $list_id, $email);
      $name = $subscriber['name'];
    }
    else {
      $default = FALSE;
    }
  }
  if ($list->displayname) {
    $form['name'] = array(
      '#type' => 'textfield',
      '#title' => t('Name'),
      '#size' => 20,
      '#maxlength' => 50,
      '#required' => TRUE,
      '#default_value' => $name,
    );
  }
  $form['email'] = array(
    '#type' => 'textfield',
    '#title' => t('Email'),
    '#size' => 20,
    '#maxlength' => 100,
    '#required' => TRUE,
    '#default_value' => $email,
  );
  $custom_fields = _campaignmonitor_get_custom_fields(variable_get(CM_API_KEY, ''), $list_id);
  foreach ($custom_fields as $field) {
    $key = str_replace(array(
      '[',
      ']',
    ), '', $field['Key']);
    $saved_cfs = $list->customfields;
    $display_cf = FALSE;
    if ($saved_cfs == NULL) {

      // case that upgraded and haven't gone to admin page yet, make sure to display as that would keep same behavior
      $display_cf = TRUE;
    }
    else {
      $saved_cf = $saved_cfs[$key];
      $display_cf = $saved_cf->display;
    }
    if ($display_cf) {
      if ($field['DataType'] == 'Text') {
        $form[$key] = array(
          '#type' => 'textfield',
          '#title' => t($field['FieldName']),
          '#size' => 20,
          '#maxlength' => 100,
          '#default_value' => $subscriber['CustomFields'][$field['FieldName']],
        );
      }
      elseif ($field['DataType'] == 'MultiSelectOne') {
        $options = array();
        foreach ($field['FieldOptions']['string'] as $option) {
          $options[$option] = $option;
        }
        $_defaultvalue = $subscriber['CustomFields'][$field['FieldName']];
        if (empty($_defaultvalue)) {
          $defaultvalue = '';
        }
        else {
          $defaultvalue = $_defaultvalue;
        }
        $form[$key] = array(
          '#type' => 'select',
          '#title' => t($field['FieldName']),
          '#options' => $options,
          '#default_value' => $defaultvalue,
        );
      }
      elseif ($field['DataType'] == 'MultiSelectMany') {
        $options = array();
        foreach ($field['FieldOptions']['string'] as $option) {
          $options[$option] = $option;
        }
        $_defaultvalues = $subscriber['CustomFields'][$field['FieldName']];
        if (empty($_defaultvalues)) {
          $defaultvalues = array();
        }
        elseif (!is_array($_defaultvalues)) {
          $defaultvalues = array();
          $defaultvalues[] = $_defaultvalues;
        }
        else {
          $defaultvalues = array();
          foreach ($_defaultvalues as $dv) {
            $defaultvalues[] = $dv;
          }
        }
        $form[$key] = array(
          '#type' => 'checkboxes',
          '#title' => t($field['FieldName']),
          '#options' => $options,
          '#default_value' => $defaultvalues,
        );
      }
      elseif ($field['DataType'] == 'Number') {
        $form[$key] = array(
          '#type' => 'textfield',
          '#title' => t($field['FieldName']),
          '#size' => 20,
          '#maxlength' => 100,
          '#default_value' => $subscriber['CustomFields'][$field['FieldName']],
        );
      }
    }
  }

  /* $form['unsubscribe_newsletter'] = array(
     '#type'          => 'checkbox',
     '#title'         => t('Unsubscribe'),
     '#default_value' => $default,
     ); */
  $form['list_id'] = array(
    '#type' => 'hidden',
    '#value' => $list_id,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
  );
  return $form;
}