You are here

function _simplenews_scheduler_new_edition in Simplenews Scheduler 7

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

Create a new newsletter edition based on the master edition of this newsletter.

This does no checking of whether a new edition should be made; it's up to the caller to determine this first.

Parameters

$nid: The node id of the parent newsletter node to use as a template.

$edition_time: Desired edition creation time.

Return value

The node id of the new edition node.

2 calls to _simplenews_scheduler_new_edition()
SimpleNewsSchedulerEditionDueTest::testEditionsDue in tests/simplenews_scheduler.test
Test simplenews_scheduler_get_newsletters_due().
simplenews_scheduler_cron in ./simplenews_scheduler.module
Implements hook_cron().

File

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

Code

function _simplenews_scheduler_new_edition($nid, $edition_time) {

  // Load the template node and clone an edition.
  $template_node = node_load($nid);
  $edition_node = simplenews_scheduler_clone_node($template_node);

  // Set the node's creation time as the given timestamp.
  $edition_node->created = $edition_time;

  // Run the title through token replacement.
  // Get title pattern from the scheduler record, not newsletter node.
  // $edition_node->title = token_replace($edition_node->title, array('node' => $edition_node));
  $schedrecord = db_select('simplenews_scheduler', 's')
    ->fields('s')
    ->condition('nid', $template_node->nid)
    ->execute()
    ->fetchAssoc();
  $edition_node->title = token_replace($schedrecord['title'], array(
    'node' => $template_node,
  ));

  // Invoke simplenews_scheduler_edition_node() to give installed modules a
  // chance to modify the cloned edition node if necessary before it gets saved.
  drupal_alter('simplenews_scheduler_edition_node', $edition_node, $template_node);

  // Save the changes of other modules
  node_save($edition_node);

  // Insert edition data.
  $values = array(
    'eid' => $edition_node->nid,
    'pid' => $template_node->nid,
    'date_issued' => $edition_time,
  );
  db_insert('simplenews_scheduler_editions')
    ->fields($values)
    ->execute();

  // Add a watchdog entry.
  $variables = array(
    '%title' => entity_label('node', $edition_node),
  );
  $uri = entity_uri('node', $edition_node);
  $link = l(t('view'), $uri['path'], $uri['options']);
  watchdog('simplenews_sched', 'Created a new newsletter edition %title', $variables, WATCHDOG_NOTICE, $link);

  // Prepare the correct status for Simplenews to pickup.
  simplenews_newsletter_update_sent_status($edition_node);
  return $edition_node->nid;
}