You are here

function _classified_scheduled_build_notify in Classified Ads 7.3

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

Build one of the various notification lists.

All this work can be skipped if no module implements hook_classified_notify(): in such a case, notifications do not happen, and there is no reason to update the notify date since notifications are not being sent.

No addTag('node_access'): this is an administrative function, that needs full access.

Parameters

string $kind: The kind of notification to send, from the module-defined list of kinds.

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

Return value

array A per-user array of per-nid node titles to be notified.

3 calls to _classified_scheduled_build_notify()
classified_cron in ./classified.module
Implements hook_cron().
drush_classified_notify in ./classified.drush.inc
Command callback for classified-notify. No output: nothing to return.
_classified_scheduled_page_notify in ./classified.scheduled.inc
Page callback for notifications.

File

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

Code

function _classified_scheduled_build_notify($kind, $time = NULL) {
  $notified = array();
  $modules = module_implements('classified_notify_alter');
  if (empty($modules)) {
    return $notified;
  }
  $now = _classified_get_time($time);

  /** @var \SelectQueryInterface $q */
  $q = db_select('node', 'n')
    ->comment(__FUNCTION__);
  $cn = $q
    ->innerJoin('classified_node', 'cn', 'n.nid = cn.nid');
  $q
    ->fields('n', array(
    'nid',
    'uid',
    'title',
  ));
  switch ($kind) {
    case 'half-life':

      // cn.notify < half-life, now > half-life.
      $q
        ->condition('n.type', 'classified')
        ->condition('n.status', 0, '!=')
        ->where("{$cn}.notify < (n.changed + {$cn}.expires) / 2")
        ->where("(n.changed + {$cn}.expires) / 2 < {$now}");
      break;
    case 'pre-expire':

      // cn.notify < expiration - 1 day, now > expiration - 1 day.
      $q
        ->condition('n.type', 'classified')
        ->condition('n.status', 0, '!=')
        ->where("{$cn}.notify < {$cn}.expires - 86400")
        ->condition("{$cn}.expires", $now + 86400, '<');
      break;
    case 'pre-purge':

      // cn.notify < purge - 1 day, now > purge - 1 day
      // 'grace' is in days.
      $grace = (_classified_get('grace') - 1) * 24 * 60 * 60;
      $q
        ->condition('n.type', 'classified')
        ->condition('n.status', 0)
        ->where("{$cn}.notify < {$cn}.expires + {$grace} - 86400")
        ->condition("{$cn}.expires", $now - $grace, '<');
      break;
    default:
      watchdog('classified', 'Invalid notify type requested: @kind', array(
        '@kind' => $kind,
      ), WATCHDOG_WARNING);
      break;
  }
  $results = $q
    ->execute();
  $notified = array();
  foreach ($results as $result) {
    $notified[$result->uid][$result->nid] = $result->title;
  }

  // Alter before updating: allow modules to modify the notification list.
  drupal_alter('classified_notify', $notified, $kind);

  // Avoid building an empty update query.
  if (empty($notified)) {
    return $notified;
  }
  $updated = array();
  foreach ($notified as $user_notified) {
    foreach (array_keys($user_notified) as $nid) {
      $updated[] = $nid;
    }
  }
  $q = db_update('classified_node')
    ->comment(__FUNCTION__)
    ->fields(array(
    'notify' => $now,
  ))
    ->condition('nid', $updated, 'IN')
    ->execute();
  watchdog('classified', 'Updated notification timestamp on @count ads.', array(
    '@count' => count($updated),
  ), WATCHDOG_INFO);
  return $notified;
}