| 1 |
<?php
|
| 2 |
// $Id: wizard.module,v 1.2 2007/01/16 03:57:19 dfletcher Exp $
|
| 3 |
|
| 4 |
|
| 5 |
/**
|
| 6 |
* Declare the wizard field type.
|
| 7 |
*/
|
| 8 |
function wizard_field_info() {
|
| 9 |
return array(
|
| 10 |
'wizard' => array('label' => t('Wizard')),
|
| 11 |
);
|
| 12 |
}
|
| 13 |
|
| 14 |
/**
|
| 15 |
* Header and footer settings.
|
| 16 |
*/
|
| 17 |
function wizard_field_settings($op, $field) {
|
| 18 |
switch ($op) {
|
| 19 |
case 'form':
|
| 20 |
$form = array();
|
| 21 |
$form['header'] = array(
|
| 22 |
'#type' => 'textarea',
|
| 23 |
'#title' => t('Page header'),
|
| 24 |
'#default_value' => $field['header'] ? $field['header'] : '',
|
| 25 |
'#required' => FALSE,
|
| 26 |
'#description' => t('A header for the wizard page.'),
|
| 27 |
'#rows' => 6,
|
| 28 |
);
|
| 29 |
$form['footer'] = array(
|
| 30 |
'#type' => 'textarea',
|
| 31 |
'#title' => t('Page footer'),
|
| 32 |
'#default_value' => $field['footer'] ? $field['footer'] : '',
|
| 33 |
'#required' => FALSE,
|
| 34 |
'#description' => t('A footer for the wizard page.'),
|
| 35 |
'#rows' => 6,
|
| 36 |
);
|
| 37 |
|
| 38 |
return $form;
|
| 39 |
|
| 40 |
case 'save':
|
| 41 |
return array('header', 'footer');
|
| 42 |
}
|
| 43 |
}
|
| 44 |
|
| 45 |
/**
|
| 46 |
* Wizard widget info.
|
| 47 |
*/
|
| 48 |
function wizard_widget_info() {
|
| 49 |
return array(
|
| 50 |
'wizard' => array(
|
| 51 |
'label' => 'Wizard Page',
|
| 52 |
'field types' => array('wizard'),
|
| 53 |
),
|
| 54 |
);
|
| 55 |
}
|
| 56 |
|
| 57 |
/**
|
| 58 |
* Wizard widget, a hidden form field.
|
| 59 |
*/
|
| 60 |
function wizard_widget($op, &$node, $field, &$node_field) {
|
| 61 |
switch ($op) {
|
| 62 |
case 'form':
|
| 63 |
$form = array();
|
| 64 |
$form[$field['field_name']]['wizard'] = array(
|
| 65 |
'#type' => 'hidden',
|
| 66 |
'#default_value' => 'wizardpage',
|
| 67 |
);
|
| 68 |
return $form;
|
| 69 |
break;
|
| 70 |
}
|
| 71 |
}
|
| 72 |
|
| 73 |
/**
|
| 74 |
* Count the number of pages in a wizard form. If the result is zero, this is
|
| 75 |
* not a wizard form.
|
| 76 |
*/
|
| 77 |
function _wizard_count_pages(&$form) {
|
| 78 |
$count = 0;
|
| 79 |
foreach ($form as &$element) {
|
| 80 |
if (!is_array($element)) continue;
|
| 81 |
if (isset($element['wizard'])) $count++;
|
| 82 |
}
|
| 83 |
return $count;
|
| 84 |
}
|
| 85 |
|
| 86 |
/**
|
| 87 |
* Wizard callback.
|
| 88 |
*/
|
| 89 |
function wizard_menu($may_cache) {
|
| 90 |
$items = array();
|
| 91 |
if (!$may_cache) {
|
| 92 |
$access = FALSE;
|
| 93 |
$menupath = FALSE;
|
| 94 |
if(isset($_REQUEST['edit']['target'])) {
|
| 95 |
$target = $_REQUEST['edit']['target'];
|
| 96 |
$a = explode('/', $target);
|
| 97 |
|
| 98 |
// During node/add/foo, just get the menu item to test access.
|
| 99 |
if (($a[0]=='node')&&($a[1]=='add')) {
|
| 100 |
$item = menu_get_item(NULL, $target);
|
| 101 |
$access = $item['access'];
|
| 102 |
}
|
| 103 |
else if (($a[0]=='node')&&($a[2]=='edit')) {
|
| 104 |
$node = node_load(array('nid'=>$a[1]));
|
| 105 |
$access = node_access('update', $node);
|
| 106 |
}
|
| 107 |
}
|
| 108 |
if ($item) {
|
| 109 |
}
|
| 110 |
$items[] = array(
|
| 111 |
'path' => 'wizard',
|
| 112 |
'type' => MENU_CALLBACK,
|
| 113 |
'callback' => '_wizard_form_submit',
|
| 114 |
'access' => $access,
|
| 115 |
);
|
| 116 |
}
|
| 117 |
return $items;
|
| 118 |
}
|
| 119 |
|
| 120 |
/**
|
| 121 |
* Change the CCK form into a set of wizard pages.
|
| 122 |
*/
|
| 123 |
function wizard_form_alter($form_id, &$form) {
|
| 124 |
|
| 125 |
// Only affect node forms
|
| 126 |
if (!isset($form['#node'])) return;
|
| 127 |
|
| 128 |
// Only affect wizard pages.
|
| 129 |
$pagecount = _wizard_count_pages($form);
|
| 130 |
if ($pagecount==0) return;
|
| 131 |
|
| 132 |
// Bring any elements defined before the first wizard into the first
|
| 133 |
// wizard page (especially important for title element which can't be
|
| 134 |
// weighted) by changing weights that are lower than the first wizard.
|
| 135 |
// Grab the top weight while we're scanning weights.
|
| 136 |
$firstwizweight = 1000000;
|
| 137 |
$topweight = -1000000;
|
| 138 |
foreach ($form as $key => &$element) {
|
| 139 |
if (!is_array($element)) continue;
|
| 140 |
$weight = (is_array($element)&&isset($element['#weight'])) ?
|
| 141 |
intval($element['#weight']) : 0;
|
| 142 |
if (isset($element['wizard'])) {
|
| 143 |
if ($firstwizweight > $weight) $firstwizweight = $weight;
|
| 144 |
}
|
| 145 |
if ($weight > $topweight) $topweight = $weight;
|
| 146 |
}
|
| 147 |
foreach ($form as $key => &$element) {
|
| 148 |
if (!is_array($element)) continue;
|
| 149 |
if ($element['#weight'] < $firstwizweight)
|
| 150 |
$element['#weight'] = $firstwizweight;
|
| 151 |
}
|
| 152 |
|
| 153 |
// Get the current wizard page.
|
| 154 |
if (isset($_REQUEST['edit']['page']))
|
| 155 |
$page = intval($_REQUEST['edit']['page']);
|
| 156 |
else $page = 0;
|
| 157 |
$op = $_REQUEST['op'];
|
| 158 |
if ($op == t('Next')) $page += 1;
|
| 159 |
else if ($op == t('Previous')) $page -= 1;
|
| 160 |
|
| 161 |
// Hide elements not on the current page.
|
| 162 |
uasort($form, '_form_sort'); // needs to be sorted for this to work
|
| 163 |
$scanpage = -1;
|
| 164 |
foreach ($form as $key => &$element) {
|
| 165 |
if (!is_array($element)) continue;
|
| 166 |
if (isset($element['wizard'])) { $scanpage++; continue; }
|
| 167 |
if ($element['#type']=='submit') unset($form[$key]);
|
| 168 |
else if ($element['#type']=='button') unset($form[$key]);
|
| 169 |
//else if ($scanpage != $page) $element['#type'] = 'hidden';
|
| 170 |
else if ($scanpage != $page) unset($form[$key]);
|
| 171 |
}
|
| 172 |
|
| 173 |
// Pass the wizard token through or create one.
|
| 174 |
if (isset($_REQUEST['edit']['wiztoken']))
|
| 175 |
$wiztoken = $_REQUEST['edit']['wiztoken'];
|
| 176 |
else $wiztoken = drupal_get_token('wiztoken/' . time());
|
| 177 |
$form['wiztoken'] = array(
|
| 178 |
'#type' => 'hidden',
|
| 179 |
'#default_value' => $wiztoken,
|
| 180 |
);
|
| 181 |
|
| 182 |
// Override the normal add/edit action.
|
| 183 |
if (isset($_REQUEST['edit']['target']))
|
| 184 |
$target = $_REQUEST['edit']['target'];
|
| 185 |
else {
|
| 186 |
$target = $form['#action'];
|
| 187 |
// broken for subdirectory installations
|
| 188 |
if (substr($target, 0, 4)=='/?q=') $target = substr($target, 4);
|
| 189 |
}
|
| 190 |
$form['target'] = array(
|
| 191 |
'#type' => 'hidden',
|
| 192 |
'#default_value' => $target,
|
| 193 |
);
|
| 194 |
$form['page'] = array(
|
| 195 |
'#type' => 'hidden',
|
| 196 |
'#default_value' => $page,
|
| 197 |
);
|
| 198 |
$form['#action'] = url('wizard');
|
| 199 |
|
| 200 |
// Add next/prev buttons.
|
| 201 |
$buttonweight = $topweight + 1;
|
| 202 |
$form['actions'] = array(
|
| 203 |
'#type' => 'fieldset',
|
| 204 |
'#weight' => $buttonweight,
|
| 205 |
);
|
| 206 |
|
| 207 |
// Previous isn't working right now.
|
| 208 |
/*if ($page > 0) {
|
| 209 |
$form['actions']['prev'] = array(
|
| 210 |
'#type' => 'submit',
|
| 211 |
'#default_value' => t('Previous'),
|
| 212 |
);
|
| 213 |
}*/
|
| 214 |
|
| 215 |
if ($page != $scanpage) {
|
| 216 |
$form['actions']['next'] = array(
|
| 217 |
'#type' => 'submit',
|
| 218 |
'#default_value' => t('Next'),
|
| 219 |
);
|
| 220 |
}
|
| 221 |
else {
|
| 222 |
$form['actions']['finish'] = array(
|
| 223 |
'#type' => 'submit',
|
| 224 |
'#default_value' => t('Finish'),
|
| 225 |
);
|
| 226 |
}
|
| 227 |
}
|
| 228 |
|
| 229 |
function _wizard_form_submit() {
|
| 230 |
|
| 231 |
// Get the target.
|
| 232 |
if (isset($_POST['edit']['target'])) $target = $_POST['edit']['target'];
|
| 233 |
else $target = '';
|
| 234 |
$a = explode('/', $target);
|
| 235 |
|
| 236 |
// Get or create a node to edit.
|
| 237 |
$wiztoken = $_POST['edit']['wiztoken'];
|
| 238 |
if (isset($_SESSION[$wiztoken])) {
|
| 239 |
$node = &$_SESSION[$wiztoken];
|
| 240 |
}
|
| 241 |
else if (($a[0]=='node')&&($a[1]=='add')) {
|
| 242 |
$node = new StdClass;
|
| 243 |
$node->type = $a[2];
|
| 244 |
}
|
| 245 |
else if (($a[0]=='node')&&($a[2]=='edit')) {
|
| 246 |
$node = node_load(array('nid'=>$a[1]));
|
| 247 |
}
|
| 248 |
|
| 249 |
// Update the node.
|
| 250 |
foreach ($_POST['edit'] as $k => $v) $node->$k = $v;
|
| 251 |
$_SESSION[$wiztoken] = $node;
|
| 252 |
|
| 253 |
// Return the form.
|
| 254 |
content_submit($node);
|
| 255 |
node_save($node); // is it redundant?
|
| 256 |
unset($_POST['edit']);
|
| 257 |
return node_form($node);
|
| 258 |
}
|