You are here

constant_contact.module in Constant Contact 7.3

File

constant_contact.module
View source
<?php

/**
 * @file
 * Main module file for Constant Contact module.
 */
module_load_include('php', 'constant_contact', 'constant_contact.config');

/**
 * Implements hook_help().
 */
function constant_contact_help($path, $arg) {
  switch ($path) {
    case 'admin/help#constant_contact':
      return '<p>' . t('<p>This module works with the new <a target="_blank" href="@api">Constant Contact REST API</a>.</p>
	  <p><a target="_blank" href="@cc">Signup for a free 60-day trial</a> if you do not already have an account.</p>
	  <p>To setup a custom signup form please follow the instructions below:</p>
	  <ol>
	  <li>First you should visit the <a href="@setup">setup page</a> to enter your account username and password. (clear the cache afterwards)</li>
	  <li>Now you should edit the <a href="@settings">module settings</a> and configure the options such as which contact lists the subscribers are added to.</li>
	  <li>If you want to display a signup checkbox on the register page simply check the appropriate checkbox on the settings page under the "Register Page and Update User Page Settings" heading.</li>
	  <li>Alternatively if you want to use the form block method see <a href="@blocks">this page</a>.</li>
	  <li>To use the sync features you need to configure <a target="_blank" href="@cron">cron</a>.</li>
	  </ol>
	  <p>Detailed instructions and screenshots will be available in a future release.</p>
	  <h6>Known Issues</h6>
	  <ul>
	   <li>Custom fields may not work on your specific setup, we have tested with the profile module only.</li>
	  </ul>
	  ', array(
        '@api' => 'http://developer.constantcontact.com/doc/reference',
        '@dev' => 'http://developer.constant_contact.com/license/login',
        '@cc' => CC_TRIAL_URL,
        '@cron' => 'http://drupal.org/cron',
        '@settings' => url('admin/config/services/constant_contact/settings'),
        '@setup' => url('admin/config/services/constant_contact/setup'),
        '@blocks' => url('admin/structure/block'),
      )) . '</p>';
    case 'admin/config/services/constant_contact/lists':
      return t('<p>You can manage your contact lists below, this saves having to visit the constant contact website if you want to edit a list name</p><p>Editing a list name will not break your registration form, deleting a list will break it but only if you are using that list in the signup form, you should edit the settings to remove the list from the signup form after you have deleted it here.</p>');
    case 'admin/config/services/constant_contact/import':
      return t('<p>You can import subscribers in bulk using this page, this does not create a drupal account for the users it simply adds them to the selected constant contact mailing list(s), DO NOT use this page to import less than 25 subscribers or your constant contact account could be terminated.</p>');
    case 'admin/config/services/constant_contact/export':
      return t('<p>You can export subscribers from a certain contact list in CSV or TEXT format using this page, this operation does not happen straight away it must be scheduled using the activities API, you can download the export file from the "View Activities" tab when it\'s completed.</p>');
    case 'admin/config/services/constant_contact/activities':
      return t('<p>View your Constant Contact account activities.</p>');
    case 'admin/config/services/constant_contact/activities/%':
      return t('<p>Information about the activity if displayed below.</p>');
    case 'admin/config/services/constant_contact/settings':
      $html = t('<p>Configure the module settings.</p>');

      // if they have webform enabled display a message about the webform CC module to let them know it exists
      if (module_exists('webform') && !module_exists('webform_constant_contact')) {
        $html .= t('<p><span style="color:green; font-weight:bold; font-size:1em;">Did you know you can integrate webform with the constant contact module? - <a href="http://drupal.org/project/webform_constant_contact">To find out more click here</a></span></p>');
      }
      return $html;
    case 'admin/config/services/constant_contact/lists/add':
      return t('<p>This will add a new contact list to your Constant Contact account.</p>');
    case 'admin/config/services/constant_contact/lists/edit/%':
      return t('<p>This will update the contact list within your Constant Contact account.</p>');
    case 'admin/config/services/constant_contact/lists/delete/%':
      return t('<p>This will delete the contact list from your Constant Contact account, users subscribed to the list will be unsubscribed from the list first but will remain subscribed to other lists.</p>');
  }
}

/**
 * Implements hook_menu().
 */
