You are here

function constant_contact_create_object in Constant Contact 7.3

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

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

24 calls to constant_contact_create_object()
constant_contact_add_list_form_submit in ./admin.lists.inc
Submit handler for adding a new contact list.
constant_contact_block_configure in ./constant_contact.module
Implements hook_block_configure().
constant_contact_cron in ./constant_contact.module
Implements hook_cron().
constant_contact_delete_list_form_submit in ./admin.lists.inc
Submit handler for deleting a contact list.
constant_contact_download_activity in ./admin.activities.inc
Download an activity file.

... See full list

File

./constant_contact.module, line 1396

Code

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;
}