You are here

function simplenews_scheduler_cron in Simplenews Scheduler 6

Same name and namespace in other branches
  1. 8 simplenews_scheduler.module \simplenews_scheduler_cron()
  2. 5 simplenews_scheduler.module \simplenews_scheduler_cron()
  3. 6.2 simplenews_scheduler.module \simplenews_scheduler_cron()
  4. 7 simplenews_scheduler.module \simplenews_scheduler_cron()
  5. 2.0.x simplenews_scheduler.module \simplenews_scheduler_cron()

Implementation of hook_cron().

Essentially we are just checking against a status table and recreating nodes to be sent.

File

./simplenews_scheduler.module, line 244
Simplenews Scheduler module allows a schedule to be set for sending (and resending) a Simplenews item.

Code

function simplenews_scheduler_cron() {

  // Set the default intervals for scheduling.
  $intervals['day'] = 86400;
  $intervals['week'] = $intervals['day'] * 7;
  $intervals['month'] = $intervals['day'] * date_days_in_month(date('Y'), date('m'));
  foreach ($intervals as $interval => $seconds) {

    // Check daily items that need to be sent.
    $now_time = gmmktime();
    $result = db_query("SELECT * FROM {simplenews_scheduler} WHERE activated = 1 AND %d - last_run > %d AND interval = '%s' AND start_date <= %d AND %d < stop_date", $now_time, $seconds, $interval, $now_time, $now_time);
    while ($row = db_fetch_array($result)) {
      $pid = $row["nid"];

      // If returns with null don't do anything.
      $first_run = intval($row['start_date']);

      // Because the scheduler runs according to last_run timestamp and the cron
      // does not run exactly at the scheduled timestamp, this correction fixes
      // this run's timestamp ($now_time) to the right time by adding a correct interval.
      $this_run = $first_run + floor(($now_time - $first_run) / $seconds) * $seconds;

      // Create a new edition.
      $eid = _simplenews_scheduler_new_edition($row["nid"]);
      if (isset($eid)) {
        db_query("UPDATE {simplenews_scheduler} SET last_run = %d WHERE nid = %d", $this_run, $pid);
        db_query("INSERT INTO {simplenews_scheduler_editions} (eid, pid, date_issued) VALUES (%d, %d, %d)", $eid, $pid, $now_time);
        $node = node_load($eid);

        // Send the newsletter edition.
        simplenews_send_node($node);
      }
    }
  }
}