function constant_contact_menu() {
  $items = array();
  $username = variable_get('cc_username', '');
  $password = variable_get('cc_password', '');

  // add user login page handler
  $items['check_login_handler'] = array(
    'page callback' => array(
      'constant_contact_check_login',
    ),
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );

  // display an intro page
  $items['admin/config/services/constant_contact'] = array(
    'title' => 'Constant contact',
    'description' => 'Setup and configure your Constant Contact signup form',
    'page callback' => 'constant_contact_intro',
    'access arguments' => array(
      'administer constant_contact',
    ),
    'weight' => 1,
    'file' => 'admin.system.inc',
  );
  $items['admin/config/services/constant_contact/intro'] = array(
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'title' => 'Constant Contact',
    'weight' => -10,
  );

  // display the setup page
  $items['admin/config/services/constant_contact/setup'] = array(
    'title' => 'Setup',
    'description' => 'Setup the constant contact module',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'constant_contact_setup',
    ),
    'access arguments' => array(
      'administer constant_contact',
    ),
    'type' => MENU_LOCAL_TASK,
    'file' => 'admin.system.inc',
    'weight' => 0,
  );

  // display the settings page
  $items['admin/config/services/constant_contact/settings'] = array(
    'title' => 'Change settings',
    'description' => 'Change your constant contact settings',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'constant_contact_settings',
    ),
    'access arguments' => array(
      'administer constant_contact',
    ),
    'type' => MENU_LOCAL_TASK,
    'file' => 'admin.system.inc',
    'weight' => 1,
  );

  // display the activities page
  $items['admin/config/services/constant_contact/activities'] = array(
    'title' => 'View Activities',
    'description' => 'View Your Constant Contact Activities',
    'page callback' => 'constant_contact_view_activities',
    'access arguments' => array(
      'administer constant_contact',
    ),
    'type' => MENU_LOCAL_TASK,
    'file' => 'admin.activities.inc',
    'weight' => 5,
  );

  // display the import page
  $items['admin/config/services/constant_contact/import'] = array(
    'title' => 'Import',
    'description' => 'Import subscribers to your constant contact mailing lists',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'constant_contact_import',
    ),
    'access arguments' => array(
      'administer constant_contact',
    ),
    'type' => MENU_LOCAL_TASK,
    'file' => 'admin.import.inc',
    'weight' => 10,
  );

  // display the export page
  $items['admin/config/services/constant_contact/export'] = array(
    'title' => 'Export',
    'description' => 'Export subscribers from your constant contact mailing lists',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'constant_contact_export',
    ),
    'access arguments' => array(
      'administer constant_contact',
    ),
    'type' => MENU_LOCAL_TASK,
    'file' => 'admin.export.inc',
    'weight' => 15,
  );
  $items['admin/config/services/constant_contact/activities/%'] = array(
    'title' => 'View Activity',
    'description' => 'View Information About This Activity',
    'page callback' => 'constant_contact_view_activity',
    'page arguments' => array(
      5,
    ),
    'access arguments' => array(
      'administer constant_contact',
    ),
    'type' => MENU_CALLBACK,
    'file' => 'admin.activities.inc',
  );
  $items['admin/config/services/constant_contact/activities/download/%'] = array(
    'title' => 'Download File',
    'description' => 'Download the Activity File',
    'page callback' => 'constant_contact_download_activity',
    'page arguments' => array(
      6,
    ),
    'access arguments' => array(
      'administer constant_contact',
    ),
    'type' => MENU_CALLBACK,
    'file' => 'admin.activities.inc',
  );

  // display the contact lists page
  $items['admin/config/services/constant_contact/lists'] = array(
    'title' => 'Contact Lists',
    'description' => 'Manage your contact lists',
    'page callback' => 'constant_contact_manage_lists',
    'access arguments' => array(
      'administer constant_contact',
    ),
    'type' => MENU_LOCAL_TASK,
    'file' => 'admin.lists.inc',
    'weight' => 20,
  );
  $items['admin/config/services/constant_contact/lists/add'] = array(
    'title' => 'Add List',
    'description' => 'Add a new contact list to constant contact',
    'page callback' => 'constant_contact_add_list',
    'access arguments' => array(
      'administer constant_contact',
    ),
    'type' => MENU_CALLBACK,
    'file' => 'admin.lists.inc',
  );
  $items['admin/config/services/constant_contact/lists/edit/%'] = array(
    'title' => 'Edit List',
    'description' => 'Edit a contact list',
    'page callback' => 'constant_contact_edit_list',
    'page arguments' => array(
      6,
    ),
    'access arguments' => array(
      'administer constant_contact',
    ),
    'type' => MENU_CALLBACK,
    'file' => 'admin.lists.inc',
  );
  $items['admin/config/services/constant_contact/lists/delete/%'] = array(
    'title' => 'Delete List',
    'description' => 'Delete a contact list',
    'page callback' => 'constant_contact_delete_list',
    'page arguments' => array(
      6,
    ),
    'access arguments' => array(
      'administer constant_contact',
    ),
    'type' => MENU_CALLBACK,
    'file' => 'admin.lists.inc',
  );
  return $items;
}

/**
 * Implements hook_permission();
 */
function constant_contact_permission() {
  return array(
    'administer constant_contact' => array(
      'title' => t('Administer Constant Contact Module'),
      'description' => t('Can edit the Constant Contact module settings and perform other admin tasks'),
    ),
  );
}

/**
 * Authenticate with the API.
 *
 * This is used for the admin settings page to check the details they entered.
 */
