You are here

function workflow_admin_ui_states_form in Workflow 7.2

Menu callback.

Creates the main workflow page, which gives an overview of workflows and workflow states. Replaced by http://drupal.org/node/1367530.

1 string reference to 'workflow_admin_ui_states_form'
EntityWorkflowUIController::hook_menu in workflow_admin_ui/includes/Entity/EntityWorkflowUIController.php
Provides definitions for implementing hook_menu().

File

workflow_admin_ui/workflow_admin_ui.page.states.inc, line 15
Provides an Admin UI page for the Workflow States.

Code

function workflow_admin_ui_states_form($form, &$form_state, $workflow, $op) {
  $form = array();
  $form['#tree'] = TRUE;
  $form['workflow'] = array(
    '#type' => 'value',
    '#value' => $workflow,
  );

  // Build select options for reassigning states.
  // We put a blank state first for validation.
  $state_list = array(
    '' => ' ',
  );
  $state_list += workflow_get_workflow_state_names($workflow->wid, $grouped = FALSE, $all = FALSE);

  // Is this the last state available?
  $form['#last_mohican'] = count($state_list) == 2;

  // Get the state objects as array ($sid => WorkflowState).
  $states = $workflow
    ->getStates($all = TRUE);

  // Create a dummy object for new state item. It must NOT be saved to DB.
  $maxweight = $minweight = -50;

  // $wid = $workflow->wid;
  $dummy = $workflow
    ->createState('', FALSE);
  $dummy->weight = $minweight;
  $states[$dummy->sid] = $dummy;

  // Although the index is 0, the state is appended at the end of the list.
  foreach ($states as $state) {

    // Allow modules to insert operations per state.
    $links = module_invoke_all('workflow_operations', 'state', $workflow, $state);
    $sid = $state->sid;
    $label = $state
      ->label();
    $count = $state
      ->count();

    // Make it impossible to reassign to the same state that is disabled.
    if ($state
      ->isCreationState() || !$sid || !$state
      ->isActive()) {
      $current_state = array();
      $current_state_list = array();
    }
    else {
      $current_state = array(
        $sid => $state_list[$sid],
      );
      $current_state_list = array_diff($state_list, $current_state);
    }
    $form['states'][$sid] = array(
      'state' => array(
        '#type' => 'textfield',
        '#size' => 30,
        '#maxlength' => 255,
        '#default_value' => $label,
      ),
      'name' => array(
        '#type' => 'machine_name',
        '#size' => 30,
        '#maxlength' => 255,
        '#required' => FALSE,
        // '#disabled' => !empty($name), // If needed this would disable updating machine name, once set.
        '#default_value' => $state
          ->getName(),
        '#machine_name' => array(
          'exists' => 'workflow_admin_ui_states_validate_state_machine_name',
          'source' => array(
            'states',
            $sid,
            'state',
          ),
          'replace_pattern' => '[^a-z0-9_()]+',
        ),
      ),
      'weight' => array(
        '#type' => 'weight',
        '#delta' => 20,
        '#default_value' => $state->weight,
        '#title-display' => 'invisible',
        '#attributes' => array(
          'class' => array(
            'state-weight',
          ),
        ),
      ),
      'status' => array(
        '#type' => 'checkbox',
        '#default_value' => $state
          ->isActive(),
      ),
      // Save the original status for the validation handler.
      'orig_status' => array(
        '#type' => 'value',
        '#value' => $state
          ->isActive(),
      ),
      'reassign' => array(
        '#type' => 'select',
        '#options' => $current_state_list,
      ),
      'count' => array(
        '#type' => 'value',
        '#value' => $count,
      ),
      'ops' => array(
        '#type' => 'markup',
        '#markup' => theme('links', array(
          'links' => $links,
        )),
      ),
    );

    // Don't let the creation state change weight or status or name.
    if ($state
      ->isCreationState()) {
      $form['states'][$sid]['weight']['#value'] = $minweight;
      $form['states'][$sid]['sysid']['#value'] = 1;
      $form['states'][$sid]['state']['#disabled'] = TRUE;
      $form['states'][$sid]['name']['#disabled'] = TRUE;
      $form['states'][$sid]['status']['#disabled'] = TRUE;
      $form['states'][$sid]['reassign']['#type'] = 'hidden';
      $form['states'][$sid]['reassign']['#disabled'] = TRUE;
    }

    // New state and disabled states cannot be reassigned.
    if (!$sid || !$state
      ->isActive() || $count == 0) {
      $form['states'][$sid]['reassign']['#type'] = 'hidden';
      $form['states'][$sid]['reassign']['#disabled'] = TRUE;
    }

    // Disabled states cannot be renamed (and is a visual clue, too.).
    if (!$state
      ->isActive()) {
      $form['states'][$sid]['state']['#disabled'] = TRUE;
    }

    // Set a proper weight to the new state.
    $maxweight = max($maxweight, $state->weight);
    if (!$sid) {
      $form['states'][$sid]['weight']['#default_value'] = $maxweight + 1;
    }
  }
  $form['actions'] = array(
    '#type' => 'actions',
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save Changes'),
  );
  return $form;
}