You are here

function workflow_allowable_transitions in Workflow 7

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

Get allowable transitions for a given workflow state. Typical use:

global $user; $possible = workflow_allowable_transitions($sid, 'to', $user->roles);

If the state ID corresponded to the state named "Draft", $possible now contains the states that the current user may move to from the Draft state.

Parameters

$sid: The ID of the state in question.

$dir: The direction of the transition: 'to' or 'from' the state denoted by $sid. When set to 'to' all the allowable states that may be moved to are returned; when set to 'from' all the allowable states that may move to the current state are returned.

mixed $roles: Array of ints (and possibly the string 'author') representing the user's roles. If the string 'ALL' is passed (instead of an array) the role constraint is ignored (this is the default for backwards compatibility).

Return value

Associative array of states ($sid => $state_name pairs), excluding current state.

3 calls to workflow_allowable_transitions()
Workflow::validate in includes/Entity/Workflow.php
Validate the workflow. Generate a message if not correct. This function is used on the settings page of
WorkflowState::getOptions in includes/Entity/WorkflowState.php
Returns the allowed values for the current state.
workflow_field_choices in ./workflow.node.inc
Get the states current user can move to for a given node.

File

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

Code

function workflow_allowable_transitions($sid, $dir = 'to', $roles = 'ALL') {
  $transitions = array();

  // Main query from transitions table.
  $query = db_select('workflow_transitions', 't')
    ->fields('t', array(
    'tid',
  ));
  if ($dir == 'to') {
    $query
      ->innerJoin('workflow_states', 's', 's.sid = t.target_sid');
    $query
      ->addField('t', 'target_sid', 'state_id');
    $query
      ->condition('t.sid', $sid);
  }
  else {
    $query
      ->innerJoin('workflow_states', 's', 's.sid = t.sid');
    $query
      ->addField('t', 'sid', 'state_id');
    $query
      ->condition('t.target_sid', $sid);
  }
  $query
    ->addField('s', 'state', 'state_name');
  $query
    ->addField('s', 'weight', 'state_weight');
  $query
    ->addField('s', 'sysid');
  $query
    ->condition('s.status', 1);

  // Now let's get the current state.
  $query2 = db_select('workflow_states', 's');
  $query2
    ->addField('s', 'sid', 'tid');
  $query2
    ->addField('s', 'sid', 'state_id');
  $query2
    ->addField('s', 'state', 'state_name');
  $query2
    ->addField('s', 'weight', 'state_weight');
  $query2
    ->addField('s', 'sysid');
  $query2
    ->condition('s.status', 1);
  $query2
    ->condition('s.sid', $sid);
  $query2
    ->orderBy('state_weight');
  $query2
    ->orderBy('state_id');

  // Add the union of the two queries
  $query
    ->union($query2, 'UNION');
  $results = $query
    ->execute();
  foreach ($results as $transition) {
    if ($roles == 'ALL' || $sid == $transition->state_id || workflow_transition_allowed($transition->tid, $roles)) {
      $transitions[] = $transition;
    }
  }
  return $transitions;
}