You are here

function workflow_execute_transition in Workflow 5

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

Execute a transition (change state of a node).

Parameters

object $node:

int $sid:

Return value

int ID of new state.

3 calls to workflow_execute_transition()
action_workflow_execute_transition in ./workflow.module
Implementation of a Drupal action. Changes the workflow state of a node to the next state of the workflow.
workflow_cron in ./workflow.module
Implementation of hook_cron
workflow_nodeapi in ./workflow.module
Implementation of hook_nodeapi(). Summary of nodeapi ops we can see (Drupal 4.7):

File

./workflow.module, line 435

Code

function workflow_execute_transition($node, $sid, $comment = NULL) {
  $old_sid = workflow_node_current_state($node);
  if ($old_sid == $sid) {

    // stop if not going to a different state
    // Write comment into history though.
    if ($comment && !$node->_workflow_scheduled_comment) {
      $node->workflow_stamp = time();
      db_query("UPDATE {workflow_node} SET stamp = %d WHERE nid = %d", $node->workflow_stamp, $node->nid);
      $result = module_invoke_all('workflow', 'transition pre', $old_sid, $sid, $node);
      _workflow_write_history($node, $sid, $comment);
    }
    $result = module_invoke_all('workflow', 'transition post', $old_sid, $sid, $node);
    return;
  }

  // Make sure this transition is valid and allowed for the current user.
  global $user;
  if ($user->uid > 1 || $user->uid == 0) {

    // allow any state change for superuser (might be cron)
    $tid = workflow_get_transition_id($old_sid, $sid);
    if (!$tid) {
      watchdog('workflow', t('Attempt to go to nonexistent transition (from %old to %new)', array(
        '%old' => $old_sid,
        '%new' => $sid,
      ), WATCHDOG_ERROR));
      return;
    }
    if (!workflow_transition_allowed($tid, array_merge(array_keys($user->roles), array(
      'author',
    )))) {
      watchdog('workflow', t('User %user not allowed to go from state %old to %new)', array(
        '%user' => $user->name,
        '%old' => $old_sid,
        '%new' => $sid,
      ), WATCHDOG_NOTICE));
      return;
    }
  }

  // Invoke a callback indicating a transition is about to occur. Modules
  // may veto the transition by returning FALSE.
  $result = module_invoke_all('workflow', 'transition pre', $old_sid, $sid, $node);
  if (in_array(FALSE, $result)) {

    // stop if a module says so
    return;
  }
  _workflow_node_to_state($node, $sid, $comment);

  // change the state
  // Register state change with watchdog
  $state_name = db_result(db_query("SELECT state FROM {workflow_states} WHERE sid = %d", $sid));
  $type = node_get_types('name', $node->type);
  watchdog('workflow', t('State of @type %node_title set to @state_name', array(
    '@type' => $type,
    '%node_title' => $node->title,
    '@state_name' => $state_name,
  )), WATCHDOG_NOTICE, l('view', 'node/' . $node->nid));

  // Notify modules that transition has occurred. Actions should take place
  // in response to this callback, not the previous one.
  module_invoke_all('workflow', 'transition post', $old_sid, $sid, $node);

  // clear any references in the scheduled listing
  db_query('DELETE FROM {workflow_scheduled_transition} WHERE nid = %d', $node->nid);
}