You are here

function constant_contact_signup_form_submit in Constant Contact 7.3

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

Submit handler for the custom signup form.

File

./constant_contact.module, line 1098

Code

function constant_contact_signup_form_submit($form, &$form_state) {
  $delta = $form_state['values']['delta'];
  $auto_lists = variable_get('cc_block_lists_' . $delta, array());
  $show_selection = variable_get('cc_block_show_list_selection_' . $delta, CC_BLOCK_SHOW_LIST_SELECTION);
  $selection_format = variable_get('cc_block_list_selection_format_' . $delta, CC_LIST_SELECTION_FORMAT);
  $redirect_to = variable_get('cc_block_redirect_url_' . $delta, '');
  $show_format_choice = variable_get('cc_show_format_choice_' . $delta, CC_SHOW_FORMAT_CHOICE);
  $default_subscribe_format = variable_get('cc_subscribe_format_' . $delta, CC_SUBSCRIBE_FORMAT);
  $cc = constant_contact_create_object();
  if (!is_object($cc)) {
    return;
  }
  $fields = array();
  if ($show_selection && isset($form_state['values']['cc_newsletter_lists_' . $delta])) {

    // Subscribe user to selected lists.
    $lists = $form_state['values']['cc_newsletter_lists_' . $delta];
    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 or !isset($form_state['values']['cc_newsletter_lists_' . $delta])) {

    // Subscribe user to all lists the admin have selected.
    $lists = constant_contact_get_lists($cc);
    if (is_array($auto_lists) && count($auto_lists) > 0) {
      $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_' . $delta, 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}_{$delta}"])) {
        $fields[$fieldname] = $form_state['values']["cc_{$fieldname}_{$delta}"];
      }
    }
  }
  $user_email = $form_state['values']['cc_email_' . $delta];
  $email_format = $default_subscribe_format;
  if ($show_format_choice) {
    $email_format = $form_state['values']['cc_email_format_' . $delta];
  }
  $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_' . $delta, 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_' . $delta, t('Sorry, there was a problem, please ensure your details are valid and try again'));
    }
  }
  $form_state['redirect'] = $redirect_to;
  return;
}