You are here

function workflow_cron in Workflow 7.2

Same name and namespace in other branches
  1. 8 workflow.module \workflow_cron()
  2. 5.2 workflow.module \workflow_cron()
  3. 5 workflow.module \workflow_cron()
  4. 6.2 workflow.module \workflow_cron()
  5. 6 workflow.module \workflow_cron()
  6. 7 workflow.module \workflow_cron()

Implements hook_cron().

File

./workflow.module, line 346
Support workflows made up of arbitrary states.

Code

function workflow_cron() {
  $clear_cache = FALSE;

  // If the time now is greater than the time to execute a transition, do it.
  foreach (WorkflowScheduledTransition::loadBetween(0, REQUEST_TIME) as $scheduled_transition) {

    /* @var $scheduled_transition WorkflowScheduledTransition */
    $entity_type = $scheduled_transition->entity_type;
    $entity = $scheduled_transition
      ->getEntity();
    $field_name = $scheduled_transition->field_name;

    // If user didn't give a comment, create one.
    if (empty($scheduled_transition->comment)) {
      $scheduled_transition
        ->addDefaultComment();
    }
    $current_sid = workflow_node_current_state($entity, $entity_type, $field_name);

    // Make sure transition is still valid: the node must still be in the state
    // it was in, when the transition was scheduled.
    if ($current_sid == $scheduled_transition->old_sid) {

      // Do transition. Force it because user who scheduled was checked.
      // The scheduled transition is not scheduled anymore, and is also deleted from DB.
      // A watchdog message is created with the result.
      $scheduled_transition
        ->schedule(FALSE);
      workflow_execute_transition($entity_type, $entity, $field_name, $scheduled_transition, TRUE);
      if (!$field_name) {
        $clear_cache = TRUE;
      }
    }
    else {

      // Node is not in the same state it was when the transition
      // was scheduled. Defer to the node's current state and
      // abandon the scheduled transition.
      $scheduled_transition
        ->delete();
    }
  }
  if ($clear_cache) {

    // Clear the cache so that if the transition resulted in a node
    // being published, the anonymous user can see it.
    cache_clear_all();
  }
}