You are here

function ad_actions_cron in Advertisement 6.3

Same name and namespace in other branches
  1. 6.2 actions/ad_actions.module \ad_actions_cron()
  2. 7 actions/ad_actions.module \ad_actions_cron()

Implementation of hook_cron().

File

actions/ad_actions.module, line 27
Enable ad triggers and actions.

Code

function ad_actions_cron() {

  // if time, send queued notification emails
  $result = db_query('SELECT qid, context FROM {ad_actions_queue} WHERE period >= 0 AND scheduled <= %d', time());
  while ($notify = db_fetch_object($result)) {
    db_query('DELETE FROM {ad_actions_queue} WHERE qid = %d', $notify->qid);
    _ad_actions_send_email(unserialize($notify->context));
  }

  // trigger "autoactivated" actions, they will end up in the queue
  $activate = array();
  $result = db_query('SELECT aid, autoactivate FROM {ads} WHERE autoactivate > 0');
  while ($ad = db_fetch_array($result)) {
    module_invoke_all('ad_actions_trigger', 'autoactivated', $ad);
    $activate[$ad['aid']] = $ad['autoactivate'];
  }

  // trigger "autoexpired" actions, they will end up in the queue
  $expire = array();
  $result = db_query('SELECT aid, autoexpire FROM {ads} WHERE autoexpire > 0');
  while ($ad = db_fetch_array($result)) {
    module_invoke_all('ad_actions_trigger', 'autoexpired', $ad);
    $expire[$ad['aid']] = $ad['autoexpire'];
  }

  // if time, send queued "autoactivated" and "autoexpired" notification emails
  if (!empty($activate) || !empty($expire)) {
    $result = db_query('SELECT qid, aid, period, context FROM {ad_actions_queue} WHERE sent = 0 AND period < 0');
    while ($notify = db_fetch_object($result)) {
      if (isset($expire[$notify->aid])) {

        // minus a minus is a plus...
        if (time() - $notify->period >= $expire[$notify->aid]) {
          db_query('UPDATE {ad_actions_queue} SET sent = 1 WHERE qid = %d', $notify->qid);
          _ad_actions_send_email(unserialize($notify->context));
        }
      }
      else {

        // autoexpiration is no longer enabled for this ad,
        // remove action from queue.
        db_query('DELETE FROM {ad_actions_queue} WHERE qid = %d', $notify->qid);
      }
    }
  }
}