You are here

function workflow_tab_page in Workflow 7

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

Menu callback. Display workflow summary of a node.

1 string reference to 'workflow_tab_page'
workflow_menu in ./workflow.module
Implements hook_menu().

File

./workflow.pages.inc, line 12
Provide user interface for changing workflow state.

Code

function workflow_tab_page($node = NULL) {
  drupal_set_title($node->title);
  $entity = $node;
  $entity_type = 'node';

  // @todo: add support for other entity types.
  $entity_bundle = $node->type;
  $workflow = workflow_get_workflows_by_type($entity_bundle, $entity_type);
  $states = $workflow
    ->getStates();

  // Show the current state and the Workflow form to allow state changing.
  // (But only show form if we are not at the terminal state.)
  // Choose a different form, depending if this is a Workflow Node or a Workflow Field.
  // Probably this can be done in hook_forms(), but this is better to maintain.
  if ($workflow_item = $workflow
    ->getWorkflowItem()) {

    // This is a Workflow Field workflow.
    $current_sid = workflow_node_current_state($entity, $entity_type, $workflow_item
      ->getField());
    $field = $workflow_item
      ->getField();
    $instance = $workflow_item
      ->getInstance();
    $langcode = NULL;
    $items[0]['value'] = $current_sid;
    $display = array();
    $form = workflowfield_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display);
    $output = drupal_render($form);
  }
  else {

    // This is a Workflow Node workflow.
    $current_sid = workflow_node_current_state($entity, $entity_type, $field = array());
    $name = $states[$current_sid]
      ->getName();
    $label = $states[$current_sid]
      ->label();
    $output = theme('workflow_current_state', array(
      'state' => $label,
      'state_system_name' => $name,
      'sid' => $current_sid,
    ));
    $form = drupal_get_form('workflow_tab_form', $node, $workflow, $states, $current_sid);
    $output .= drupal_render($form);
  }

  // Show the history table.
  $rows = array();
  $current_themed = FALSE;
  foreach (workflow_get_workflow_node_history_by_nid($node->nid) as $history) {
    $old_state_name = $new_state_name = '';
    $label = $name = '';
    $history_state = isset($states[$history->sid]) ? $states[$history->sid] : 0;
    if ($history_state != NULL) {
      $name = $history_state
        ->getName();
      $label = $history_state
        ->label();
    }
    if (!$history_state) {

      // This is an invalid/deleted state.
      $old_state_name = check_plain($label);
    }
    elseif ($history->sid == $current_sid && $history_state
      ->isActive() && !$current_themed) {

      // Theme the current state differently so it stands out.
      $new_state_name = theme('workflow_history_current_state', array(
        'state_name' => $label,
        'state_system_name' => $name,
        'sid' => $history->sid,
      ));

      // Make a note that we have themed the current state; other times in the history
      // of this node where the node was in this state do not need to be specially themed.
      $current_themed = TRUE;
    }
    elseif (!$history_state
      ->isActive()) {

      // The state has been deleted, but we include it in the history.
      $new_state_name = theme('workflow_deleted_state', array(
        'state_name' => $label,
        'state_system_name' => $name,
        'sid' => $history->sid,
      ));
      $footer_needed = TRUE;
    }
    else {

      // Regular state.
      $new_state_name = check_plain($label);
    }
    $label = $name = MARK_STATE_IS_DELETED;
    $history_state = $states[$history->old_sid];
    if ($history_state != NULL) {
      $name = $history_state
        ->getName();
      $label = $history_state
        ->label();
    }
    if (!$history_state) {

      // This is an invalid/deleted state.
      $old_state_name = check_plain($label);
    }
    elseif (!$history_state
      ->isActive()) {
      $old_state_name = theme('workflow_deleted_state', array(
        'state_name' => $label,
        'state_system_name' => $name,
        'sid' => $history->old_sid,
      ));
      $footer_needed = TRUE;
    }
    else {

      // Regular state.
      $old_state_name = check_plain($label);
    }
    $variables = array(
      'transition' => $history,
      // @todo D8: pass this WorkflowTransition as only variable. It contains everything.
      'history' => $history,
      // @todo D8: remove, as this is the same as 'transition'.
      'old_sid' => $history->old_sid,
      // @todo D8: remove this redundant property.
      'sid' => $history->sid,
      // @todo D8: remove this redundant property.
      'uid' => $history->uid,
      // @todo D8: remove this redundant property.
      'old_state_name' => $old_state_name,
      'state_name' => $new_state_name,
    );

    // Allow other modules to modify the row.
    // $todo D8: pass only a $transition object.
    drupal_alter('workflow_history', $variables);
    $rows[] = theme('workflow_history_table_row', $variables);
  }

  // Mark the first and last rows.
  $rows[0]['class'][] = 'first';
  $last = count($rows) - 1;
  $rows[$last]['class'][] = 'last';

  // Only display the table if there's anything in it.
  if ($rows) {
    $output .= theme('workflow_history_table', array(
      'rows' => $rows,
      'footer' => !empty($footer_needed),
    ));
    $output .= theme('pager', array(
      'tags' => variable_get('workflow_states_per_page', 20),
    ));
  }
  return $output;
}