function constant_contact_check_login() {
  module_load_include('php', 'constant_contact', 'class.cc');
  $username = isset($_GET['username']) ? strip_tags($_GET['username']) : '';
  $password = isset($_GET['password']) ? strip_tags($_GET['password']) : '';
  if (trim($username) != '' && strpos($username, ' ') !== FALSE) {
    echo "<div style='color:red;'>Usernames with a space in them are NOT supported by the constant contact API, please change your username and try again.</div>";
  }
  elseif (trim($username) != '' && trim($password) != '') {
    $cc = new cc($username, $password);
    if (is_object($cc) && $cc
      ->get_service_description()) {

      // we have successfully connected
      echo '<div class="cc_auth_res" style="color:green;">Your login details appear to be ok, please click the "Save configuration" button to proceed with the module setup</div>';
    }
    elseif ($cc->http_response_code) {

      // oops, problem occured and we have an error code
      echo "<div' style='color:red;'>" . $cc
        ->http_get_response_code_error($cc->http_response_code) . "</div>";
    }
    else {
      echo "<div style='color:red;'>Sorry there has been an unknown error</div>";
    }
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function constant_contact_form_user_register_form_alter(&$form, &$form_state) {
  $subscribe_method = variable_get('cc_register_page_method', CC_REGISTER_PAGE_METHOD);
  $list_format = variable_get('cc_list_selection_format', CC_LIST_SELECTION_FORMAT);
  $default_opt_in = variable_get('cc_default_opt_in', CC_DEFAULT_OPT_IN);
  $show_format_choice = variable_get('cc_show_format_choice', CC_SHOW_FORMAT_CHOICE);
  $default_subscribe_format = variable_get('cc_subscribe_format', CC_SUBSCRIBE_FORMAT);
  if ($subscribe_method == 'none') {
    return;
  }
  $cc = constant_contact_create_object();
  if (!is_object($cc)) {
    return;
  }
  $selected_lists = array();
  if ($subscribe_method == 'lists') {
    $show_lists = variable_get('cc_lists', array());
    $lists = constant_contact_get_lists($cc);

    // select all lists by default, if enabled
    if ($lists && $default_opt_in) {
      foreach ($lists as $list_id => $list_name) {
        $selected_lists[] = $list_id;
      }
    }
    $options = array();

    // if they have selected some lists to show build them otherwise show all lists
    if (count($show_lists) > 0) {
      foreach ($show_lists as $list_id) {
        if (array_key_exists($list_id, $lists)) {
          $options[$list_id] = $lists[$list_id];
        }
      }
    }
    else {

      // displaqy all lists
      $options = $lists;
    }
    if (count($options) > 0) {
      if ($list_format == 'select') {
        $field_type = 'select';
      }
      else {
        $field_type = 'checkboxes';
      }
      $form['account']['cc_newsletter_lists'] = array(
        '#type' => $field_type,
        '#title' => variable_get('cc_signup_title', CC_SIGNUP_TITLE),
        '#description' => variable_get('cc_signup_description', CC_SIGNUP_DESCRIPTION),
        '#options' => $options,
        '#weight' => 11,
        '#default_value' => $selected_lists,
      );
      if ($list_format == 'select') {
        $field_size = $options && count($options) > 25 ? 25 : count($options);
        $form['account']['cc_newsletter_lists']['#multiple'] = TRUE;
        $form['account']['cc_newsletter_lists']['#size'] = $field_size;
      }
    }
  }
  else {
    $form['account']['cc_newsletter'] = array(
      '#type' => 'checkbox',
      '#title' => variable_get('cc_signup_title', CC_SIGNUP_TITLE),
      '#description' => variable_get('cc_signup_description', CC_SIGNUP_DESCRIPTION),
      '#weight' => 10,
      '#default_value' => $default_opt_in,
    );
  }
  if ($show_format_choice) {
    $form['account']['cc_email_format'] = array(
      '#type' => 'radios',
      '#title' => t('Email Format'),
      '#description' => 'You can receive emails in Text or HTML format',
      '#weight' => 12,
      '#default_value' => $default_subscribe_format,
      '#options' => $default_subscribe_format == 'HTML' ? array(
        'HTML' => t('HTML'),
        'Text' => t('Text'),
      ) : array(
        'Text' => t('Text'),
        'HTML' => t('HTML'),
      ),
    );
  }
  return $form;
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function constant_contact_form_user_profile_form_alter(&$form, &$form_state) {
  $subscribe_method = variable_get('cc_register_page_method', CC_REGISTER_PAGE_METHOD);
  $list_format = variable_get('cc_list_selection_format', CC_LIST_SELECTION_FORMAT);
  $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)) {
    return;
  }
  $selected_lists = array();

  // check if user is subscribed
  $contact = $cc
    ->query_contacts($form_state['user']->mail);

  // user is subscribed so load their options
  if ($contact) {

    // get full contact details including lists their subscribed to
    $contact = $cc
      ->get_contact($contact['id']);
    $default_subscribe_format = $contact['EmailType'];
  }
  else {
    $default_subscribe_format = variable_get('cc_subscribe_format', CC_SUBSCRIBE_FORMAT);
  }
  if ($subscribe_method == 'lists') {
    $show_lists = variable_get('cc_lists', array());
    $lists = constant_contact_get_lists($cc);

    // if the user is subscribed select the lists they are subscribed to
    if ($lists && $contact && $contact['Status'] == 'Active') {
      foreach ($contact['lists'] as $list_id) {
        $selected_lists[] = $list_id;
      }
    }
    $options = array();

    // if they have selected some lists to show build them otherwise show all lists
    if (count($show_lists) > 0) {
      foreach ($show_lists as $list_id) {
        if (array_key_exists($list_id, $lists)) {
          $options[$list_id] = $lists[$list_id];
        }
      }
    }
    else {

      // displaqy all lists
      $options = $lists;
    }
    if (count($options) > 0) {
      if ($list_format == 'select') {
        $field_type = 'select';
      }
      else {
        $field_type = 'checkboxes';
      }
      $form['account']['cc_newsletter_lists'] = array(
        '#type' => $field_type,
        '#title' => variable_get('cc_signup_title', CC_SIGNUP_TITLE),
        '#description' => variable_get('cc_signup_description', CC_SIGNUP_DESCRIPTION),
        '#options' => $options,
        '#weight' => 11,
        '#default_value' => $selected_lists,
      );
      if ($list_format == 'select') {
        $field_size = $options && count($options) > 25 ? 25 : count($options);
        $form['account']['cc_newsletter_lists']['#multiple'] = TRUE;
        $form['account']['cc_newsletter_lists']['#size'] = $field_size;
      }
    }
  }
  else {
    $form['account']['cc_newsletter'] = array(
      '#type' => 'checkbox',
      '#title' => variable_get('cc_signup_title', CC_SIGNUP_TITLE),
      '#description' => variable_get('cc_signup_description', CC_SIGNUP_DESCRIPTION),
      '#weight' => 10,
    );

    // if the user is subscribed check the checkbox
    if ($contact && $contact['Status'] == 'Active') {
      $form['account']['cc_newsletter']['#default_value'] = 1;
    }
  }
  if ($show_format_choice) {
    $form['account']['cc_email_format'] = array(
      '#type' => 'radios',
      '#title' => t('Email Format'),
      '#description' => 'You can receive emails in Text or HTML format',
      '#weight' => 12,
      '#default_value' => $default_subscribe_format,
      '#options' => $default_subscribe_format == 'HTML' ? array(
        'HTML' => t('HTML'),
        'Text' => t('Text'),
      ) : array(
        'Text' => t('Text'),
        'HTML' => t('HTML'),
      ),
    );
  }
  return $form;
}

/**
 * Implements hook_user_delete().
 */
function constant_contact_user_delete($account) {

  // we need to determine if an admin user is performing this action
  $action_type = 'contact';
  $cc = constant_contact_create_object();
  if (!is_object($cc)) {
    return;
  }

  // Find out if contact is subscribed.
  $contact = $cc
    ->query_contacts($account->mail);

  // Important, as this tells CC that the contact or customer made this action.
  $cc
    ->set_action_type($action_type);

  // If subscribed, remove them.
  if ($contact) {
    $cc
      ->delete_contact($contact['id']);
  }
}

/**
 * Implements hook_user_insert().
 */
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');
  }
}

