You are here

function constant_contact_user_insert in Constant Contact 7.3

Implements hook_user_insert().

File

./constant_contact.module, line 521

Code

function constant_contact_user_insert(&$edit, $account, $category) {
  $subscribe_method = variable_get('cc_register_page_method', CC_REGISTER_PAGE_METHOD);
  $show_format_choice = variable_get('cc_show_format_choice', CC_SHOW_FORMAT_CHOICE);
  $default_subscribe_format = variable_get('cc_subscribe_format', CC_SUBSCRIBE_FORMAT);

  // We need to determine if an admin user is performing this action.
  $action_type = 'contact';
  if ($subscribe_method == 'none') {
    return;
  }
  $cc = constant_contact_create_object();
  if (!is_object($cc)) {
    return;
  }
  $fields = variable_get('cc_extra_fields', array());
  $field_mappings = constant_contact_build_field_mappings();
  if ($subscribe_method == 'checkbox') {
    if (!$account->cc_newsletter) {

      // User does not want to subscribe
      return;
    }
    $lists = variable_get('cc_lists', array());

    // If no lists have been selected, subscribe user to all of them.
    if (!count($lists)) {
      $lists = constant_contact_get_lists($cc);
    }
  }
  else {
    $lists = $account->cc_newsletter_lists;
    if (!is_array($lists) || !count($lists)) {

      // user does not want to subscribe to any lists.
      return;
    }
    $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;
  }

  // Parse custom fields.
  $extra_fields = array();
  if (is_array($fields)) {
    foreach ($fields as $field) {
      $fieldname = str_replace(' ', '', $field);
      if (isset($field_mappings[$fieldname]) && isset($_POST[$field_mappings[$fieldname]]['und'][0]['value'])) {
        $extra_fields[$fieldname] = check_plain($_POST[$field_mappings[$fieldname]]['und'][0]['value']);

        // CCK fields / multi-value fields - strip down array to first value.
        // structure is probably $field[$delta][$value], but try to work for any array
        if (is_array($extra_fields[$fieldname])) {
          while (is_array($extra_fields[$fieldname])) {
            $extra_fields[$fieldname] = array_shift($extra_fields[$fieldname]);
          }
        }
      }
    }
  }

  // Find contact.
  $contact = $cc
    ->query_contacts($account->mail);
  $cc
    ->set_action_type($action_type);
  $email_format = $contact ? $contact['EmailType'] : $default_subscribe_format;
  if ($show_format_choice && isset($account->cc_email_format)) {
    $email_format = $account->cc_email_format;
  }
  $extra_fields['EmailType'] = $email_format;
  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) {
        if (!isset($lists[$list_id])) {
          $list = $cc
            ->get_list($list_id);
          $lists[$list_id] = $list['Name'];
        }
      }
    }
    $status = $cc
      ->update_contact($contact['id'], $account->mail, array_keys($lists), $extra_fields);
  }
  else {
    $status = $cc
      ->create_contact($account->mail, array_keys($lists), $extra_fields);
  }
  if (!$status) {
    drupal_set_message(t(constant_contact_display_last_error($cc->http_response_code)), 'error');
  }
}