You are here

function mailchimp_cron in Mailchimp 6.2

Same name and namespace in other branches
  1. 8 mailchimp.module \mailchimp_cron()
  2. 5.2 mailchimp.module \mailchimp_cron()
  3. 5 mailchimp.module \mailchimp_cron()
  4. 6 mailchimp.module \mailchimp_cron()
  5. 7.5 mailchimp.module \mailchimp_cron()
  6. 7 mailchimp.module \mailchimp_cron()
  7. 7.3 mailchimp.module \mailchimp_cron()
  8. 7.4 mailchimp.module \mailchimp_cron()
  9. 2.x mailchimp.module \mailchimp_cron()

Implementation of hook_cron.

Resubscribes all users to the required lists if the user has chosen to do so.

1 call to mailchimp_cron()
_mailchimp_cron_batch in ./mailchimp.drush.inc
3 string references to 'mailchimp_cron'
mailchimp_admin_settings in ./mailchimp.admin.inc
implementation of hook_admin_settings
mailchimp_uninstall in ./mailchimp.install
Implementation of hook_uninstall().
mailchimp_user in ./mailchimp.module
Implementation of hook_user

File

./mailchimp.module, line 333
Mailchimp module.

Code

function mailchimp_cron() {
  if (variable_get('mailchimp_cron', FALSE) && ($q = _mailchimp_get_api_object())) {

    // grab UIDs for active users who are pending
    $sql = "SELECT mu.uid FROM {mailchimp_user} mu LEFT OUTER JOIN {users} u ON mu.uid = u.uid WHERE mu.status = '%s' AND u.status = 1";
    $result = db_query_range($sql, array(
      MAILCHIMP_USERSTATUS_PENDING,
    ), 0, variable_get('mailchimp_batch_limit', 100));
    if ($result) {
      $lists = _mailchimp_get_required_lists();
      foreach ($lists as $key => $list) {
        $lists[$key]->batch = array();
        $lists[$key]->listMergeVars = $q
          ->listMergeVars($list->id);
        $lists[$key]->unsubscribe = array();
      }
      while ($row = db_fetch_object($result)) {
        if ($account = user_load(array(
          'uid' => $row->uid,
        ))) {
          db_query('UPDATE {mailchimp_user} SET status = \'%s\' WHERE uid = %d', MAILCHIMP_USERSTATUS_CURRENT, $account->uid);
          foreach ((array) $lists as $key => $list) {
            $is_allowed = FALSE;
            foreach ((array) $account->roles as $rid => $info) {
              if ($list->roles[$rid]) {
                $lists[$key]->batch[] = _mailchimp_load_user_list_mergevars($row->uid, $list->id, $lists[$key]->listMergeVars);
                $is_allowed = TRUE;

                // user is allowed in this list
                break;
              }
            }

            // if a user is no longer allowed in this list, add to unsubscribe batch
            if (!$is_allowed) {
              $lists[$key]->unsubscribe[] = $account->mail;
            }
          }
        }
        else {

          // user exists in mc_user table even though they don't have an account, remove
          db_query('DELETE FROM {mailchimp_user} WHERE uid = %d', $account->uid);
        }
      }
      $count = 0;
      $unsub_count = 0;
      foreach ($lists as $key => $list) {

        // subscribe batch
        if (count($lists[$key]->batch)) {
          $ret = $q
            ->listBatchSubscribe($list->id, $lists[$key]->batch, FALSE, TRUE);
          if ($ret['error_count'] > 0) {
            foreach ((array) $ret['errors'] as $error) {
              watchdog('mailchimp', 'MCAPI Error: %errormsg', array(
                '%errormsg' => $error['message'],
              ), WATCHDOG_ERROR);
            }
          }
          $count += $ret['success_count'];
        }

        // unsubscribe batch
        if (count($lists[$key]->unsubscribe)) {
          $ret = $q
            ->listBatchUnsubscribe($list->id, $lists[$key]->unsubscribe, FALSE, FALSE);
          if ($ret['error_count'] > 0) {
            foreach ((array) $ret['errors'] as $error) {
              watchdog('mailchimp', 'MCAPI Error: %errormsg', array(
                '%errormsg' => $error['message'],
              ), WATCHDOG_ERROR);
            }
          }
          $unsub_count += $ret['success_count'];
        }
      }
      watchdog('mailchimp', 'Updated !count records in MailChimp and unsubscribed !unsub emails.', array(
        '!count' => $count,
        '!unsub' => $unsub_count,
      ), WATCHDOG_NOTICE);
    }
  }
}