You are here

function _simplenews_scheduler_new_edition in Simplenews Scheduler 6.2

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

Create a new newsletter edition.

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
Implementation of hook_cron().

File

./simplenews_scheduler.module, line 659
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) {
  $node = node_load($nid);
  if (module_exists('upload')) {
    $files = upload_load($node);
  }

  // Switch to the anonymous user to render node content.
  // This prevents things like Views admin links from showing in edition node body.
  global $user;
  if ($user->uid) {

    // Store the current user and session so we can restore them.
    $original_user = $user;
    $old_state = session_save_session();

    // Disable session saving and load the anonymous user.
    session_save_session(FALSE);
    $user = user_load(0);
  }

  // Render node content.
  $node = node_build_content($node, FALSE, FALSE);
  $content = drupal_render($node->content);

  // Keep the teaser in sync with the rendered node content.
  $node->teaser = node_teaser($node->body, isset($node->format) ? $node->format : NULL);

  // Restore the original user if necessary.
  if (isset($original_user)) {
    $user = $original_user;
    session_save_session($old_state);
  }

  // Store taxonomy terms to save later to the node.
  $terms = $node->taxonomy;

  // Output format for all newly created items should just be Full HTML, incase of Views output etc.
  if ($format_id = _simplenews_scheduler_get_full_html_format()) {
    $node->format = $format_id;
  }

  // Trigger it for sending.
  $node->simplenews['send'] = 1;

  // Mark as new with removeing node ID and creation date.
  unset($node->nid, $node->created, $node->path);

  // Mark as new edition.
  $node->is_edition = TRUE;

  // Set the creation time.
  $node->created = $edition_time;

  // Run the title through token replacement.
  $title_pattern = $node->simplenews_scheduler['title'];
  $node->title = token_replace($title_pattern, 'node', $node);

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

  // Now save it as a new node.
  node_save($node);

  // Save taxonomy terms.
  taxonomy_node_save($node, $terms);
  watchdog('simplenews_sched', 'Saved new node ready to be sent. Node ID: !nid', array(
    '!nid' => $node->nid,
  ));

  // If the node has attachments.
  if (isset($files) && count($files)) {

    // Simply copy the corresponding records in files and upload tables without duplicate the file.
    foreach ($files as $file) {
      db_query_range("INSERT INTO {files} (uid, filename, filepath, filemime, filesize, status, timestamp) (SELECT uid, filename, filepath, filemime, filesize, status, timestamp FROM {files} WHERE filename = '%s')", $file->filename, 0, 1);
      db_query("INSERT INTO {upload} (fid, nid, vid, list, description, weight) VALUES ((SELECT MAX(fid) AS fid FROM files WHERE filename = '%s'), %d, %d, %d, '%s', %d)", $file->filename, $node->nid, $node->vid, $file->list, $file->description, $file->weight);
    }
  }

  // Prepare the correct status for Simplenews to pickup.
  db_query("UPDATE {simplenews_newsletters} SET s_status=1 WHERE nid=%d", $node->nid);

  // ensure this edition has the same simplenews_newsletter settings as the parent
  // also ensures the 'tid' is set, so we use the same newsletter settings as the parent
  if ($newsletter = db_fetch_array(db_query("SELECT * from {simplenews_newsletters} WHERE nid = %d", $nid))) {
    db_query("UPDATE {simplenews_newsletters} SET s_status = 1, tid = %d, s_format = '%s', priority = %d, receipt = %d WHERE nid=%d", $newsletter['tid'], $newsletter['s_format'], $newsletter['priority'], $newsletter['receipt'], $node->nid);
  }

  // Record the new edition.
  db_query("INSERT INTO {simplenews_scheduler_editions} (eid, pid, date_issued) VALUES (%d, %d, %d)", $node->nid, $nid, $edition_time);
  return $node->nid;
}