You are here

function workflow_admin_ui_overview_form_submit in Workflow 7

Submission handler for the state form.

File

workflow_admin_ui/workflow_admin_ui.pages.inc, line 848
Provides administrative UI for workflow. Why it's own module? Lower code footprint and better performance. Additional credit to gcassie ( http://drupal.org/user/80260 ) for the initial push to split UI out of core workflow. We're moving…

Code

function workflow_admin_ui_overview_form_submit($form, &$form_state) {

  // Get the workflow id, then save it for the next round.
  $workflow = $form_state['values']['workflow'];
  $wid = $workflow->wid;

  // Because the form elements were keyed with the item ids from the database,
  // we can simply iterate through the submitted values.
  foreach ($form_state['values']['states'] as $sid => $item) {
    $item['sid'] = $sid;
    $item['wid'] = $wid;

    // Is there not a new state name?
    if (empty($item['state'])) {

      // No new state entered, so skip it.
      continue;
    }

    // Reload $state from db, in case the states were changed by anyone else. And it is just a s fast.
    $state = $sid ? WorkflowState::load($sid) : WorkflowState::create($name = '', $wid);

    // Does user want to deactivate the state (reassign current nodes)?
    if ($sid > 0 && $item['status'] == 0 && $state
      ->isActive()) {
      if ($item['count'] > 0) {
        $new_sid = $item['reassign'];
        $new_state = WorkflowState::load($new_sid);
        $args = array(
          '%workflow' => $workflow
            ->getName(),
          '%old_state' => $state
            ->getName(),
          '%new_state' => isset($new_state) ? $new_state
            ->getName() : '',
        );
        if ($form['#last_mohican']) {
          $new_sid = NULL;

          // Do not reassign to new state.
          $message = 'Removing workflow states from content in the %workflow.';
          drupal_set_message(t($message, $args));
        }
        else {

          // Prepare the state delete function.
          $message = 'Reassigning content from %old_state to %new_state.';
          drupal_set_message(t($message, $args));
        }

        // Delete the old state without orphaning nodes, move them to the new state.
        $state
          ->deactivate($new_sid);
        $message = 'Deactivated workflow state %old_state in %workflow.';
        watchdog('workflow', $message, $args);
        drupal_set_message(t($message, $args));
      }
    }
    $state
      ->setName($item['state']);
    $state->status = $item['status'];
    $state->weight = $item['weight'];
    $state
      ->save();
  }
  $form_state['redirect'] = "admin/config/workflow/workflow/{$wid}";
}