You are here

function _scheduler_unpublish in Scheduler 6

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

Unpublish scheduled nodes.

Return value

TRUE is any node has been unpublished, FALSE otherwise.

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

File

./scheduler.module, line 806

Code

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

  // If the time is greater than the time to unpublish a node, unpublish it.
  $query_result = db_query('SELECT s.nid AS nid FROM {scheduler} s LEFT JOIN {node} n ON s.nid = n.nid WHERE s.unpublish_on > 0 AND s.unpublish_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('unpublish')));
  foreach ($nids as $nid) {

    // If this node is to be unpublished, we can update the node and remove the
    // record since it cannot be republished.
    $n = node_load($nid);
    $old_change_date = $n->changed;
    $n->changed = $n->unpublish_on;
    if ($n->status == 1) {
      $create_unpublishing_revision = variable_get('scheduler_unpublish_revision_' . $n->type, 0) == 1;
      if ($create_unpublishing_revision) {
        $n->revision = TRUE;
        $n->log = "Node unpublished by scheduler module. Original change date was " . format_date($old_change_date, 'custom', $date_format) . ".";
      }

      // Use the actions system to unpublish the node.
      watchdog('scheduler', '@type: scheduled unpublishing of %title.', array(
        '@type' => $n->type,
        '%title' => $n->title,
      ), WATCHDOG_NOTICE, l(t('view'), 'node/' . $n->nid));
      $actions = array(
        'node_unpublish_action',
        'node_save_action',
      );
      $context['node'] = $n;
      actions_do($actions, $n, $context, NULL, NULL);
    }
    db_query('DELETE FROM {scheduler} WHERE nid = %d', $n->nid);

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