You are here

function constant_contact_user_update in Constant Contact 7.3

Implements hook_update_user().

File

./constant_contact.module, line 636

Code

function constant_contact_user_update(&$edit, $account, $category) {
  $action_type = 'contact';
  $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);
  if ($subscribe_method == 'none') {
    return;
  }
  $cc = constant_contact_create_object();
  if (!is_object($cc) || !isset($edit['mail'])) {
    return;
  }
  $fields = variable_get('cc_extra_fields', array());
  $field_mappings = constant_contact_build_field_mappings();
  if ($subscribe_method == 'lists') {
    $lists = $edit['cc_newsletter_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;
  }
  else {
    $edit['cc_newsletter_lists'] = array();
    $lists = constant_contact_get_lists($cc);
    $enabled_lists = variable_get('cc_lists', array());
    $newlists = array();
    if (count($enabled_lists)) {
      foreach ($lists as $list_id => $list_name) {
        if (in_array($list_id, $enabled_lists)) {
          $newlists[$list_id] = $list_name;
        }
      }
    }
    else {

      // Add user to all lists.
      $newlists = $lists;
    }
    $lists = $newlists;
  }

  // Parse custom fields.
  $extra_fields = array();
  foreach ($fields as $field) {
    $fieldname = str_replace(' ', '', $field);
    if (isset($field_mappings[$fieldname])) {
      $actual_fieldname = $field_mappings[$fieldname];
      if (isset($edit[$actual_fieldname])) {
        $extra_fields[$fieldname] = $edit[$actual_fieldname];

        // 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($edit['mail']);
  $cc
    ->set_action_type($action_type);
  $status = TRUE;
  if ($contact) {
    $email_format = $contact['EmailType'];
    if ($show_format_choice) {
      $email_format = $edit['cc_email_format'];
    }
    $extra_fields['EmailType'] = $email_format;
    $contact = $cc
      ->get_contact($contact['id']);
    if ($subscribe_method == 'checkbox' && $edit['cc_newsletter']) {
      $status = $cc
        ->update_contact($contact['id'], $edit['mail'], array_keys($lists), $extra_fields);
    }
    elseif ($subscribe_method == 'checkbox' && !$edit['cc_newsletter']) {
      $status = $cc
        ->update_contact($contact['id'], $edit['mail'], array(), $extra_fields);
    }
    elseif ($subscribe_method == 'lists' && count($lists) > 0) {
      $status = $cc
        ->update_contact($contact['id'], $edit['mail'], array_keys($lists), $extra_fields);
    }
    elseif ($subscribe_method == 'lists' && !count($lists)) {
      $status = $cc
        ->update_contact($contact['id'], $edit['mail'], array(), $extra_fields);
    }
  }
  elseif ($subscribe_method == 'checkbox' && $edit['cc_newsletter']) {
    $status = $cc
      ->create_contact($edit['mail'], array_keys($lists), $extra_fields);
  }
  elseif ($subscribe_method == 'lists' && count($lists) > 0) {
    $status = $cc
      ->create_contact($edit['mail'], array_keys($lists), $extra_fields);
  }
  if ((bool) $status === FALSE) {
    drupal_set_message(t(constant_contact_display_last_error($cc->http_response_code)), 'error');
  }
}