| [ Index ] |
PHP Cross Reference of Drupal 6 (yi-drupal) |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * Transform the single-step form into a mult-step form by breaking apart the outer-layer of 5 * fieldsets (leaving nested fieldsets alone). Adds the necesary steps, storage, etc. to make 6 * it perform as a typical multi-step form. 7 * 8 * @param $form_state 9 * The current form state. Since this function may be called while stepping through the form, 10 * it's necesary to keep up with all the pieces. 11 * 12 * @return 13 * The multi-step form array. 14 */ 15 function form_manager_form(&$form_state, $form_id) { 16 17 // ensure step is set in storage 18 if (empty($form_state['storage']['#navigation']['step'])) { 19 $form_state['storage']['#navigation']['step'] = 1; 20 //$form_state['storage']['#navigation']['newStep'] = $form_state['storage']['#navigation']['step'] + 1; 21 } 22 elseif ($form_state['storage']['#navigation']['newStep']) { 23 // if the form has been submitted, check for a new step 24 $form_state['storage']['#navigation']['step'] = $form_state['storage']['#navigation']['newStep']; 25 unset ($form_state['storage']['#navigation']['newStep']); 26 } 27 28 $form = array(); 29 30 // add form fields 31 _form_manager_form_add_fields($form, $form_state, $form_id); 32 33 // calculate max number of steps 34 _form_manager_form_max_steps($form, $form_state); 35 36 // create tabs 37 _form_manager_form_create_tabs($form, $form_state); 38 39 // remove form fields per step 40 _form_manager_form_remove_fields($form, $form_state); 41 42 // Once the fieldset has been extracted to display that particular "page", remove the fieldset element 43 _form_manager_form_remove_fieldsets($form, $form_state); 44 45 // set default values for the remaining form elements 46 _form_manager_form_default_values($form, $form_state); 47 48 // add tabs to form 49 _form_manager_form_add_tabs($form, $form_state); 50 51 // add buttons to form 52 _form_manager_form_add_buttons($form, $form_state); 53 54 // add file support to form 55 _form_manager_form_add_file_support($form, $form_state); 56 57 return $form; 58 } 59 60 61 function _form_manager_form(&$form, &$form_state) { 62 _form_manager_form_add_fields($form, $form_state); 63 //return $form; 64 } 65 66 67 function _form_manager_form_add_fields(&$form, &$form_state, $form_id = '') { 68 // TODO: Get the form_id passed as a parameter to this function 69 if (empty($form_id)) { 70 if (arg(1) == 'build') { 71 $form_id = check_plain(arg(4)); 72 } 73 else { 74 $form_id = check_plain(arg(1)); 75 } 76 } 77 78 form_manager_get_form($form, $form_state, $form_id); 79 _form_manager_convert_form($form, $form_id); 80 81 form_manager_set_hidden_values($form, $form_state, $form_id); 82 } 83 84 85 function form_manager_get_form(&$form = array(), &$form_state, $form_id) { 86 if (empty($form_id)) return FALSE; 87 88 static $form_cache; 89 90 // TODO: This is causing issues after initial submit of a form. Remove from caching for now. 91 //if (empty($form_cache)) { 92 if (1) { 93 if (($cache = cache_get("form_manager-$form_id"))) { 94 $form_cache = unserialize($cache->data); 95 $form = $form_cache; 96 97 } 98 else { 99 // Look in the database for the form 100 $data = db_fetch_array(db_query("SELECT * FROM {form_manager_forms} WHERE form_id='%s'", $form_id)); 101 if (!empty($data)) { 102 $form = unserialize($data['data']); 103 #cache_set("form_manager-$form_id", 'cache', $data['data'], CACHE_TEMPORARY); 104 cache_set("form_manager-$form_id", $data['data'], 'cache', CACHE_TEMPORARY); 105 return $form; 106 } 107 108 // If not found in the database, look in an include file for the form 109 else { 110 // TODO: Enable a hook to allow other modules to include their form .inc files 111 $file = drupal_get_path('module', 'form_manager') . "/forms/$form_id.inc"; 112 if (file_exists($file)) { 113 // As much as I want to, module_load_include doesn't make the $form variable active 114 //module_load_include('inc', 'form_manager', "forms/$form_id"); 115 include_once($file); 116 #cache_set("form_manager-$form_id", 'cache', serialize($form), CACHE_TEMPORARY); 117 cache_set("form_manager-$form_id", serialize($form), 'cache', CACHE_TEMPORARY); 118 return $form; 119 } 120 } 121 } 122 } 123 124 return $form_cache; 125 } 126 127 128 /** 129 * Helper function to get the max number of steps in the forms, so that #navigation 130 * can keep track of where the user is at and give prev/next buttons accordingly. 131 */ 132 function _form_manager_form_max_steps($form, &$form_state) { 133 // calculate max steps if empty 134 if (empty($form_state['storage']['#navigation']['maxSteps'])) { 135 $fieldsetCount = 0; 136 $otherFields = false; 137 138 // loop through form elements 139 foreach ($form as $k => $v) { 140 // check for fieldset 141 if (is_array($v) && $v['#type'] == 'fieldset') { 142 $fieldsetCount++; 143 } else { 144 $otherFields = true; 145 } 146 } 147 148 // if there are other fields, increment max steps 149 if ($otherFields) { 150 $fieldsetCount++; 151 $form_state['storage']['#navigation']['otherFields'] = true; 152 } 153 154 // add count to storage 155 $form_state['storage']['#navigation']['maxSteps'] = $fieldsetCount; 156 } 157 } 158 159 function _form_manager_form_create_tabs(&$form, &$form_state) { 160 // create list of tabs if not set in storage 161 if (empty($form_state['storage']['#navigation']['tabs'])) { 162 $tabsCount = 0; 163 $tabs = array(); 164 165 // check for other fields 166 if ($form_state['storage']['#navigation']['otherFields'] && $form_state['storage']['#navigation']['maxSteps'] > 1) { 167 $tabsCount++; 168 $tabs[$tabsCount] = "Start"; 169 } 170 171 // loop through form items 172 foreach ($form as $k => $v) { 173 if (is_array($v) && $v['#type']=='fieldset') { 174 $tabsCount++; 175 $tabs[$tabsCount] = $v['#title']; 176 } 177 } 178 179 // add tabs to storage 180 $form_state['storage']['#navigation']['tabs'] = $tabs; 181 182 } 183 184 } 185 186 187 /** 188 * Helper function to remove all form elements that aren't part of the current "page" 189 */ 190 function _form_manager_form_remove_fields(&$form, &$form_state) { 191 // set fieldset counter based on otherfields 192 if ($form_state['storage']['#navigation']['otherFields']) { 193 $fieldsetCount = 1; 194 } else { 195 $fieldsetCount = 0; 196 } 197 198 // loop though form fields 199 foreach ($form as $k => $v) { 200 // ensure this form item is a fieldset 201 if (is_array($v) && $v['#type']=='fieldset') { 202 203 // increment count of fieldsets 204 $fieldsetCount++; 205 206 // unset fieldset 207 if ($form_state['storage']['#navigation']['step'] != $fieldsetCount) unset($form[$k]); 208 209 } elseif (is_array($v) && $form_state['storage']['#navigation']['otherFields'] && $form_state['storage']['#navigation']['step'] > 1) { 210 // unset field 211 unset($form[$k]); 212 } 213 } 214 $form_state['storage']['removed_fields'] = TRUE; 215 } 216 217 218 /** 219 * Helper function to add remove the current page's fieldset wrapper so that only the form elements 220 * are presented to the user. If the top-level field element is a fieldset, loop through it to find 221 * all the children (and not properties, throw those away) and add those directly to the form. Then 222 * unset the fieldset once the children are safely removed. 223 */ 224 function _form_manager_form_remove_fieldsets(&$form, &$form_state) { 225 foreach (element_children($form) as $child) { 226 if ($form[$child]['#type'] == 'fieldset') { 227 foreach (element_children($form[$child]) as $key) { 228 if(element_child($form[$child])) { 229 $form[$key] = $form[$child][$key]; 230 } 231 } 232 unset($form[$child]); 233 } 234 } 235 } 236 237 238 /** 239 * Helper function to add the default values to the current form's page if the user has 240 * submitted anything. This is taken from form_state[storage']. 241 */ 242 function _form_manager_form_default_values(&$form, &$form_state, $parent = NULL) { 243 $excludes = array('fieldset', 'password'); 244 245 // Some FAPI element types don't/can't have default_values 246 if (!in_array($form['#type'], $excludes) && $form_state['storage'][$parent]) { 247 $form['#default_value'] = $form_state['storage'][$parent]; 248 } 249 250 // Recurse through the form or element to make sure that we set the default values on 251 // any child elements 252 foreach (element_children($form) as $key => $child) { 253 _form_manager_form_default_values($form[$child], $form_state, $child); 254 } 255 } 256 257 258 function _form_manager_form_add_tabs(&$form, &$form_state) { 259 $tabsArray = array(); 260 $tabsCount = 0; 261 262 // loop through tabs and create buttons for each one 263 foreach ($form_state['storage']['#navigation']['tabs'] as $k => $v) { 264 $tabsCount++; 265 $tabsArray['tab_' . $k] = array( 266 '#type' => 'submit', 267 '#value' => t($v), 268 '#attributes' => array( 269 'class' => 'tabs' . ($tabsCount == $form_state['storage']['#navigation']['step'] ? ' active' : '') 270 ), 271 // Set the weight to something really light so that the tab buttons will always be on top 272 '#weight' => -50 + $tabsCount, 273 ); 274 } 275 276 // add tab buttons to beginning of form 277 $form = array_merge($tabsArray, $form); 278 279 } 280 281 function _form_manager_form_add_buttons(&$form, &$form_state) { 282 // add previous button 283 if ($form_state['storage']['#navigation']['step'] > 1) { 284 $form['previous'] = array( 285 '#type' => 'submit', 286 '#value' => t('Previous'), 287 // Set the weight to something really heavy so that the previous/next/save buttons will always be on bottom 288 '#weight' => 53, 289 ); 290 } 291 292 // add next button 293 if ($form_state['storage']['#navigation']['step'] < $form_state['storage']['#navigation']['maxSteps']) { 294 $form['next'] = array( 295 '#type' => 'submit', 296 '#value' => t('Next'), 297 '#weight' => 52, // ibid, see $form['previous'] 298 ); 299 } 300 301 // add save button 302 $form['submit'] = array( 303 '#type' => 'submit', 304 '#value' => t('Save'), 305 '#weight' => 51, // ibid, see $form['previous'] 306 ); 307 } 308 309 /** 310 * Helper function to add file support to the form 311 */ 312 function _form_manager_form_add_file_support(&$form, &$form_state) { 313 $form['#attributes'] = array('enctype' => 'multipart/form-data'); 314 } 315 316 317 function form_manager_set_hidden_values(&$form, &$form_state, $form_id) { 318 319 } 320 321 322 323 324
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Mon Jul 9 18:01:44 2012 | Cross-referenced by PHPXref 0.7 |