/**
 * Implements hook_update_user().
 */
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');
  }
}

/**
 * Implements hook_user_operations().
 */
function constant_contact_user_operations() {
  $operations = array(
    'unsubscribe' => array(
      'label' => t('Unsubscribe the selected users'),
      'callback' => 'constant_contact_user_operations_unsubscribe',
    ),
    'unsubscribe_and_delete' => array(
      'label' => t('Unsubscribe and delete the selected users'),
      'callback' => 'constant_contact_user_operations_unsubscribe_and_delete',
    ),
  );
  return $operations;
}

/**
 * Callback function for admin mass unsubscribe and delete users.
 */
function constant_contact_user_operations_unsubscribe_and_delete($accounts) {
  $cc = constant_contact_create_object();
  if (!is_object($cc)) {
    return;
  }

  // Use the API to remove these, users then delete their drupal account.
  foreach ($accounts as $uid) {
    $account = user_load($uid);
    if ($account !== FALSE) {

      // Skip unsubscribing user if they are already unsubscribed.
      // Find contact.
      $contact = $cc
        ->query_contacts($account->mail);
      if ($contact) {
        $cc
          ->delete_contact($contact['id']);
      }
      user_delete(array(), (int) $uid);
    }
  }
}

/**
 * Callback function for admin mass unsubscribe users.
 */
function constant_contact_user_operations_unsubscribe($accounts) {
  $cc = constant_contact_create_object();
  if (!is_object($cc)) {
    return;
  }

  // Use the API to remove these users.
  foreach ($accounts as $uid) {
    $account = user_load($uid);

    // Skip unsubscribing user if they are already unsubscribed.
    if ($account !== FALSE) {
      user_save($account, array(
        'cc_newsletter' => 0,
      ));

      // Find contact
      $contact = $cc
        ->query_contacts($account->mail);
      if ($contact) {
        $cc
          ->delete_contact($contact['id']);
      }
    }
  }
}

/**
 * Implements hook_block_info().
 */
function constant_contact_block_info() {
  $blocks = array();
  $total_blocks_available = variable_get('cc_total_blocks_available', CC_BLOCK_COUNT);
  for ($i = 1; $i <= $total_blocks_available; ++$i) {
    $blocks[$i] = array(
      'info' => t("Constant Contact Signup Form {$i}"),
      'cache' => DRUPAL_NO_CACHE,
      'pages' => 'user*',
      'visibility' => BLOCK_VISIBILITY_NOTLISTED,
    );
  }
  return $blocks;
}

