1 | <?php |
---|
2 | // $Id: workflow.pages.inc,v 1.2.2.3 2010/02/16 03:44:19 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] = check_plain(t($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] = check_plain(t($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(t($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(t($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 | * $old_state_name and $state_name must be run through check_plain(t()) prior |
---|
69 | * to calling this theme function. |
---|
70 | */ |
---|
71 | function theme_workflow_history_table_row($history, $old_state_name, $state_name) { |
---|
72 | return array( |
---|
73 | format_date($history->stamp), |
---|
74 | $old_state_name, |
---|
75 | $state_name, |
---|
76 | theme('username', $history), |
---|
77 | filter_xss($history->comment, array('a', 'em', 'strong')), |
---|
78 | ); |
---|
79 | } |
---|
80 | |
---|
81 | /* |
---|
82 | * Theme entire workflow history table. |
---|
83 | */ |
---|
84 | function theme_workflow_history_table($rows, $footer) { |
---|
85 | $output = theme('table', array(t('Date'), t('Old State'), t('New State'), t('By'), t('Comment')), $rows, array('class' => 'workflow_history'), t('Workflow History')); |
---|
86 | if ($footer) { |
---|
87 | $output .= t('*State is no longer available.'); |
---|
88 | } |
---|
89 | return $output; |
---|
90 | } |
---|
91 | |
---|
92 | /** |
---|
93 | * Theme the current state in the workflow history table. |
---|
94 | */ |
---|
95 | function theme_workflow_current_state($state_name) { |
---|
96 | return '<strong>'. check_plain(t($state_name)) .'</strong>'; |
---|
97 | } |
---|
98 | |
---|
99 | /** |
---|
100 | * Theme a deleted state in the workflow history table. |
---|
101 | */ |
---|
102 | function theme_workflow_deleted_state($state_name) { |
---|
103 | return check_plain(t($state_name)) .'*'; |
---|
104 | } |
---|
105 | |
---|
106 | /** |
---|
107 | * Form builder. Allow workflow state change and scheduling from workflow tab. |
---|
108 | * |
---|
109 | * @param $node |
---|
110 | * Node for which workflow information will be displayed. |
---|
111 | * @param $wid |
---|
112 | * ID of workflow to display. |
---|
113 | * @param $states |
---|
114 | * Array of states for the workflow. |
---|
115 | * @param $current |
---|
116 | * Current workflow state of this node. |
---|
117 | * @return |
---|
118 | * Form definition array. |
---|
119 | */ |
---|
120 | function workflow_tab_form($form_state, $node, $wid, $states, $current) { |
---|
121 | $form['#tab'] = TRUE; |
---|
122 | $choices = workflow_field_choices($node); |
---|
123 | |
---|
124 | $min = $states[$current] == t('(creation)') ? 1 : 2; |
---|
125 | // Only build form if user has possible target state(s). |
---|
126 | if (count($choices) >= $min) { |
---|
127 | $wid = workflow_get_workflow_for_type($node->type); |
---|
128 | $workflow = workflow_load($wid); |
---|
129 | $form['#wf'] = $workflow; |
---|
130 | $name = check_plain((t($workflow->name))); |
---|
131 | // See if scheduling information is present. |
---|
132 | if ($node->_workflow_scheduled_timestamp && $node->_workflow_scheduled_sid) { |
---|
133 | global $user; |
---|
134 | if (variable_get('configurable_timezones', 1) && $user->uid && strlen($user->timezone)) { |
---|
135 | $timezone = $user->timezone; |
---|
136 | } |
---|
137 | else { |
---|
138 | $timezone = variable_get('date_default_timezone', 0); |
---|
139 | } |
---|
140 | // The default value should be the upcoming sid. |
---|
141 | $current = $node->_workflow_scheduled_sid; |
---|
142 | $timestamp = $node->_workflow_scheduled_timestamp; |
---|
143 | $comment = $node->_workflow_scheduled_comment; |
---|
144 | } |
---|
145 | |
---|
146 | // Include the same form elements here that are included on a |
---|
147 | // regular node editing page. $form is modified by reference. |
---|
148 | workflow_node_form($form, $form_state, t('Change %s state', array('%s' => $name)), $name, $current, $choices, $timestamp, $comment); |
---|
149 | |
---|
150 | $form['node'] = array( |
---|
151 | '#type' => 'value', |
---|
152 | '#value' => $node, |
---|
153 | ); |
---|
154 | $form['submit'] = array( |
---|
155 | '#type' => 'submit', |
---|
156 | '#value' => t('Submit') |
---|
157 | ); |
---|
158 | } |
---|
159 | return $form; |
---|
160 | } |
---|
161 | |
---|
162 | /** |
---|
163 | * Submit handler for the form on the workflow tab. |
---|
164 | * |
---|
165 | * @see workflow_tab_form |
---|
166 | */ |
---|
167 | function workflow_tab_form_submit($form, &$form_state) { |
---|
168 | // The entire node object was stashed in the form. |
---|
169 | $node = $form_state['values']['node']; |
---|
170 | $node->workflow = $form_state['values']['workflow']; |
---|
171 | $node->workflow_comment = $form_state['values']['workflow_comment']; |
---|
172 | $node->workflow_scheduled = $form_state['values']['workflow_scheduled']; |
---|
173 | $node->workflow_scheduled_date = $form_state['values']['workflow_scheduled_date']; |
---|
174 | $node->workflow_scheduled_hour = $form_state['values']['workflow_scheduled_hour']; |
---|
175 | |
---|
176 | // Call node_save() to make sure any handlers that use the |
---|
177 | // new workflow values will see them. |
---|
178 | node_save($node); |
---|
179 | |
---|
180 | $form_state['redirect'] = 'node/' . $node->nid; |
---|
181 | } |
---|