You are here

function constant_contact_cron in Constant Contact 7.3

Same name and namespace in other branches
  1. 6.3 constant_contact.module \constant_contact_cron()

Implements hook_cron().

File

./constant_contact.module, line 1197

Code

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