You are here

function campaignmonitor_subscribe_form_submit in Campaign Monitor 7

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

Subscribe form submit handler.

File

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

Code

function campaignmonitor_subscribe_form_submit($form, &$form_state) {

  // Get a Campaign Monitor object.
  $cm = CampaignMonitor::getConnector();
  $custom_fields = array();
  if (isset($form_state['values']['CustomFields'])) {
    foreach ($form_state['values']['CustomFields'] as $key => $field) {
      if (is_array($field)) {

        // Filter out non-selected values.
        $field = array_filter($field);

        // Transform two level array into one level.
        foreach ($field as $value) {
          $custom_fields[] = array(
            'Key' => check_plain($key),
            'Value' => check_plain($value),
          );
        }
      }
      else {

        // Add non-array custom fields.
        $custom_fields[] = array(
          'Key' => check_plain($key),
          'Value' => check_plain($field),
        );
      }
    }
  }
  $list_id = $form_state['values']['list_id'];
  $name = isset($form_state['values']['name']) ? check_plain($form_state['values']['name']) : '';
  $email = check_plain($form_state['values']['email']);

  // Update subscriber information or add new subscriber to the list.
  if (!$cm
    ->subscribe($list_id, $email, $name, $custom_fields)) {
    form_set_error('', t('You were not subscribed to the list, please try again.'));
    $form_state['redirect'] = FALSE;
    return FALSE;
  }

  // Check if the user should be sent to a subscribe page.
  $lists = $cm
    ->getLists();
  if (isset($lists[$list_id]['details']['ConfirmationSuccessPage']) && !empty($lists[$list_id]['details']['ConfirmationSuccessPage'])) {
    drupal_goto($lists[$list_id]['details']['ConfirmationSuccessPage']);
  }
  else {
    drupal_set_message(t('You are now subscribed to the "@list" list.', array(
      '@list' => $lists[$list_id]['name'],
    )), 'status');
  }
}