You are here

function _classified_scheduled_build_notify in Classified Ads 6.3

Same name and namespace in other branches
  1. 7.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.

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
_classified_scheduled_page_notify in ./classified.scheduled.inc
Page callback for notifications

File

./classified.scheduled.inc, line 118
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);
  switch ($kind) {
    case 'half-life':

      // cn.notify < half-life, now > half-life.
      $sq = <<<EOT
        SELECT n.nid, n.uid, n.title, cn.notify
        FROM {node} n
          INNER JOIN {classified_node} cn ON n.nid = cn.nid
        WHERE
          n.type = 'classified' AND n.status != 0
          AND cn.notify < (n.changed + cn.expires) / 2
          AND (n.changed + cn.expires) / 2 < %d
EOT;

      // Clean up whitespace for regexps.
      $sq = trim(str_replace("\n", ' ', $sq));
      $sq = db_rewrite_sql($sq);
      $q = db_query($sq, $now);
      break;
    case 'pre-expire':

      // cn.notify < expiration - 1 day, now > expiration - 1 day.
      $sq = <<<EOT
        SELECT n.nid, n.uid, n.title, cn.notify
        FROM {node} n
          INNER JOIN {classified_node} cn ON n.nid = cn.nid
        WHERE
          n.type = 'classified' AND n.status != 0
          AND cn.notify < cn.expires - 86400
          AND cn.expires < %d
EOT;

      // Clean up whitespace for regexps.
      $sq = trim(str_replace("\n", ' ', $sq));
      $sq = db_rewrite_sql($sq);
      $q = db_query($sq, $now + 86400);
      break;
    case 'pre-purge':

      // cn.notify < purge - 1 day, now > purge - 1 day
      $sq = <<<EOT
      SELECT n.nid, n.uid, n.title, cn.notify
        FROM {node} n
          INNER JOIN {classified_node} cn ON n.nid = cn.nid
        WHERE
          n.type = 'classified' AND n.status = 0
          AND cn.notify < cn.expires + %d - 86400
          AND cn.expires + %d < %d
EOT;

      // Clean up whitespace for regexps.
      $sq = trim(str_replace("\n", ' ', $sq));
      $sq = db_rewrite_sql($sq);

      // 'grace' is in days.
      $grace = (_classified_get('grace') - 1) * 24 * 60 * 60;
      $q = db_query($sq, $grace, $grace, $now);
      break;
    default:
      watchdog('classified', 'Invalid notify type requested: @kind', array(
        '@kind' => $kind,
      ), WATCHDOG_WARNING);
      break;
  }
  $notified = array();
  while ($result = db_fetch_object($q)) {
    $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 $uid => $user_notified) {
    foreach (array_keys($user_notified) as $nid) {
      $updated[] = $nid;
    }
  }
  $placeholders = db_placeholders($updated);
  $sq = <<<EOT
    UPDATE {classified_node}
    SET notify = %d
    WHERE nid IN ({<span class="php-variable">$placeholders</span>})
EOT;

  // Single array format query parameter needed with a placeholders array.
  array_unshift($updated, $now);
  $q = db_query($sq, $updated);
  $touched = db_affected_rows();
  watchdog('classified', 'Updated notification timestamp on @count ads.', array(
    '@count' => $touched,
    WATCHDOG_INFO,
  ));
  return $notified;
}