| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Defines a field type for referencing a state.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Implementation of hook_field_info().
|
| 11 |
*/
|
| 12 |
function statereference_field_info() {
|
| 13 |
return array(
|
| 14 |
'statereference' => array('label' => 'Workflow State Reference'),
|
| 15 |
);
|
| 16 |
}
|
| 17 |
|
| 18 |
/**
|
| 19 |
* Implementation of hook_field_settings().
|
| 20 |
*/
|
| 21 |
function statereference_field_settings($op, $field) {
|
| 22 |
switch ($op) {
|
| 23 |
case 'form':
|
| 24 |
$form = array();
|
| 25 |
return $form;
|
| 26 |
|
| 27 |
case 'save':
|
| 28 |
return array('referenceable_types');
|
| 29 |
|
| 30 |
case 'database columns':
|
| 31 |
$columns = array(
|
| 32 |
'sid' => array('type' => 'int', 'not null' => TRUE, 'default' => '0'),
|
| 33 |
);
|
| 34 |
return $columns;
|
| 35 |
}
|
| 36 |
}
|
| 37 |
|
| 38 |
/**
|
| 39 |
* Return an array of all states
|
| 40 |
*/
|
| 41 |
function _statereference_get_states() {
|
| 42 |
$states_list = array();
|
| 43 |
$states_list[-10] = 'Any';
|
| 44 |
|
| 45 |
$states_result = db_query('SELECT
|
| 46 |
ws.sid AS sid, w.name AS name, ws.state AS state
|
| 47 |
FROM
|
| 48 |
{workflows} AS w INNER JOIN {workflow_states} AS ws ON w.wid = ws.wid
|
| 49 |
ORDER BY
|
| 50 |
w.name, ws.state');
|
| 51 |
while ($obj = db_fetch_object($states_result)) {
|
| 52 |
if ($obj->state == '(creation)') {
|
| 53 |
$states_list[0] = $obj->name .':'. $obj->state;
|
| 54 |
}
|
| 55 |
else {
|
| 56 |
$states_list[$obj->sid] = $obj->name .':'. $obj->state;
|
| 57 |
}
|
| 58 |
}
|
| 59 |
return $states_list;
|
| 60 |
}
|
| 61 |
|
| 62 |
/**
|
| 63 |
* Implementation of hook_field().
|
| 64 |
*/
|
| 65 |
function statereference_field($op, &$state, $field, &$items, $teaser, $page) {
|
| 66 |
switch ($op) {
|
| 67 |
case 'view':
|
| 68 |
foreach ($items as $delta => $item) {
|
| 69 |
$items[$delta]['view'] = content_format($field, $item, 'default', $state);
|
| 70 |
}
|
| 71 |
return theme('field', $state, $field, $items, $teaser, $page);
|
| 72 |
}
|
| 73 |
}
|
| 74 |
|
| 75 |
/**
|
| 76 |
* Implementation of hook_field_formatter_info().
|
| 77 |
*/
|
| 78 |
function statereference_field_formatter_info() {
|
| 79 |
return array(
|
| 80 |
'default' => array(
|
| 81 |
'label' => 'Default',
|
| 82 |
'field types' => array('statereference'),
|
| 83 |
),
|
| 84 |
'plain' => array(
|
| 85 |
'label' => 'Plain text',
|
| 86 |
'field types' => array('statereference'),
|
| 87 |
),
|
| 88 |
);
|
| 89 |
}
|
| 90 |
|
| 91 |
/**
|
| 92 |
* Implementation of hook_field_formatter().
|
| 93 |
*/
|
| 94 |
function statereference_field_formatter($field, $item, $formatter, $state) {
|
| 95 |
$text = '';
|
| 96 |
if (isset($item['sid'])) {
|
| 97 |
$states = _statereference_get_states();
|
| 98 |
$referenced_state = $states[ $item['sid'] ];
|
| 99 |
if ($referenced_state) {
|
| 100 |
$text = $referenced_state;
|
| 101 |
}
|
| 102 |
}
|
| 103 |
|
| 104 |
switch ($formatter) {
|
| 105 |
case 'plain':
|
| 106 |
return strip_tags($text);
|
| 107 |
|
| 108 |
default:
|
| 109 |
return $text;
|
| 110 |
}
|
| 111 |
}
|
| 112 |
|
| 113 |
/**
|
| 114 |
* Implementation of hook_widget_info().
|
| 115 |
*/
|
| 116 |
function statereference_widget_info() {
|
| 117 |
return array(
|
| 118 |
'statereference_select' => array(
|
| 119 |
'label' => 'Select List',
|
| 120 |
'field types' => array('statereference'),
|
| 121 |
)
|
| 122 |
);
|
| 123 |
}
|
| 124 |
|
| 125 |
/**
|
| 126 |
* Implementation of hook_widget().
|
| 127 |
*/
|
| 128 |
function statereference_widget($op, &$state, $field, &$state_field) {
|
| 129 |
if ($field['widget']['type'] == 'statereference_select') {
|
| 130 |
switch ($op) {
|
| 131 |
case 'prepare form values':
|
| 132 |
$state_field_transposed = content_transpose_array_rows_cols($state_field);
|
| 133 |
$state_field['default sids'] = $state_field_transposed['sid'];
|
| 134 |
break;
|
| 135 |
|
| 136 |
case 'form':
|
| 137 |
$form = array();
|
| 138 |
|
| 139 |
$form[$field['field_name']] = array('#tree' => TRUE);
|
| 140 |
$form[$field['field_name']]['sids'] = array(
|
| 141 |
'#type' => 'select',
|
| 142 |
'#title' => t($field['widget']['label']),
|
| 143 |
'#default_value' => $state_field['default sids'],
|
| 144 |
'#multiple' => $field['multiple'],
|
| 145 |
'#options' => _statereference_get_states(),
|
| 146 |
'#required' => $field['required'],
|
| 147 |
'#description' => $field['widget']['description'],
|
| 148 |
);
|
| 149 |
|
| 150 |
return $form;
|
| 151 |
|
| 152 |
case 'process form values':
|
| 153 |
if ($field['multiple']) {
|
| 154 |
$state_field = content_transpose_array_rows_cols(array('sid' => $state_field['sids']));
|
| 155 |
}
|
| 156 |
else {
|
| 157 |
$state_field[0]['sid'] = $state_field['sids'];
|
| 158 |
}
|
| 159 |
// Remove the widget's data representation so it isn't saved.
|
| 160 |
unset($state_field['sids']);
|
| 161 |
}
|
| 162 |
}
|
| 163 |
}
|