/**
 * Implements hook_block_view().
 */
function constant_contact_block_view($delta = '') {
  $block = array();
  $block['subject'] = t("Signup Form {$delta}");
  $block['content'] = drupal_get_form("constant_contact_signup_form_{$delta}", $delta);
  return $block;
}

/**
 * Implements hook_forms().
 */
function constant_contact_forms($form_id, $args) {
  $forms = array();
  $total_blocks_available = variable_get('cc_total_blocks_available', CC_BLOCK_COUNT);
  for ($i = 1; $i <= $total_blocks_available; ++$i) {
    $forms["constant_contact_signup_form_{$i}"]['callback'] = 'constant_contact_signup_form';
    $forms["constant_contact_signup_form_{$i}"]['callback arguments'] = array(
      $i,
    );
  }
  return $forms;
}

/**
 * Implements hook_block_configure().
 */
function constant_contact_block_configure($delta = '') {
  $form = array();
  $lists = array();

  // If we have an object get the users contact lists.
  $cc = constant_contact_create_object();
  if (is_object($cc)) {
    $lists = constant_contact_get_lists($cc);
  }

  // Form block settings
  $form['cc_block_show_list_selection_' . $delta] = array(
    '#type' => 'radios',
    '#title' => t('Show List Selection'),
    '#default_value' => variable_get('cc_block_show_list_selection_' . $delta, CC_BLOCK_SHOW_LIST_SELECTION),
    '#options' => array(
      0 => 'No',
      1 => 'Yes',
    ),
    '#description' => t('Do you want to display a contact list selection to the user?'),
  );
  $form['cc_block_list_selection_format_' . $delta] = array(
    '#type' => 'radios',
    '#title' => t('List Selection Format'),
    '#default_value' => variable_get('cc_block_list_selection_format_' . $delta, CC_LIST_SELECTION_FORMAT),
    '#options' => array(
      'select' => 'Multi-Select',
      'checkbox' => 'Checkboxes',
    ),
    '#description' => t('You can change the format of the list selection to either checkboxes or a multi-select drop down, the default is a multi-select drop down.'),
  );
  $form['cc_show_format_choice_' . $delta] = array(
    '#type' => 'checkbox',
    '#title' => t('Show Email Format Selection'),
    '#description' => t('Should we show an email format selection to subscribers, if you check this box it will show a selection to the user allowing them to choose between HTML and Text email'),
    '#default_value' => variable_get('cc_show_format_choice_' . $delta, CC_SHOW_FORMAT_CHOICE),
  );
  $form['cc_subscribe_format_' . $delta] = array(
    '#type' => 'radios',
    '#title' => t('Default Email Format'),
    '#description' => t('You can choose the format of email for new subscribers, HTML or Text, if you select to show a selection to the user below it will ask them what format email they would like to receive and the option you choose here will be selected by default'),
    '#default_value' => variable_get('cc_subscribe_format_' . $delta, CC_SUBSCRIBE_FORMAT),
    '#options' => array(
      'HTML' => 'HTML',
      'Text' => 'Text',
    ),
  );
  $form['cc_list_selection_title_' . $delta] = array(
    '#type' => 'textfield',
    '#title' => t('Title text for the list selection, if enabled'),
    '#description' => t('If you show the list selection this text is shown above it'),
    '#default_value' => variable_get('cc_list_selection_title_' . $delta, CC_SIGNUP_TITLE),
  );
  $form['cc_list_selection_description_' . $delta] = array(
    '#type' => 'textarea',
    '#title' => t('Description text for the list selection, if enabled'),
    '#description' => t('If you show the list selection this text is shown below it'),
    '#default_value' => variable_get('cc_list_selection_description_' . $delta, CC_SIGNUP_DESCRIPTION),
    '#maxlength' => NULL,
    '#rows' => 3,
  );
  if ($lists) {
    $form['cc_block_lists_' . $delta] = array(
      '#type' => 'select',
      '#title' => t('Contact lists'),
      '#description' => t('If you show the list selection you should choose which contact lists the user will be shown, if you do not show the list selection you should choose which contact lists the user will be automatically added to, if you dont select any lists here all lists will be used'),
      '#default_value' => variable_get('cc_block_lists_' . $delta, ''),
      '#options' => $lists,
      '#multiple' => TRUE,
      '#size' => 10,
    );
  }
  $form['cc_block_redirect_url_' . $delta] = array(
    '#type' => 'textfield',
    '#title' => t('Thank You Page'),
    '#description' => t('After a user submits the form you can send them to a URL, enter the full website address here or leave blank for no redirect'),
    '#default_value' => variable_get('cc_block_redirect_url_' . $delta, ''),
  );
  $extra_fields = array_slice(variable_get('cc_extra_fields', array()), 0, 18);
  $options = $extra_fields ? array_combine(array_values($extra_fields), array_values($extra_fields)) : array();
  $form['cc_form_block_fields_' . $delta] = array(
    '#type' => 'checkboxes',
    '#title' => t('Extra Fields'),
    '#description' => t('Select which extra fields you would like to display in the form, these will be sent to Constant Contact with the subscribers data'),
    '#default_value' => variable_get('cc_form_block_fields_' . $delta, array()),
    '#options' => $options,
  );
  $form['cc_email_field_position_' . $delta] = array(
    '#type' => 'textfield',
    '#title' => t('Position of email field'),
    '#description' => t('Enter the position of the email field in the form, numbers only, depending which extra fields you choose above you may not need to configure this option'),
    '#default_value' => variable_get('cc_email_field_position_' . $delta, 1),
    '#size' => 10,
  );
  return $form;
}

