| 1 |
<?php
|
| 2 |
// $Id: workflow.pages.inc,v 1.1 2008/08/05 01:55:06 jvandyk Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Provide user interface for changing workflow state.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Menu callback. Display workflow summary of a node.
|
| 11 |
*/
|
| 12 |
function workflow_tab_page($node = NULL) {
|
| 13 |
drupal_set_title(check_plain($node->title));
|
| 14 |
$wid = workflow_get_workflow_for_type($node->type);
|
| 15 |
$states_per_page = variable_get('workflow_states_per_page', 20);
|
| 16 |
$result = db_query("SELECT sid, state FROM {workflow_states} WHERE status = 1 ORDER BY sid");
|
| 17 |
while ($data = db_fetch_object($result)) {
|
| 18 |
$states[$data->sid] = $data->state;
|
| 19 |
}
|
| 20 |
$deleted_states = array();
|
| 21 |
$result = db_query("SELECT sid, state FROM {workflow_states} WHERE status = 0 ORDER BY sid");
|
| 22 |
while ($data = db_fetch_object($result)) {
|
| 23 |
$deleted_states[$data->sid] = $data->state;
|
| 24 |
}
|
| 25 |
$current = workflow_node_current_state($node);
|
| 26 |
|
| 27 |
// theme_workflow_current_state() must run state through check_plain().
|
| 28 |
$output = '<p>'. t('Current state: !state', array('!state' => theme('workflow_current_state', $states[$current]))) . "</p>\n";
|
| 29 |
$output .= drupal_get_form('workflow_tab_form', $node, $wid, $states, $current);
|
| 30 |
|
| 31 |
$result = pager_query("SELECT h.*, u.name FROM {workflow_node_history} h LEFT JOIN {users} u ON h.uid = u.uid WHERE nid = %d ORDER BY hid DESC", $states_per_page, 0, NULL, $node->nid);
|
| 32 |
$rows = array();
|
| 33 |
while ($history = db_fetch_object($result)) {
|
| 34 |
if ($history->sid == $current && !isset($deleted_states[$history->sid]) && !isset($current_themed)) {
|
| 35 |
// Theme the current state differently so it stands out.
|
| 36 |
$state_name = theme('workflow_current_state', $states[$history->sid]);
|
| 37 |
// Make a note that we have themed the current state; other times in the history
|
| 38 |
// of this node where the node was in this state do not need to be specially themed.
|
| 39 |
$current_themed = TRUE;
|
| 40 |
}
|
| 41 |
elseif (isset($deleted_states[$history->sid])) {
|
| 42 |
// The state has been deleted, but we include it in the history.
|
| 43 |
$state_name = theme('workflow_deleted_state', $deleted_states[$history->sid]);
|
| 44 |
$footer_needed = TRUE;
|
| 45 |
}
|
| 46 |
else {
|
| 47 |
// Regular state.
|
| 48 |
$state_name = check_plain($states[$history->sid]);
|
| 49 |
}
|
| 50 |
|
| 51 |
if (isset($deleted_states[$history->old_sid])) {
|
| 52 |
$old_state_name = theme('workflow_deleted_state', $deleted_states[$history->old_sid]);
|
| 53 |
$footer_needed = TRUE;
|
| 54 |
}
|
| 55 |
else {
|
| 56 |
$old_state_name = check_plain($states[$history->old_sid]);
|
| 57 |
}
|
| 58 |
$rows[] = theme('workflow_history_table_row', $history, $old_state_name, $state_name);
|
| 59 |
}
|
| 60 |
$output .= theme('workflow_history_table', $rows, !empty($footer_needed));
|
| 61 |
$output .= theme('pager', $states_per_page);
|
| 62 |
return $output;
|
| 63 |
}
|
| 64 |
|
| 65 |
/*
|
| 66 |
* Theme one workflow history table row.
|
| 67 |
*/
|
| 68 |
function theme_workflow_history_table_row($history, $old_state_name, $state_name) {
|
| 69 |
return array(
|
| 70 |
format_date($history->stamp),
|
| 71 |
$old_state_name,
|
| 72 |
$state_name,
|
| 73 |
theme('username', $history),
|
| 74 |
filter_xss($history->comment, array('a', 'em', 'strong')),
|
| 75 |
);
|
| 76 |
}
|
| 77 |
|
| 78 |
/*
|
| 79 |
* Theme entire workflow history table.
|
| 80 |
*/
|
| 81 |
function theme_workflow_history_table($rows, $footer) {
|
| 82 |
$output = theme('table', array(t('Date'), t('Old State'), t('New State'), t('By'), t('Comment')), $rows, array('class' => 'workflow_history'), t('Workflow History'));
|
| 83 |
if ($footer) {
|
| 84 |
$output .= t('*State is no longer available.');
|
| 85 |
}
|
| 86 |
return $output;
|
| 87 |
}
|
| 88 |
|
| 89 |
/**
|
| 90 |
* Theme the current state in the workflow history table.
|
| 91 |
*/
|
| 92 |
function theme_workflow_current_state($state_name) {
|
| 93 |
return '<strong>'. check_plain($state_name) .'</strong>';
|
| 94 |
}
|
| 95 |
|
| 96 |
/**
|
| 97 |
* Theme a deleted state in the workflow history table.
|
| 98 |
*/
|
| 99 |
function theme_workflow_deleted_state($state_name) {
|
| 100 |
return check_plain($state_name) .'*';
|
| 101 |
}
|
| 102 |
|
| 103 |
/**
|
| 104 |
* Form builder. Allow workflow state change and scheduling from workflow tab.
|
| 105 |
*
|
| 106 |
* @param $node
|
| 107 |
* Node for which workflow information will be displayed.
|
| 108 |
* @param $wid
|
| 109 |
* ID of workflow to display.
|
| 110 |
* @param $states
|
| 111 |
* Array of states for the workflow.
|
| 112 |
* @param $current
|
| 113 |
* Current workflow state of this node.
|
| 114 |
* @return
|
| 115 |
* Form definition array.
|
| 116 |
*/
|
| 117 |
function workflow_tab_form($form_state, &$node, $wid, $states, $current) {
|
| 118 |
$form['#tab'] = TRUE;
|
| 119 |
$choices = workflow_field_choices($node);
|
| 120 |
|
| 121 |
$min = $states[$current] == t('(creation)') ? 1 : 2;
|
| 122 |
// Only build form if user has possible target state(s).
|
| 123 |
if (count($choices) >= $min) {
|
| 124 |
$wid = workflow_get_workflow_for_type($node->type);
|
| 125 |
$workflow = workflow_load($wid);
|
| 126 |
$form['#wf'] = $workflow;
|
| 127 |
$name = check_plain($workflow->name);
|
| 128 |
// See if scheduling information is present.
|
| 129 |
if ($node->_workflow_scheduled_timestamp && $node->_workflow_scheduled_sid) {
|
| 130 |
global $user;
|
| 131 |
if (variable_get('configurable_timezones', 1) && $user->uid && strlen($user->timezone)) {
|
| 132 |
$timezone = $user->timezone;
|
| 133 |
}
|
| 134 |
else {
|
| 135 |
$timezone = variable_get('date_default_timezone', 0);
|
| 136 |
}
|
| 137 |
// The default value should be the upcoming sid.
|
| 138 |
$current = $node->_workflow_scheduled_sid;
|
| 139 |
$timestamp = $node->_workflow_scheduled_timestamp;
|
| 140 |
$comment = $node->_workflow_scheduled_comment;
|
| 141 |
}
|
| 142 |
|
| 143 |
// Include the same form elements here that are included on a
|
| 144 |
// regular node editing page. $form is modified by reference.
|
| 145 |
workflow_node_form($form, $form_state, t('Change %s state', array('%s' => $name)), $name, $current, $choices, $timestamp, $comment);
|
| 146 |
|
| 147 |
$form['node'] = array(
|
| 148 |
'#type' => 'value',
|
| 149 |
'#value' => $node,
|
| 150 |
);
|
| 151 |
$form['submit'] = array(
|
| 152 |
'#type' => 'submit',
|
| 153 |
'#value' => t('Submit')
|
| 154 |
);
|
| 155 |
}
|
| 156 |
return $form;
|
| 157 |
}
|
| 158 |
|
| 159 |
/**
|
| 160 |
* Submit handler for the form on the workflow tab.
|
| 161 |
*
|
| 162 |
* @see workflow_tab_form
|
| 163 |
*/
|
| 164 |
function workflow_tab_form_submit($form, &$form_state) {
|
| 165 |
// The entire node object was stashed in the form.
|
| 166 |
$node = $form_state['values']['node'];
|
| 167 |
$node->workflow = $form_state['values']['workflow'];
|
| 168 |
$node->workflow_comment = $form_state['values']['workflow_comment'];
|
| 169 |
$node->workflow_scheduled = $form_state['values']['workflow_scheduled'];
|
| 170 |
$node->workflow_scheduled_date = $form_state['values']['workflow_scheduled_date'];
|
| 171 |
$node->workflow_scheduled_hour = $form_state['values']['workflow_scheduled_hour'];
|
| 172 |
|
| 173 |
// Call node_save() to make sure any handlers that use the
|
| 174 |
// new workflow values will see them.
|
| 175 |
node_save($node);
|
| 176 |
|
| 177 |
$form_state['redirect'] = 'node/' . $node->nid;
|
| 178 |
}
|