You are here

function constant_contact_signup_form_submit in Constant Contact 6.3

Same name and namespace in other branches
  1. 6.2 constant_contact.module \constant_contact_signup_form_submit()
  2. 7.3 constant_contact.module \constant_contact_signup_form_submit()

Custom signup form submit handler

File

./constant_contact.module, line 947

Code

function constant_contact_signup_form_submit($form, &$form_state) {
  $auto_lists = variable_get('cc_block_lists', array());
  $show_selection = variable_get('cc_block_show_list_selection', CC_BLOCK_SHOW_LIST_SELECTION);
  $selection_format = variable_get('cc_block_list_selection_format', CC_LIST_SELECTION_FORMAT);
  $redirect_to = variable_get('cc_block_redirect_url', '');
  $show_format_choice = variable_get('cc_show_format_choice', CC_SHOW_FORMAT_CHOICE);
  $default_subscribe_format = variable_get('cc_subscribe_format', CC_SUBSCRIBE_FORMAT);
  $cc = constant_contact_create_object();
  if (!is_object($cc)) {
    return;
  }
  $fields = array();
  if ($show_selection and isset($form_state['values']['cc_newsletter_lists'])) {
    $lists = $form_state['values']['cc_newsletter_lists'];
    if ($selection_format == 'checkbox' && $lists) {
      $newlists = array();
      foreach ($lists as $list_id => $enabled) {
        if (intval($enabled) !== 0) {
          $list = $cc
            ->get_list($list_id);
          $newlists[$list_id] = $list['Name'];
        }
      }
      $lists = $newlists;
    }
  }
  elseif (!$show_selection and !isset($form_state['values']['cc_newsletter_lists'])) {
    $lists = constant_contact_get_lists($cc);
    $newlists = array();
    foreach ($lists as $list_id => $list_name) {
      if (in_array($list_id, $auto_lists)) {
        $newlists[$list_id] = $list_name;
      }
    }
    $lists = $newlists;
  }
  else {
    $lists = array();
  }
  $form_block_fields = variable_get('cc_form_block_fields', array());
  if (is_array($form_block_fields)) {
    foreach ($form_block_fields as $field => $enabled) {
      $fieldname = str_replace(' ', '', $field);
      if ($enabled && isset($form_state['values']["cc_{$fieldname}"])) {
        $fields[$fieldname] = $form_state['values']["cc_{$fieldname}"];
      }
    }
  }
  $user_email = $form_state['values']['cc_email'];
  $email_format = $default_subscribe_format;
  if ($show_format_choice) {
    $email_format = $form_state['values']['cc_email_format'];
  }
  $fields['EmailType'] = $email_format;
  $cc
    ->set_action_type('contact');

  /* important, tell CC that the contact made this action */
  $contact = $cc
    ->query_contacts($user_email);
  $lists = array_keys($lists);
  if ($contact) {
    form_set_error('cc_email', t('Your email address is already subscribed'));
  }
  else {
    $status = $cc
      ->create_contact($user_email, $lists, $fields);
    if ($status) {
      if (!$redirect_to) {
        drupal_set_message(t('Success, you are now subscribed to our mailing list'));
      }
    }
    else {
      form_set_error('cc_email', t('Sorry, there was a problem, please ensure your details are valid and try again'));
    }
  }
  if (trim($redirect_to) != '') {
    $form_state['redirect'] = $redirect_to;
  }
  return;
}