You are here

function _classified_scheduled_build_expire in Classified Ads 7.3

Same name and namespace in other branches
  1. 6.3 classified.scheduled.inc \_classified_scheduled_build_expire()

Unpublish nodes past their expiration date.

The test on n.type is not required, since the inner join will only return such nodes anyway, but allows the query to take advantage of the core index on node.type.

Reset notify time to 0 because this field is only used for interim notifications, not expire/purge.

No addTag('node_access'): this query can be run at any time by anyone.

We SELECT first and UPDATE later in order to get a notification list.

Parameters

int $time: A UNIX timestamp. Normally not set: this was added for testing purposes.

Return value

array A per-user array of per-nid expired nodes titles.

Throws

\Exception

3 calls to _classified_scheduled_build_expire()
classified_cron in ./classified.module
Implements hook_cron().
drush_classified_expire in ./classified.drush.inc
Command callback for classified-expire. No output: nothing to return.
_classified_scheduled_page_expire in ./classified.scheduled.inc
Page callback for expirations.

File

./classified.scheduled.inc, line 54
Scheduled operations for classified.module.

Code

function _classified_scheduled_build_expire($time = NULL) {

  // Obtain the list of ads to expire.
  $expires = _classified_get_time($time);
  $q = db_select('node', 'n')
    ->comment(__FUNCTION__);
  $cn = $q
    ->innerJoin('classified_node', 'cn', 'n.nid = cn.nid');
  $results = $q
    ->fields('n', array(
    'nid',
    'title',
    'uid',
  ))
    ->condition('n.type', 'classified')
    ->condition('n.status', 1)
    ->condition("{$cn}.expires", $expires, '<')
    ->execute();
  $expired = array();
  $nids = array();
  foreach ($results as $result) {
    $expired[$result->uid][$result->nid] = $result->title;
    $nids[] = $result->nid;
  }
  unset($results);
  $count = count($nids);
  if ($count) {

    /*
     * Now perform the expiration. SQL*99 does not include join in updates, so
     * DBTNG does not have them either.
     *
     * http://api.drupal.org/api/drupal/includes--database--database.inc/function/db_update/7#comment-15464
     */
    $transaction = db_transaction();
    try {
      $q = db_update('node')
        ->comment(__FUNCTION__)
        ->fields(array(
        'status' => 0,
      ))
        ->condition('nid', $nids, 'IN');
      $touched = $q
        ->execute();
      db_update('node_revision')
        ->comment(__FUNCTION__)
        ->fields(array(
        'status' => 0,
      ))
        ->condition('nid', $nids, 'IN')
        ->execute();
      db_update('classified_node')
        ->comment(__FUNCTION__)
        ->fields(array(
        'notify' => 0,
      ))
        ->condition('nid', $nids, 'IN')
        ->execute();
    } catch (Exception $e) {
      $transaction
        ->rollback();
      watchdog_exception('classified', $e);
      throw $e;
    }
    if (function_exists('_trigger_node')) {
      $nodes = node_load_multiple($nids);
      foreach ($nodes as $node) {
        _trigger_node($node, 'node_update');
      }
    }

    // DBTNG controlled commit.
    unset($transaction);
    watchdog('classified', 'Expiration unpublished @count ads: @expired', array(
      '@count' => $touched,
      '@expired' => var_export($expired, TRUE),
    ), WATCHDOG_INFO);
  }
  else {
    watchdog('classified', 'Expiration check did not find any ad to expire.', NULL, WATCHDOG_INFO);
  }
  drupal_alter('classified_expire', $expired);
  return $expired;
}