You are here

function constant_contact_signup_form_submit in Constant Contact 6.2

Same name and namespace in other branches
  1. 6.3 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 695

Code

function constant_contact_signup_form_submit($form, &$form_state) {
  $cc = constant_contact_create_object();
  $lists = variable_get('constant_contact_lists', '');
  $show_lists_selection = variable_get('constant_contact_show_list_selection', CONSTANT_CONTACT_SHOW_LIST_SELECTION);
  if ($cc) {
    $fields = array();
    if ($show_lists_selection && is_array($lists)) {
      $lists = $form_state['values']['cc_newsletter_lists'];
    }
    elseif (!$show_lists_selection) {
      $_lists = $cc
        ->get_lists();
      if ($_lists) {
        foreach ($_lists as $k => $v) {
          $_lists[$k] = $v['id'];
        }
      }
      $newlists = array();
      foreach ($_lists as $list_id) {
        if (in_array($list_id, $lists)) {
          $list = $cc
            ->get_list($list_id);
          $newlists[$list['id']] = $list['Name'];
        }
      }
      $lists = $newlists;
    }
    else {
      $lists = array();
    }
    $show_firstname = variable_get('constant_contact_show_firstname', CONSTANT_CONTACT_SHOW_FIRSTNAME);
    $show_lastname = variable_get('constant_contact_show_lastname', CONSTANT_CONTACT_SHOW_LASTNAME);
    $show_job_title = variable_get('constant_contact_show_job_title', CONSTANT_CONTACT_SHOW_JOB_TITLE);
    $show_company_name = variable_get('constant_contact_show_company_name', CONSTANT_CONTACT_SHOW_COMPANY_NAME);
    $custom_fields = variable_get('constant_contact_custom_fields', CONSTANT_CONTACT_CUSTOM_FIELDS);
    $custom_fields_bits = explode(',', $custom_fields);
    if ($show_firstname) {
      $fields['FirstName'] = $form_state['values']['cc_firstname'];
    }
    if ($show_lastname) {
      $fields['LastName'] = $form_state['values']['cc_lastname'];
    }
    if ($show_company_name) {
      $fields['CompanyName'] = $form_state['values']['cc_company_name'];
    }
    if ($show_job_title) {
      $fields['JobTitle'] = $form_state['values']['cc_job_title'];
    }
    $user_email = $form_state['values']['cc_email'];
    $cc
      ->set_action_type('contact');

    /* important, this tells CC that the contact made this action */
    $contact = $cc
      ->query_contacts($user_email);
    if ($contact) {
      $contact = $cc
        ->get_contact($contact['id']);

      // merge contact lists user is already subscribed to
      if ($lists && $contact['lists']) {
        foreach ($contact['lists'] as $list_id => $list_name) {
          if (!isset($lists[$list_id])) {
            $list = $cc
              ->get_list($list_id);
            $lists[$list_id] = $list['Name'];
          }
        }
      }
      $status = $cc
        ->update_contact($contact['id'], $user_email, array_keys($lists), $fields);
      if ($status) {
        drupal_set_message(t('Success, we have updated your subscription'));
      }
      else {
        drupal_set_message(t('Sorry, there was a problem, please ensure your email is valid and try again'), 'error');
      }
    }
    else {
      $status = $cc
        ->create_contact($user_email, array_keys($lists), $fields);
      if ($status) {
        drupal_set_message(t('Success, you are now subscribed to our mailing list'));
      }
      else {
        drupal_set_message(t('Sorry, there was a problem, please ensure your email is valid and try again'), 'error');
      }
    }
  }
  else {
    drupal_set_message(t('The username, password or API key data could not be found'), 'error');
  }
  $form_state['redirect'] = '';
  return;
}