You are here

function workbench_scheduler_process_dates in Workbench Scheduler 7.2

Run schedules

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_dates()
_workbench_scheduler_run in ./workbench_scheduler.module
Process the different workbench scheduler schedules.

File

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

Code

function workbench_scheduler_process_dates($timestamp) {

  // Fetch all nodes that need to have their states changed to the 'end_date'.
  // State before NOW, and have not already done so.
  // Instantiate a counter for number of nodes processed.
  $schedule_query = db_select('workbench_scheduler_nodes', 'wsn');
  $schedule_query
    ->fields('wsn')
    ->condition('wsn.completed', 0)
    ->condition('wsn.date', 0, '!=')
    ->condition('wsn.date', time(), '<=');
  $schedule_query
    ->orderby('wsn.date', 'ASC');
  $result = $schedule_query
    ->execute();
  $scheduled_nodes = array();
  foreach ($result as $record) {
    $scheduled_nodes[] = $record;
  }

  // Only process active node schedules.
  _workbench_scheduler_process_node_schedules($scheduled_nodes);
  $count = 0;
  if (!empty($scheduled_nodes)) {

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

    // Loading transitions
    $transitions = workbench_moderation_transitions();

    // Loop through each scheduled node.
    foreach ($scheduled_nodes as $node_schedule) {
      if ($node_schedule->active) {

        // Getting current transition info.
        $transition = current(array_filter($transitions, function ($transition) use (&$node_schedule) {
          return $transition->id == $node_schedule->schedule->transition;
        }));
        $do_process = FALSE;

        // Need to load the node revision.
        $node = node_load($node_schedule->nid, $node_schedule->vid);
        if (!empty($node) && !empty($transition)) {

          // Make sure current transition is the starting transition.
          $transition_valid = $transition->from_name == $node->workbench_moderation['current']->state;
          if ($transition_valid) {
            $do_process = TRUE;

            // Allow modules to override the transition logic.
            // Note: modules can only prevent valid transitions from being processed;
            // they can't force invalid transitions.
            $override = module_invoke_all('workbench_scheduler_cron_transition', $node_schedule);
            if (in_array(FALSE, $override, TRUE)) {
              $do_process = FALSE;
            }
          }
          if ($do_process) {

            // Only moderate if state is not 'unpublish'.
            // If transition is not published  follow a different workflow.
            if ($transition->to_name != workbench_moderation_state_published() && $node->status == NODE_PUBLISHED) {
              workbench_scheduler_moderate_unpublish($node, $transition->to_name);
            }
            else {

              // Adding support for Workbench Moderation 7.x-3.x.
              if (function_exists('workbench_moderation_save')) {
                $node->workbench_moderation_state_new = $transition->to_name;
                $node->revision = TRUE;
                $node->is_current = TRUE;

                // Remove any schedules from new revision that are being executed.
                foreach ($node->workbench_schedule as $index => $revision_schedule) {
                  $revisions_match = $revision_schedule->vid == $node_schedule->vid;
                  $schedules_match = $revision_schedule->sid == $node_schedule->sid;
                  if ($revisions_match && $schedules_match) {
                    unset($node->workbench_schedule[$index]);
                  }
                }
                node_save($node);
              }
              else {
                workbench_moderation_moderate($node, $transition->to_name);
              }
            }

            // Mark schedule as completed.
            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_dates', $node_schedule);
          }
        }
      }
    }
  }

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