You are here

function workbench_scheduler_process_start_dates in Workbench Scheduler 7

Run schedules for start times / states.

Parameters

int $timestamp: Timestamp to check against.

Return value

int|bool Count of nodes that were processed or boolean FALSE.

1 call to workbench_scheduler_process_start_dates()
_workbench_scheduler_run in ./workbench_scheduler.module
Process the different workbench scheduler schedules.

File

./workbench_scheduler.module, line 1157
Content scheduling for Workbench.

Code

function workbench_scheduler_process_start_dates($timestamp) {

  // Fetch all nodes that need to have their states changed to the 'start_date'.
  // State before NOW, and have not already done so.
  // Instantiate a counter for number of nodes processed.
  $count = 0;

  // Select from the workbench_scheduler_nodes table.
  $schedule_query = db_select('workbench_scheduler_nodes', 'wsn');

  // Join node table to get 'type'.
  $schedule_query
    ->innerjoin('node', 'node', 'node.nid = wsn.nid');

  // Inner join on workbench_scheduler_schedules table on schedule id.
  $schedule_query
    ->innerjoin('workbench_scheduler_schedules', 'wss', 'wsn.sid = wss.sid');

  // Inner join on workbench_moderation_node_history table on nid, revision id.
  // And where the current state does not equal the start state.
  $schedule_query
    ->innerjoin('workbench_moderation_node_history', 'wmnh', 'wsn.nid = wmnh.nid AND wsn.vid = wmnh.vid AND wss.start_state != wmnh.state');

  // Join the node history table and grab highest vid of the highest hid.
  $schedule_query
    ->leftJoin('workbench_moderation_node_history', 'wmnh2', 'wmnh.nid = wmnh2.nid AND wmnh.vid = wmnh2.vid AND wmnh.hid < wmnh2.hid');

  // Retrieve the nid, vid from the workbench_scheduler_nodes table.
  $schedule_query
    ->fields('wsn')
    ->fields('wss', array(
    'start_state',
  ))
    ->fields('node', array(
    'type',
  ))
    ->condition('wsn.completed', 0)
    ->condition('wsn.start_date', 0, '!=')
    ->condition('wsn.start_date', $timestamp, '<=');

  // Only grab vid with highest hid.
  $schedule_query
    ->isNull('wmnh2.hid');

  // Create an or condition.
  $db_or = db_or();

  // Where no end date.
  $db_or
    ->condition('wsn.end_date', 0);

  // Or end date is after now.
  $db_or
    ->condition('wsn.end_date', $timestamp, '>');

  // Add or condition to the query.
  $schedule_query
    ->condition($db_or);

  // Order by vid to return the highest vid of each node.
  $schedule_query
    ->orderBy('wmnh.vid', 'DESC');

  // Execute the query.
  $schedule_nodes = $schedule_query
    ->execute();

  // If scheduled nodes are returned from the query.
  if ($schedule_nodes
    ->rowCount()) {

    // Include workbench moderation.
    module_load_include('module', 'workbench_moderation');

    // Loop through each scheduled node.
    foreach ($schedule_nodes as $node_schedule) {
      $do_process = FALSE;
      $type_settings = variable_get('workbench_scheduler_' . $node_schedule->type, array());

      // Only process latest revision.
      if (in_array('workbench_scheduler_limit_current_revision', $type_settings)) {

        // Getting the latest revision.
        $rev_list = node_revision_list($node_schedule);
        $latest_vid = max(array_keys($rev_list));
        if ($node_schedule->vid == $latest_vid) {
          $do_process = TRUE;
        }
      }
      else {
        $do_process = TRUE;
      }

      // Load and process the node.
      if ($do_process && ($node = node_load($node_schedule->nid, $node_schedule->vid))) {

        // If moderation state is 'unpublished' follow a different workflow.
        if ($node_schedule->start_state == 'unpublished' && $node->status) {
          workbench_scheduler_moderate_unpublish($node);
        }
        else {
          workbench_moderation_moderate($node, $node_schedule->start_state);
        }

        // Does this schedule have an end date as well?
        if ($node_schedule->end_date) {
        }
        else {

          // No end state so mark schedule as complete.
          workbench_scheduler_node_set_complete($node_schedule->nid, $node_schedule->vid, $node_schedule->sid);
        }

        // Update count of run schedules.
        $count++;
        module_invoke_all('workbench_scheduler_post_process_start_dates', $node_schedule);
      }
    }
  }

  // Return int if nodes that were moderated, if none boolean FALSE.
  return $count ?: FALSE;
}