You are here

function _scheduler_publish in Scheduler 6

Same name and namespace in other branches
  1. 7 scheduler.cron.inc \_scheduler_publish()

Publish scheduled nodes.

Return value

TRUE if any node has been published, FALSE otherwise.

1 call to _scheduler_publish()
scheduler_cron in ./scheduler.module
Implementation of hook_cron().

File

./scheduler.module, line 748

Code

function _scheduler_publish() {
  $result = FALSE;
  $date_format = variable_get('scheduler_date_format', SCHEDULER_DATE_FORMAT);

  // If the time now is greater than the time to publish a node, publish it.
  $query_result = db_query('SELECT s.nid AS nid FROM {scheduler} s LEFT JOIN {node} n ON s.nid = n.nid WHERE n.status = 0 AND s.publish_on > 0 AND s.publish_on < %d ', time());
  $nids = array();
  while ($node = db_fetch_object($query_result)) {
    $nids[] = $node->nid;
  }
  $nids = array_unique(array_merge($nids, _scheduler_scheduler_nid_list('publish')));
  foreach ($nids as $nid) {
    $n = node_load($nid);
    $n->changed = $n->publish_on;
    $old_creation_date = $n->created;
    if (variable_get('scheduler_publish_touch_' . $n->type, 0) == 1) {
      $n->created = $n->publish_on;
    }
    $create_publishing_revision = variable_get('scheduler_publish_revision_' . $n->type, 0) == 1;
    if ($create_publishing_revision) {
      $n->revision = TRUE;
      $n->log = "Node published by scheduler module. Original creation date was " . format_date($old_creation_date, 'custom', $date_format) . ".";
    }

    // Use the actions system to publish the node.
    watchdog('scheduler', '@type: scheduled publishing of %title.', array(
      '@type' => $n->type,
      '%title' => $n->title,
    ), WATCHDOG_NOTICE, l(t('view'), 'node/' . $n->nid));
    $actions = array(
      'node_publish_action',
      'node_save_action',
    );
    $context['node'] = $n;
    actions_do($actions, $n, $context, NULL, NULL);

    // If this node is not to be unpublished, then we can delete the record.
    if (isset($n->unpublish_on) && $n->unpublish_on == 0) {
      db_query('DELETE FROM {scheduler} WHERE nid = %d', $n->nid);
    }
    else {
      db_query('UPDATE {scheduler} SET publish_on = 0 WHERE nid = %d', $n->nid);
    }

    // Invoke scheduler API.
    _scheduler_scheduler_api($n, 'publish');
    $result = TRUE;
  }
  return $result;
}