You are here

function simplenews_scheduler_get_newsletters_due in Simplenews Scheduler 8

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

Get the newsletters that need to have new editions sent.

This is a helper function for hook_cron that has the current date abstracted out so it can be tested.

Parameters

$timestamp: A unix timestamp at which to determine which newsletters are due to be sent. In ordinary operation this should be the current time.

Return value

An array of newsletter data arrays in the form of rows from the {simplenews_scheduler} table, keyed by newsletter nid.

2 calls to simplenews_scheduler_get_newsletters_due()
SimplenewsSchedulerEditionDueTest::testEditionsDue in src/Tests/SimplenewsSchedulerEditionDueTest.php
Test simplenews_scheduler_get_newsletters_due().
simplenews_scheduler_cron in ./simplenews_scheduler.module
Implements hook_cron().

File

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

Code

function simplenews_scheduler_get_newsletters_due($timestamp) {

  // Get all newsletters that need to be sent.
  $result = db_query("SELECT * FROM {simplenews_scheduler} WHERE activated = 1 AND next_run <= :now AND (stop_date > :now OR stop_date = 0)", array(
    ':now' => $timestamp,
  ));
  $newsletters = array();
  foreach ($result as $newsletter_parent_data) {

    // The node id of the parent node.
    $pid = $newsletter_parent_data->nid;

    // Check upon if sending should stop with a given edition number.
    $stop = $newsletter_parent_data->stop_type;
    $stop_edition = $newsletter_parent_data->stop_edition;
    $edition_count = db_query('SELECT COUNT(*) FROM {simplenews_scheduler_editions} WHERE pid = :pid', array(
      ':pid' => $pid,
    ))
      ->fetchField();

    // Don't create new edition if the edition number would exceed the given maximum value.
    if ($stop == 2 && $edition_count >= $stop_edition) {
      continue;
    }
    $newsletters[$pid] = $newsletter_parent_data;
  }
  return $newsletters;
}