You are here

function auto_expire_cron in Auto Expire 7

Same name and namespace in other branches
  1. 5 auto_expire.module \auto_expire_cron()

Implements hook_cron().

File

./auto_expire.module, line 352

Code

function auto_expire_cron() {
  foreach (node_type_get_types() as $type => $name) {
    $code = AUTO_EXPIRE_NODE_TYPE . $type;
    if (variable_get($code . '_e', 0)) {
      $days = variable_get($code . '_d', AUTO_EXPIRE_DAYS);
      $warn = variable_get($code . '_w', AUTO_EXPIRE_WARN);
      $purge = variable_get($code . '_p', AUTO_EXPIRE_PURGE);

      // Send out expiration warnings.
      if ($warn > 0) {
        $subject = variable_get(AUTO_EXPIRE_EMAIL . 'warn_subject', '');
        $body = variable_get(AUTO_EXPIRE_EMAIL . 'warn_body', '');
        $query = db_select('auto_expire', 'e');
        $query
          ->fields('n', array(
          'nid',
          'title',
        ));
        $query
          ->join('node', 'n', 'n.nid = e.nid');
        $query
          ->condition('n.status', 1, '=');
        $query
          ->condition('e.warned', 0, '=');
        $query
          ->condition('n.type', $type, '=');
        switch (db_driver()) {
          case 'mysql':
          case 'mysqli':
            $query
              ->where('(FROM_UNIXTIME(e.expire) - INTERVAL :day DAY) <= NOW()', array(
              ':day' => $warn,
            ));
            break;
          case 'pgsql':
            $query
              ->where('((e.expire::ABSTIME::TIMESTAMP) - INTERVAL \\:days DAYS\') <= NOW()', array(
              ':day' => $warn,
            ));
            break;
        }
        $result = $query
          ->execute();
        foreach ($result as $record) {
          _auto_expire_notify_warning($record->nid, $record->title, $type, $days, $subject, $body);
          db_update('auto_expire')
            ->fields(array(
            'warned' => 1,
          ))
            ->condition('nid', $record->nid)
            ->execute();
          watchdog('auto_expire', 'Auto expire warning for node %node', array(
            '%node' => $record->nid,
          ), WATCHDOG_NOTICE, l(t('view'), 'node/' . $record->nid));
        }
      }

      // Expire
      $subject = variable_get(AUTO_EXPIRE_EMAIL . 'expired_subject', '');
      $body = variable_get(AUTO_EXPIRE_EMAIL . 'expired_body', '');
      $query = db_select('auto_expire', 'e');
      $query
        ->fields('n', array(
        'nid',
        'title',
      ));
      $query
        ->join('node', 'n', 'n.nid = e.nid');
      $query
        ->condition('n.status', 1, '=');
      $query
        ->condition('n.type', $type, '=');
      switch (db_driver()) {
        case 'mysql':
        case 'mysqli':
          $query
            ->where('FROM_UNIXTIME(e.expire) <= NOW()');
          break;
        case 'pgsql':
          $query
            ->where('(e.expire::ABSTIME::TIMESTAMP) <= NOW()');
          break;
      }
      $result = $query
        ->execute();
      foreach ($result as $record) {
        db_update('node')
          ->fields(array(
          'status' => 0,
        ))
          ->condition('nid', $record->nid)
          ->execute();
        _auto_expire_notify_expired($record->nid, $record->title, $type, $days, $subject, $body);
        watchdog('auto_expire', 'Unpublishing node %node', array(
          '%node' => $record->nid,
        ), WATCHDOG_NOTICE, l(t('view'), 'node/' . $record->nid));
      }

      // Purge
      if ($purge > 0) {
        $query = db_select('auto_expire', 'e');
        $query
          ->fields('e', array(
          'nid',
        ));
        $query
          ->join('node', 'n', 'n.nid = e.nid');
        $query
          ->condition('n.status', 0, '=');
        $query
          ->condition('n.type', $type, '=');
        switch (db_driver()) {
          case 'mysql':
          case 'mysqli':
            $query
              ->where('(FROM_UNIXTIME(e.expire) + INTERVAL :day DAY) <= NOW()', array(
              ':day' => $purge,
            ));
            break;
          case 'pgsql':
            $query
              ->where('((e.expire::ABSTIME::TIMESTAMP) + INTERVAL \\:days DAYS\') <= NOW()', array(
              ':day' => $purge,
            ));
            break;
        }
        $result = $query
          ->execute();
        $count_purged = 0;
        $nids = array();
        foreach ($result as $record) {
          $nids[] = $record->nid;
          $count_purged++;
        }
        if ($count_purged > 0) {
          node_delete_multiple($nids);
          watchdog('auto_expire', "Auto expire purged %num_purged node(s).", array(
            '%num_purged' => $count_purged,
          ), WATCHDOG_NOTICE);
        }
      }
    }
  }
}