/**
 * Implements hook_block_save().
 */
function constant_contact_block_save($delta = '', $edit = array()) {
  variable_set('cc_block_show_list_selection_' . $delta, $edit['cc_block_show_list_selection_' . $delta]);
  variable_set('cc_block_list_selection_format_' . $delta, $edit['cc_block_list_selection_format_' . $delta]);
  variable_set('cc_show_format_choice_' . $delta, $edit['cc_show_format_choice_' . $delta]);
  variable_set('cc_subscribe_format_' . $delta, $edit['cc_subscribe_format_' . $delta]);
  variable_set('cc_list_selection_title_' . $delta, $edit['cc_list_selection_title_' . $delta]);
  variable_set('cc_list_selection_description_' . $delta, $edit['cc_list_selection_description_' . $delta]);
  variable_set('cc_block_lists_' . $delta, $edit['cc_block_lists_' . $delta]);
  variable_set('cc_block_redirect_url_' . $delta, $edit['cc_block_redirect_url_' . $delta]);
  variable_set('cc_form_block_fields_' . $delta, $edit['cc_form_block_fields_' . $delta]);
  variable_set('cc_email_field_position_' . $delta, $edit['cc_email_field_position_' . $delta]);
  return;
}

/**
 * Form builder for the custom signup form, added using a block.
 */
function constant_contact_signup_form($form, &$form_state, $delta) {
  $cc = constant_contact_create_object();
  $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);
  $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);
  $form_block_fields = variable_get('cc_form_block_fields_' . $delta, array());
  $email_field_position = variable_get('cc_email_field_position_' . $delta, 1);
  if (is_array($form_block_fields)) {

    // Dummy entry so we get enough for the email field.
    $form_block_fields['_email_' . $delta] = 1;
    $current_pos = 1;
    foreach ($form_block_fields as $field => $enabled) {
      if ($enabled) {
        if ($current_pos == $email_field_position) {
          $form['cc_email_' . $delta] = array(
            '#type' => 'textfield',
            '#title' => t('Email'),
            '#size' => 30,
            '#required' => TRUE,
          );
        }
        if ($field != '_email_' . $delta) {
          $fieldname = str_replace(' ', '', $field);
          $form["cc_{$fieldname}_{$delta}"] = array(
            '#type' => 'textfield',
            '#title' => check_plain($field),
            '#size' => 30,
            '#required' => TRUE,
            '#default_value' => isset($form_state['input']["cc_{$fieldname}_{$delta}"]) ? $form_state['input']["cc_{$fieldname}_{$delta}"] : '',
          );
        }
        ++$current_pos;
      }
    }
  }
  if ($show_selection && is_object($cc)) {
    $show_lists = variable_get('cc_block_lists_' . $delta, array());
    $lists = constant_contact_get_lists($cc);
    $options = array();
    foreach ($lists as $list_id => $list_name) {
      if (in_array($list_id, $show_lists)) {
        $options[$list_id] = $list_name;
      }
    }
    if (count($options) > 0) {
      if ($selection_format == 'select') {
        $field_type = 'select';
      }
      else {
        $field_type = 'checkboxes';
      }
      $form['cc_newsletter_lists_' . $delta] = array(
        '#type' => $field_type,
        '#title' => check_plain(variable_get('cc_list_selection_title_' . $delta, CC_SIGNUP_TITLE)),
        '#description' => check_plain(variable_get('cc_list_selection_description_' . $delta, CC_SIGNUP_DESCRIPTION)),
        '#options' => $options,
        '#required' => TRUE,
      );
      if ($selection_format == 'select') {
        $field_size = $options && count($options) > 25 ? 25 : count($options);
        $form['cc_newsletter_lists_' . $delta]['#multiple'] = TRUE;
        $form['cc_newsletter_lists_' . $delta]['#size'] = $field_size;
      }
    }
  }
  if ($show_format_choice) {
    $form['cc_email_format_' . $delta] = array(
      '#type' => 'radios',
      '#title' => t('Email Format'),
      '#description' => 'You can receive emails in Text or HTML format',
      '#default_value' => $default_subscribe_format,
      '#options' => $default_subscribe_format == 'HTML' ? array(
        'HTML' => t('HTML'),
        'Text' => t('Text'),
      ) : array(
        'Text' => t('Text'),
        'HTML' => t('HTML'),
      ),
    );
  }
  $form['#id'] = "cc_signup_form_{$delta}";
  $form['delta'] = array(
    '#type' => 'hidden',
    '#value' => $delta,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#name' => "submit_{$delta}",
    '#value' => t('Sign up'),
  );
  return $form;
}

