You are here

function ad_cron in Advertisement 6.3

Same name and namespace in other branches
  1. 5.2 ad.module \ad_cron()
  2. 5 ad.module \ad_cron()
  3. 6 ad.module \ad_cron()
  4. 6.2 ad.module \ad_cron()
  5. 7 ad.module \ad_cron()

Implementation of hook_cron().

File

./ad.module, line 383

Code

function ad_cron() {
  if (time() - variable_get('ad_cron_timestamp', 0) >= 60) {

    // Locate ads that need to be activated or expired.
    $result = db_query('SELECT aid, adstatus, adtype, autoactivate, autoactivated, autoexpire, autoexpired FROM {ads} WHERE autoactivate <> 0 OR autoexpire <> 0');
    while ($ad = db_fetch_object($result)) {
      switch ($ad->adstatus) {
        case 'approved':

          // See if this ad is ready to be activated.
          if ($ad->autoactivate && $ad->autoactivate <= time()) {
            $node = node_load($ad->aid);

            // Activate the ad.
            db_query("UPDATE {ads} SET adstatus = 'active', autoactivate = 0, autoactivated = %d, activated = %d WHERE aid = %d", time(), time(), $ad->aid);
            ad_statistics_increment($ad->aid, 'autoactivated');
            ad_statistics_increment($ad->aid, 'active');
            watchdog('ad', 'Automatically activated ad %title with nid %nid.', array(
              '%title' => $node->title,
              '%nid' => $node->nid,
            ));

            // Allow modules to do special processing to automatically
            // activated advertisements.
            module_invoke('ad_' . $ad->adtype, 'adapi', 'autoactivate', $node);
          }
          else {
            if (!$ad->autoactivate) {

              // Once daily warn that there's an ad stuck in approved state.
              if (time() - variable_get("ad_autoactivate_warning_{$ad->aid}", 0) >= 8600) {
                watchdog('ad', 'Warning: ad %title with nid %nid in approved state has no autoactivate date set.', array(
                  '%title' => $node->title,
                  '%nid' => $node->nid,
                ));
                variable_set("ad_autoactivate_warning_{$ad->aid}", time());
              }
            }
          }
          break;
        case 'active':

          // See if this ad is ready to be activated.
          if ($ad->autoexpire && $ad->autoexpire <= time()) {
            $node = node_load($ad->aid);

            // Expire the ad.
            db_query("UPDATE {ads} SET adstatus = 'expired', autoexpire = 0, autoexpired = %d, expired = %d WHERE aid = %d", time(), time(), $ad->aid);
            ad_statistics_increment($ad->aid, 'autoexpired');
            ad_statistics_increment($ad->aid, 'expired');
            watchdog('ad', 'Automatically expired ad %title with nid %nid.', array(
              '%title' => $node->title,
              '%nid' => $node->nid,
            ));

            // Allow modules to do special processing to automatically
            // activated advertisements.
            module_invoke('ad_' . $ad->adtype, 'adapi', 'autoexpire', $node);
          }
          else {
            if (!$ad->autoexpire) {

              // Ad is already activated, but has autoactivate timestamp set.
              db_query("UPDATE {ads} SET autoactivate = 0 WHERE aid = %d", $ad->aid);
            }
          }
          break;
        default:
          $node = node_load($ad->aid);
          db_query('UPDATE {ads} SET autoactivate = 0, autoexpire = 0 WHERE aid = %d', $ad->aid);
          watchdog('ad', 'Warning: reset %type timestamp on advertisement %title with nid %nid because it is in %state state.', array(
            '%title' => $node->title,
            '%nid' => $node->nid,
            '%type' => $ad->autoactivate ? 'autoactivate' : 'autoexpire',
            '%state' => $ad->adstatus,
          ));
      }
    }
    variable_set('ad_cron_timestamp', time());
  }
}