You are here

function _simplenews_scheduler_new_edition in Simplenews Scheduler 6

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. 7 simplenews_scheduler.module \_simplenews_scheduler_new_edition()
  4. 2.0.x simplenews_scheduler.module \_simplenews_scheduler_new_edition()

Create a new newsletter edition.

1 call to _simplenews_scheduler_new_edition()
simplenews_scheduler_cron in ./simplenews_scheduler.module
Implementation of hook_cron().

File

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

Code

function _simplenews_scheduler_new_edition($nid) {
  $node = node_load($nid);
  if (module_exists('upload')) {
    $files = upload_load($node);
  }
  $node = node_build_content($node, FALSE, FALSE);
  $content = drupal_render($node->content);

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

  // Append title.
  $serial = _simplenews_scheduler_count_editions($nid) + 1;
  $node->title = theme('simplenews_scheduler_title', $node, $serial);

  // Output format for all newly created items should just be Full HTML, incase of Views output etc.
  if ($format_id = db_result(db_query('SELECT format FROM {filter_formats} WHERE name like "full html"'))) {
    $node->format = $format_id;
  }

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

  // Check upon if sending should stop with a given edition number.
  $result = db_fetch_array(db_query("SELECT stop, stop_edition FROM {simplenews_scheduler} WHERE nid = %d", $nid));
  $stop = $result['stop'];
  $stop_edition = $result['stop_edition'];

  // Don't create new edition if the edition number exceeds the given maximum value.
  if ($stop == 2 && $serial <= $stop_edition || $stop != 2) {

    // 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;

    // 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);

    // We have to call simplenews_nodeapi() directly as we wont have a new node ID before we save.
    simplenews_nodeapi($node, 'update', $teaser = NULL, $page = NULL);
    return $node->nid;
  }
}