function constant_contact_cron in Constant Contact 6.3
Same name and namespace in other branches
- 7.3 constant_contact.module \constant_contact_cron()
Cron job to handle syncing unsubscribers
File
- ./
constant_contact.module, line 1042
Code
function constant_contact_cron() {
$sync_users = variable_get('cc_sync_unsubscribed_users', CC_SYNC_UNSUBSCRIBED_USERS);
if (!$sync_users) {
return;
/* syncing is disabled */
}
$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); /* for testing only */
$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
$sync_last_run = date($date_format, strtotime('-1 month'));
/* default to one month ago */
}
$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) and !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) and !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 occured
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(array(
'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');
}