/**
 * Submit handler for the custom signup form.
 */
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;
}

/**
 * Implements hook_cron().
 */
function constant_contact_cron() {
  $sync_users = variable_get('cc_sync_unsubscribed_users', CC_SYNC_UNSUBSCRIBED_USERS);

  // Syncing is disabled.
  if (!$sync_users) {
    return;
  }
  $cc = constant_contact_create_object();
  if (!is_object($cc)) {
    return;
  }

  // Measure execution time of this cron job.
  timer_start('cc_cron');

  // variable_set('cc_sync_last_run',0);
  $date_format = 'Y-m-d\\TH:i:s.000\\Z';
  $sync_last_run = variable_get('cc_sync_last_run', 0);
  if (!$sync_last_run) {

    // Set to current time. Default to one month ago.
    $sync_last_run = date($date_format, strtotime('-1 month'));
  }
  $all_users = array();
  $operation_failed = FALSE;

  // Get removed users since the date above.
  $action = "contacts?updatedsince={$sync_last_run}&listtype=removed";
  $users = $cc
    ->get_contacts($action);
  if ($users !== FALSE) {
    $all_users = array_merge($users, $all_users);
  }
  else {
    $operation_failed = TRUE;
  }

  // If we have other pages, grab them too.
  if (isset($cc->contact_meta_data->next_page) && !is_null($cc->contact_meta_data->next_page)) {
    while (!is_null($cc->contact_meta_data->next_page)) {
      $action = $cc->contact_meta_data->next_page;
      $users = $cc
        ->get_contacts($action);
      if ($users !== FALSE) {
        $all_users = array_merge($users, $all_users);
      }
      else {
        $operation_failed = TRUE;
      }
    }
  }

  // Get unsubscribed users since the date above.
  $action = "contacts?updatedsince={$sync_last_run}&listtype=do-not-mail";
  $users = $cc
    ->get_contacts($action);
  if ($users !== FALSE) {
    $all_users = array_merge($users, $all_users);
  }
  else {
    $operation_failed = TRUE;
  }

  // If we have other pages, grab them too
  if (isset($cc->contact_meta_data->next_page) && !is_null($cc->contact_meta_data->next_page)) {
    while (!is_null($cc->contact_meta_data->next_page)) {
      $action = $cc->contact_meta_data->next_page;
      $users = $cc
        ->get_contacts($action);
      if ($users !== FALSE) {
        $all_users = array_merge($users, $all_users);
      }
      else {
        $operation_failed = TRUE;
      }
    }
  }

  // If no errors occurred.
  if ($operation_failed === FALSE) {
    $users_synced = 0;

    // Loop users and change their local settings to unsubscribed.
    foreach ($all_users as $k => $v) {
      $user = user_load_by_mail($v['EmailAddress']);
      if ($user !== FALSE) {
        $newfields = array(
          'cc_newsletter_lists' => array(),
        );
        if ($v['Status'] != 'Removed') {
          $newfields['cc_newsletter'] = 0;
        }
        user_save($user, $newfields, 'account');
        ++$users_synced;
      }
    }

    // Reset sync_last_run variable to the current date and time.
    variable_set('cc_sync_last_run', date($date_format));
    if ($users_synced) {
      watchdog('Constant Contact', 'Successfully synchronized %users unsubscribed user(s) in %timetaken seconds.', array(
        '%users' => $users_synced,
        '%timetaken' => timer_read('cc_cron'),
      ));
    }
  }
  else {
    watchdog('Constant Contact', 'Failed to synchronize unsubscribed users %error.', array(
      '%error' => $cc->last_error,
    ), WATCHDOG_ERROR);
  }
  timer_stop('cc_cron');
}

/**
 * Display a friendly last error message.
 */
function constant_contact_display_last_error($status_code = 0) {
  $last_error = '';
  $status_code = intval($status_code);
  if (!$status_code) {
    return $last_error;
  }
  $last_error = 'Sorry there was a problem communicating with the constant contact server, the error given was: ';
  switch ($status_code) {
    case 400:
      $last_error .= 'Invalid Request';
      break;
    case 401:
      $last_error .= 'Unauthorized';
      break;
    case 404:
      $last_error .= 'Page Not Found';
      break;
    case 409:
      $last_error .= 'Conflict';
      break;
    case 415:
      $last_error .= 'Unsupported Media Type';
      break;
    case 500:
      $last_error .= 'Internal Server Error';
      break;
    default:
      $last_error .= 'Unknown Error';
      break;
  }
  watchdog('Constant Contact', '%error', array(
    '%error' => $last_error,
  ), WATCHDOG_ERROR);
  drupal_set_message(t('Error: %last_error', array(
    '%last_error' => $last_error,
  )), 'error');
}

/**
 * Formats field mappings into array.
 */
