You are here

public function WorkflowTransitionForm::getTransition in Workflow 7.2

Extract WorkflowTransition or WorkflowScheduledTransition from the form.

This merely extracts the transition from the form/widget. No validation.

Parameters

$old_sid:

array $items:

$field_name:

\stdClass $user:

Return value

\WorkflowScheduledTransition|\WorkflowTransition|null

1 call to WorkflowTransitionForm::getTransition()
WorkflowTransitionForm::submitForm in includes/Form/WorkflowTransitionForm.php

File

includes/Form/WorkflowTransitionForm.php, line 674
Contains \workflow\Form\WorkflowTransitionForm.

Class

WorkflowTransitionForm
Provides a Transition Form to be used in the Workflow Widget.

Code

public function getTransition($old_sid, array $items, $field_name, stdClass $user, array &$form = array(), array &$form_state = array()) {
  $entity_type = $this->entity_type;
  $entity = $this->entity;

  // $entity_id = entity_id($entity_type, $entity);
  $field_name = !empty($this->field) ? $this->field['field_name'] : '';
  if (isset($items[0]['transition'])) {

    // a complete transition was already passed on.
    $transition = $items[0]['transition'];
  }
  else {

    // Get the new Transition properties. First the new State ID.
    if (isset($items[0]['workflow']['workflow_sid'])) {

      // We have shown a workflow form.
      $new_sid = $items[0]['workflow']['workflow_sid'];
    }
    elseif (isset($items[0]['value'])) {

      // We have shown a core options widget (radios, select).
      $new_sid = $items[0]['value'];
    }
    else {

      // This may happen if only 1 option is left, and a formatter is shown.
      $state = workflow_state_load_single($old_sid);
      if (!$state
        ->isCreationState()) {
        $new_sid = $old_sid;
      }
      else {

        // This only happens on workflows, when only one transition from
        // '(creation)' to another state is allowed.

        /* @var $workflow Workflow */
        $workflow = $state
          ->getWorkflow();
        $new_sid = $workflow
          ->getFirstSid($this->entity_type, $this->entity, $field_name, $user, FALSE);
      }
    }

    // If an existing Transition has been edited, $hid is set.
    $hid = isset($items[0]['workflow']['workflow_hid']) ? $items[0]['workflow']['workflow_hid'] : '';

    // Get the comment.
    $comment = isset($items[0]['workflow']['workflow_comment']) ? $items[0]['workflow']['workflow_comment'] : '';

    // Remember, the workflow_scheduled element is not set on 'add' page.
    $scheduled = !empty($items[0]['workflow']['workflow_scheduling']['scheduled']);
    if ($hid) {

      // We are editing an existing transition. Only comment may be changed.
      $transition = workflow_transition_load($hid);
      $transition->comment = $comment;
    }
    elseif (!$scheduled) {
      $transition = new WorkflowTransition();
      $transition
        ->setValues($entity_type, $entity, $field_name, $old_sid, $new_sid, $user->uid, REQUEST_TIME, $comment);
    }
    else {

      // Schedule the time to change the state.
      // If Field Form is used, use plain values;
      // If Node Form is used, use fieldset 'date_time'.
      $schedule = isset($items[0]['workflow']['workflow_scheduling']['date_time']) ? $items[0]['workflow']['workflow_scheduling']['date_time'] : $items[0]['workflow'];
      if (!isset($schedule['workflow_scheduled_hour'])) {
        $schedule['workflow_scheduled_hour'] = '00:00';
      }
      $scheduled_date_time = $schedule['workflow_scheduled_date']['year'] . substr('0' . $schedule['workflow_scheduled_date']['month'], -2, 2) . substr('0' . $schedule['workflow_scheduled_date']['day'], -2, 2) . ' ' . $schedule['workflow_scheduled_hour'] . ' ' . $schedule['workflow_scheduled_timezone'];
      if ($timestamp = strtotime($scheduled_date_time)) {
        $transition = new WorkflowScheduledTransition();
        $transition
          ->setValues($entity_type, $entity, $field_name, $old_sid, $new_sid, $user->uid, $timestamp, $comment);
      }
      else {
        $transition = NULL;
      }
    }
  }
  return $transition;
}