You are here

function scheduler_node_presave in Scheduler 7

Same name and namespace in other branches
  1. 8 scheduler.module \scheduler_node_presave()

Implements hook_node_presave().

4 calls to scheduler_node_presave()
scheduler_remove_publish_date_action in ./scheduler.rules.inc
Remove the publish_on date for the node.
scheduler_remove_unpublish_date_action in ./scheduler.rules.inc
Remove the unpublish_on date for the node.
scheduler_set_publish_date_action in ./scheduler.rules.inc
Set the publish_on date for the node.
scheduler_set_unpublish_date_action in ./scheduler.rules.inc
Set the unpublish_on date for the node.

File

./scheduler.module, line 566
Scheduler publishes and unpublishes nodes on dates specified by the user.

Code

function scheduler_node_presave($node) {
  foreach (array(
    'publish_on',
    'unpublish_on',
  ) as $key) {
    if (empty($node->{$key}) || is_array($node->{$key})) {

      // Make sure publish_on and unpublish_on are not empty strings.
      $node->{$key} = 0;
    }
    elseif (!is_numeric($node->{$key})) {

      // Convert to unix timestamp, but ensure any failure is converted to zero.
      $node->{$key} = _scheduler_strtotime($node->{$key}) + 0;
    }
  }
  if ($node->publish_on > 0) {

    // Check that other modules allow the action on this node.
    $publication_allowed = _scheduler_allow($node, 'publish');

    // Publish the node immediately if the publication date is in the past.
    $publish_immediately = variable_get('scheduler_publish_past_date_' . $node->type, 'error') == 'publish';
    if ($publication_allowed && $publish_immediately && $node->publish_on <= REQUEST_TIME) {

      // If required, set the created date to match published date.
      if (variable_get('scheduler_publish_touch_' . $node->type, 0) == 1) {
        $node->created = $node->publish_on;
      }
      $node->publish_on = 0;
      $node->status = 1;

      // Allow modules to react to immediate publishing.
      _scheduler_scheduler_api($node, 'publish_immediately');
    }
    else {

      // Ensure the node is unpublished as it will be published by cron later.
      $node->status = 0;

      // Only inform the user that the node is scheduled if publication has not
      // been prevented by other modules. Those modules have to display a
      // message themselves explaining why publication is denied.
      if ($publication_allowed) {
        $date_format = variable_get('scheduler_date_format', SCHEDULER_DATE_FORMAT);
        drupal_set_message(t('This post is unpublished and will be published @publish_time.', array(
          '@publish_time' => format_date($node->publish_on, 'custom', $date_format),
        )), 'status', FALSE);
      }
    }
  }
}