function constant_contact_build_field_mappings() {
  if (isset($GLOBALS['cc_extra_field_mappings'])) {
    return $GLOBALS['cc_extra_field_mappings'];
  }
  $mappings = variable_get('cc_extra_field_mappings', array());
  $field_mappings = explode(',', $mappings);
  $GLOBALS['cc_extra_field_mappings'] = array();
  if ($field_mappings) {
    foreach ($field_mappings as $mapping) {
      $bits = explode(':', $mapping);
      if (is_array($bits) && isset($bits[0], $bits[1])) {
        $GLOBALS['cc_extra_field_mappings'][trim($bits[0])] = trim($bits[1]);
      }
    }
  }
  return $GLOBALS['cc_extra_field_mappings'];
}

/**
 * Creates an object of the cc class.
 *
 * Used in many functions throughout and handles startup errors. Return false
 * for everyone else, admin will see an error but users won't Other functions
 * will fail silently
 */
function constant_contact_create_object() {
  global $user;
  $username = variable_get('cc_username', '');
  $password = variable_get('cc_password', '');
  module_load_include('php', 'constant_contact', 'class.cc');
  $cc = new cc($username, $password);
  if (!$username || !$password) {
    if (isset($user->uid) && $user->uid) {
      if (in_array('Administrator', $user->roles) || intval($user->uid) === 1) {
        drupal_set_message(t('Please enter your Constant Contact account username and password'), 'error');
      }
    }
    watchdog('Constant Contact', 'Account settings not entered', array(), WATCHDOG_ERROR);
    return FALSE;
  }
  if (is_object($cc)) {

    // we have an object
    return $cc;
  }
  elseif ($cc->http_response_code) {

    // oops, problem occured and we have an error code
    $error = $cc
      ->http_get_response_code_error($cc->http_response_code);
    watchdog('Constant Contact', '%error', array(
      '%error' => $error,
    ), WATCHDOG_ERROR);

    // if we get an unauthorized 401 error code reset the username and password
    // if we don't do this the CC account will be temporarily blocked after a few tries
    if (intval($cc->http_response_code) === 401) {
      variable_set('cc_username', '');
      variable_set('cc_password', '');
    }
    if (isset($user->uid) && $user->uid) {
      if (in_array('Administrator', $user->roles) || intval($user->uid) === 1) {
        drupal_set_message(t('Constant Contact - %error', array(
          '%error' => $error,
        )), 'error');
      }
    }
  }
  return FALSE;
}

/**
 * Sort lists based on the user defined sort field.
 */
function constant_contact_sort_lists($a, $b) {
  $sort_field = variable_get('cc_contact_list_sort_order', CC_CONTACT_LIST_SORT_ORDER);
  if ($sort_field == 'SortOrder' || $sort_field == 'id') {
    if (!isset($a[$sort_field], $b[$sort_field]) || $a[$sort_field] == $b[$sort_field]) {
      return 0;
    }
    return $a[$sort_field] < $b[$sort_field] ? -1 : 1;
  }
  elseif ($sort_field == 'Name') {
    return strcmp($a[$sort_field], $b[$sort_field]);
  }
}

/**
 * Helper function to get contact lists.
 */
function constant_contact_get_lists(&$cc, $exclude = 3, $bypass_cache = FALSE) {

  // If some cached data exists and we are not bypassing the cache use the
  // cached data.
  if (!$bypass_cache && ($cached = cache_get('cc_lists', 'cache'))) {
    return $cached->data;
  }
  $_lists = $cc
    ->get_all_lists('lists', $exclude, 'constant_contact_sort_lists');
  $lists = array();
  if ($_lists) {
    foreach ($_lists as $k => $v) {
      $lists[$v['id']] = $v['Name'];
    }
  }

  // Save to the cache.
  $expire = variable_get('cc_lists_cache_expire', CC_CONTACT_LISTS_CACHE_EXPIRE);
  cache_set('cc_lists', $lists, 'cache', time() + $expire);
  return $lists;
}

Functions

Namesort descending Description
constant_contact_block_configure Implements hook_block_configure().
constant_contact_block_info Implements hook_block_info().
constant_contact_block_save Implements hook_block_save().
constant_contact_block_view Implements hook_block_view().
constant_contact_build_field_mappings Formats field mappings into array.
constant_contact_check_login Authenticate with the API.
constant_contact_create_object Creates an object of the cc class.
constant_contact_cron Implements hook_cron().
constant_contact_display_last_error Display a friendly last error message.
constant_contact_forms Implements hook_forms().
constant_contact_form_user_profile_form_alter Implements hook_form_FORM_ID_alter().
constant_contact_form_user_register_form_alter Implements hook_form_FORM_ID_alter().
constant_contact_get_lists Helper function to get contact lists.
constant_contact_help Implements hook_help().
constant_contact_menu Implements hook_menu().
constant_contact_permission Implements hook_permission();
constant_contact_signup_form Form builder for the custom signup form, added using a block.
constant_contact_signup_form_submit Submit handler for the custom signup form.
constant_contact_sort_lists Sort lists based on the user defined sort field.
constant_contact_user_delete Implements hook_user_delete().
constant_contact_user_insert Implements hook_user_insert().
constant_contact_user_operations Implements hook_user_operations().
constant_contact_user_operations_unsubscribe Callback function for admin mass unsubscribe users.
constant_contact_user_operations_unsubscribe_and_delete Callback function for admin mass unsubscribe and delete users.
constant_contact_user_update Implements hook_update_user().