| 1 |
<?php
|
| 2 |
// $Id: workflow_named_transitions.module,v 1.4 2009/04/14 14:14:52 deekayen Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Modify the workflow form items so specific workflow transitions
|
| 7 |
* can have their own labels which the admin can describe relative
|
| 8 |
* to the beginning and ending states.
|
| 9 |
*/
|
| 10 |
|
| 11 |
/**
|
| 12 |
* Implementation of hook_menu().
|
| 13 |
*/
|
| 14 |
function workflow_named_transitions_menu() {
|
| 15 |
$items = array();
|
| 16 |
|
| 17 |
$items['admin/build/workflow/labels'] = array(
|
| 18 |
'title' => 'Edit labels',
|
| 19 |
'description' => 'Edit the labels on workflow transitions.',
|
| 20 |
'access callback' => 'user_access',
|
| 21 |
'access arguments' => array('administer workflow'),
|
| 22 |
'page callback' => 'workflow_named_transitions_edit_labels_page',
|
| 23 |
'type' => MENU_LOCAL_TASK
|
| 24 |
);
|
| 25 |
$items['admin/build/workflow/labels/%'] = array(
|
| 26 |
'title' => 'Edit labels',
|
| 27 |
'description' => '',
|
| 28 |
'access callback' => 'user_access',
|
| 29 |
'access arguments' => array('administer workflow'),
|
| 30 |
'page callback' => 'drupal_get_form',
|
| 31 |
'page arguments' => array('workflow_named_transitions_edit_labels_form', 4),
|
| 32 |
'type' => MENU_LOCAL_TASK
|
| 33 |
);
|
| 34 |
|
| 35 |
return $items;
|
| 36 |
}
|
| 37 |
|
| 38 |
/**
|
| 39 |
* Lists options of workflows to edit labels on.
|
| 40 |
*
|
| 41 |
* @return
|
| 42 |
* string of links to edit labels
|
| 43 |
*/
|
| 44 |
function workflow_named_transitions_edit_labels_page() {
|
| 45 |
$output = '';
|
| 46 |
$workflows = workflow_get_all();
|
| 47 |
|
| 48 |
foreach ($workflows as $workflow_id => $workflow_name) {
|
| 49 |
$output .= '<br />' . l(t("Edit workflow '@workflow_name' labels", array('@workflow_name' => $workflow_name)), "admin/build/workflow/labels/$workflow_id");
|
| 50 |
}
|
| 51 |
|
| 52 |
return $output;
|
| 53 |
}
|
| 54 |
|
| 55 |
/**
|
| 56 |
* Label edit form, where each fieldset represents a starting workflow state.
|
| 57 |
*
|
| 58 |
* Each contains the transitions with that starting workflow state.
|
| 59 |
*
|
| 60 |
* @return
|
| 61 |
* array of form items for editing labels on transitions
|
| 62 |
*/
|
| 63 |
function workflow_named_transitions_edit_labels_form($request, $workflow) {
|
| 64 |
if (!is_numeric($workflow)) {
|
| 65 |
drupal_set_message(t('Improper worklow ID provided.'), 'error');
|
| 66 |
watchdog('workflow_named_transitions', 'Improper worklow ID provided.');
|
| 67 |
drupal_goto('admin/build/workflow/labels');
|
| 68 |
}
|
| 69 |
|
| 70 |
$form = array();
|
| 71 |
|
| 72 |
$transitions = workflow_named_transitions_get_transitions($workflow);
|
| 73 |
|
| 74 |
foreach ($transitions as $transition) {
|
| 75 |
|
| 76 |
if (!isset($form[$transition['from_state']])) {
|
| 77 |
$form[$transition['from_state']] = array(
|
| 78 |
'#title' => $transition['from_state'],
|
| 79 |
'#type' => 'fieldset',
|
| 80 |
'#collapsible' => TRUE,
|
| 81 |
'#collapsed' => TRUE
|
| 82 |
);
|
| 83 |
}
|
| 84 |
|
| 85 |
$form[$transition['from_state']][$transition['tid']] = array(
|
| 86 |
'#title' => $transition['from_state'] . " → " . $transition['to_state'],
|
| 87 |
'#type' => 'textfield',
|
| 88 |
'#default_value' => $transition['label']
|
| 89 |
);
|
| 90 |
}
|
| 91 |
$form['submit'] = array(
|
| 92 |
'#type' => 'submit',
|
| 93 |
'#value' => 'Submit'
|
| 94 |
);
|
| 95 |
|
| 96 |
return $form;
|
| 97 |
}
|
| 98 |
|
| 99 |
/**
|
| 100 |
* Implementation of hook_theme().
|
| 101 |
*
|
| 102 |
* @return
|
| 103 |
* array to register the form to edit transition labels
|
| 104 |
*/
|
| 105 |
function workflow_named_transitions_theme() {
|
| 106 |
return array(
|
| 107 |
'workflow_named_transitions_edit_labels_form' => array(
|
| 108 |
'arguments' => array('form' => NULL)
|
| 109 |
)
|
| 110 |
);
|
| 111 |
}
|
| 112 |
|
| 113 |
/**
|
| 114 |
* Formats the label form into two column table rows.
|
| 115 |
*
|
| 116 |
* The transition is the first table cell, followed by the
|
| 117 |
* label field. Otherwise, the default display would be a
|
| 118 |
* form field with the transition as the FAPI title, and the
|
| 119 |
* form field as the content in a list format.
|
| 120 |
*
|
| 121 |
* @see workflow_named_transitions_edit_labels_form()
|
| 122 |
*/
|
| 123 |
function theme_workflow_named_transitions_edit_labels_form($form) {
|
| 124 |
$header = array();
|
| 125 |
|
| 126 |
// doing something with element_children() might be a better way
|
| 127 |
// than this foreach looping methods
|
| 128 |
foreach ($form as $starting_state => $transitions) {
|
| 129 |
if (isset($form[$starting_state]['#type']) && $form[$starting_state]['#type'] == 'fieldset') {
|
| 130 |
$rows = array();
|
| 131 |
foreach ($transitions as $tid => $item) {
|
| 132 |
if (is_numeric($tid)) {
|
| 133 |
// loops through starting transitions
|
| 134 |
$title = $item['#title'];
|
| 135 |
$form[$starting_state][$tid]['#title'] = '';
|
| 136 |
$rows[] = array(
|
| 137 |
array('data' => $title),
|
| 138 |
array('data' => drupal_render($form[$starting_state][$tid]))
|
| 139 |
);
|
| 140 |
unset($form[$starting_state][$tid]);
|
| 141 |
}
|
| 142 |
}
|
| 143 |
$form[$starting_state]['content'] = array(
|
| 144 |
'#value' => theme('table', $header, $rows)
|
| 145 |
);
|
| 146 |
}
|
| 147 |
}
|
| 148 |
return drupal_render($form);
|
| 149 |
}
|
| 150 |
|
| 151 |
/**
|
| 152 |
* Automatic submission handler for the edit labels form.
|
| 153 |
*
|
| 154 |
* @see workflow_named_transitions_edit_labels_form()
|
| 155 |
*/
|
| 156 |
function workflow_named_transitions_edit_labels_form_submit($form, &$form_state) {
|
| 157 |
foreach ($form_state['values'] as $tid => $label) {
|
| 158 |
if (!empty($tid) && $label != '') {
|
| 159 |
db_query("REPLACE INTO {workflow_named_transitions} (tid, label) VALUES (%d, '%s')", $tid, $label, $tid, $label);
|
| 160 |
}
|
| 161 |
else {
|
| 162 |
db_query("DELETE FROM {workflow_named_transitions} WHERE tid = %d", $tid);
|
| 163 |
}
|
| 164 |
}
|
| 165 |
}
|
| 166 |
|
| 167 |
/**
|
| 168 |
* Queries the database for an array of transition labels.
|
| 169 |
*
|
| 170 |
* IDs (tid), their starting and ending workflow states, and the label,
|
| 171 |
* if any, set by the admin for each transition.
|
| 172 |
*
|
| 173 |
* @return
|
| 174 |
* array of transitions tid, to_state, from_state, and label
|
| 175 |
*/
|
| 176 |
function workflow_named_transitions_get_transitions($workflow) {
|
| 177 |
$query = "SELECT wt.tid, from_states.state AS from_state, to_states.state AS to_state, wnt.label
|
| 178 |
FROM ({workflow_transitions} AS wt
|
| 179 |
INNER JOIN {workflow_states} AS from_states
|
| 180 |
ON wt.sid = from_states.sid
|
| 181 |
INNER JOIN {workflow_states} AS to_states
|
| 182 |
ON wt.target_sid = to_states.sid)
|
| 183 |
LEFT JOIN {workflow_named_transitions} AS wnt
|
| 184 |
ON wt.tid = wnt.tid "
|
| 185 |
. ($workflow != NULL ? "WHERE from_states.wid = $workflow AND to_states.wid = $workflow " : "") . " ORDER BY from_state, to_state";
|
| 186 |
$transition_results = db_query($query);
|
| 187 |
|
| 188 |
$transitions = array();
|
| 189 |
while ($transition = db_fetch_array($transition_results)) {
|
| 190 |
$transitions[$transition['tid']] = $transition;
|
| 191 |
}
|
| 192 |
return $transitions;
|
| 193 |
}
|
| 194 |
|
| 195 |
/**
|
| 196 |
* Implementation of hook_form_alter().
|
| 197 |
*
|
| 198 |
* Modifies the generic state labels that the workflow module added to the form.
|
| 199 |
*/
|
| 200 |
function workflow_named_transitions_form_alter(&$form, $form_state, $form_id) {
|
| 201 |
|
| 202 |
if (($form_id == 'workflow_tab_form' || $form['#id'] == 'node-form') && (isset($form['workflow']) && is_array($form['workflow']))) {
|
| 203 |
// if no nid, use sysid == 1 from workflow_states for each wid
|
| 204 |
// as the starting sid to lookup in workflow_transitions
|
| 205 |
if ($form_id == 'workflow_tab_form') {
|
| 206 |
$nid = $form['node']['#value']->nid;
|
| 207 |
}
|
| 208 |
else {
|
| 209 |
$nid = empty($form['nid']['#value']) ? 0 : $form['nid']['#value'];
|
| 210 |
}
|
| 211 |
// workflow ID, name of the workflow, and the creation state ID
|
| 212 |
if ($nid == 0) {
|
| 213 |
$workflows_result = db_query("SELECT w.wid, w.name, ws.sid AS starting_sid FROM {workflows} AS w LEFT JOIN {workflow_states} AS ws ON w.wid = ws.wid WHERE ws.sysid = 1");
|
| 214 |
}
|
| 215 |
else {
|
| 216 |
$workflows_result = db_query("SELECT w.wid, w.name, MAX(wn.sid) AS starting_sid FROM {workflows} AS w LEFT JOIN {workflow_states} AS ws on w.wid = ws.wid LEFT JOIN {workflow_node} AS wn ON ws.sid = wn.sid WHERE wn.nid = %d or wn.nid IS NULL GROUP BY w.wid", $nid);
|
| 217 |
}
|
| 218 |
|
| 219 |
while ($workflow = db_fetch_object($workflows_result)) {
|
| 220 |
if (!empty($form['workflow'][$workflow->name]['#options'])) {
|
| 221 |
// if a workflow was added to a content type after this node was created,
|
| 222 |
// it will have a NULL current state ID for the newly added workflow,
|
| 223 |
// so go back and get the default creation state ID
|
| 224 |
$workflow->starting_sid = empty($workflow->starting_sid) ? _workflow_named_transitions_creation_sid($workflow->wid) : $workflow->starting_sid;
|
| 225 |
|
| 226 |
$tids_result = db_query("SELECT wt.target_sid, wnt.label FROM {workflow_named_transitions} AS wnt INNER JOIN {workflow_transitions} AS wt ON wt.tid = wnt.tid WHERE wnt.tid IN(
|
| 227 |
SELECT sub_wt.tid FROM {workflow_transitions} AS sub_wt WHERE sub_wt.sid = %d AND sub_wt.target_sid IN(%s)
|
| 228 |
)", $workflow->starting_sid, implode(', ', array_keys($form['workflow'][$workflow->name]['#options'])));
|
| 229 |
while ($options_to_update = db_fetch_object($tids_result)) {
|
| 230 |
$form['workflow'][$workflow->name]['#options'][$options_to_update->target_sid] = $options_to_update->label;
|
| 231 |
}
|
| 232 |
}
|
| 233 |
}
|
| 234 |
}
|
| 235 |
}
|
| 236 |
|
| 237 |
/**
|
| 238 |
* Find the creation state id for a workflow.
|
| 239 |
*/
|
| 240 |
function _workflow_named_transitions_creation_sid($wid) {
|
| 241 |
return db_result(db_query_range("SELECT sid FROM {workflow_states} WHERE wid = %d AND sysid = 1", $wid, 0, 1));
|
| 242 |
}
|