| [ Index ] |
PHP Cross Reference of Drupal 6 (gatewave) |
[Summary view] [Print] [Text view]
1 <?php 2 // $Id: macro.module,v 1.3.2.2 2009/06/11 21:39:39 nickel Exp $ 3 4 /** 5 * @file 6 * - allow administrators to record (export) form submissions 7 * - allow administrators to replay (import) form submissions 8 */ 9 10 /** 11 * Implementation of hook_help(). 12 */ 13 function macro_help($section) { 14 switch ($section) { 15 case 'admin/help#macro': 16 $output = t('Todo: Add help text.'); 17 case 'admin/build/macro/export' : 18 return t('This output can be saved to the profile`s .macro file, to be automatically played back upon completed install or used on an import on another site.'); 19 case 'admin/build/macro/import' : 20 return t('Insert recorded macro here to be played into your site. All referenced modules needs to be enabled.'); 21 case 'admin/build/macro': 22 return t('Configuration settings for the drupal macro engine.'); 23 } 24 } 25 26 27 /** 28 * Implementation of hook_perm(). 29 */ 30 function macro_perm() { 31 return array('administer macro settings', 'macro access'); 32 } 33 34 35 /** 36 * Implementation of hook_init(). 37 */ 38 function macro_init() { 39 if (empty($_POST) && variable_get('macro_enabled', FALSE)) { 40 $args = array( 41 '%d' => count(variable_get('macro_submissions', array())), 42 '!link' => l('End session', 'admin/build/macro/export/session/end') 43 ); 44 45 drupal_set_message(t('[Active macros: %d | !link]', $args)); 46 } 47 } 48 49 50 /** 51 * Implementation of hook_menu(). 52 */ 53 function macro_menu() { 54 $items = array(); 55 56 $items['admin/build/macro'] = array( 57 'title' => 'Macro engine', 58 'description' => 'Configure the Drupal macro engine. Export recorded macros or import previously recorded macros.', 59 'page callback' => 'drupal_get_form', 60 'page arguments' => array('macro_admin_settings'), 61 'access callback' => 'user_access', 62 'access arguments' => array('administer macro settings'), 63 ); 64 $items['admin/build/macro/export'] = array( 65 'title' => 'Export', 66 'page callback' => 'drupal_get_form', 67 'page arguments' => array('macro_export_macro'), 68 'access arguments' => array('macro access'), 69 'type' => MENU_LOCAL_TASK, 70 ); 71 $items['admin/build/macro/import'] = array( 72 'title' => 'Import', 73 'page callback' => 'drupal_get_form', 74 'page arguments' => array('macro_import_macro'), 75 'access arguments' => array('macro access'), 76 'type' => MENU_LOCAL_TASK, 77 ); 78 $items['admin/build/macro/settings'] = array( 79 'title' => 'Configure', 80 'type' => MENU_DEFAULT_LOCAL_TASK, 81 'weight' => -10, 82 ); 83 $items['admin/build/macro/export/session/end'] = array( 84 'page callback' => 'macro_end_macro_session', 85 'access arguments' => array('macro access'), 86 'type' => MENU_CALLBACK, 87 ); 88 89 return $items; 90 } 91 92 93 /** 94 * Implementation of hook_form_alter(). 95 */ 96 function macro_form_alter(&$form, $form_state, $form_id) { 97 switch ($form_id) { 98 case 'macro_export_macro': 99 /** TODO: eventually add file saving with foldering options. 100 $form['file-save'] = array('#type' => 'fieldset', '#title' => 'Save To File', '#description' => ''); 101 $form['file-save']['filename'] = array('#type' => 'textfield', '#title' => 'Filename'); 102 $form['file-save]['save'] = array('#type' => 'submit', '#value' => 'Save', '#submit' => array('macro_export_file_save_submit')); 103 */ 104 break; 105 106 // Forms to specifically ignore for macro. 107 case 'macro_admin_settings': case 'macro_import_macro': case 'macro_export_macro': 108 109 break; 110 111 default: 112 // Add import / export buttons to each form for simplified macro saving. 113 if (user_access('macro access') && variable_get('macro_display_actions', FALSE)) { 114 $form['macro-actions'] = array('#type' => 'fieldset', '#title' => t('Macro actions'), '#weight' => 5000, '#collapsible' => TRUE, '#collapsed' => TRUE); 115 $form['macro-actions']['import-data'] = array('#type' => 'submit', '#name' => 'import', '#value' => t('Import'), '#submit' => array('macro_import_action_submit')); 116 $form['macro-actions']['export-data'] = array('#type' => 'submit', '#name' => 'export', '#value' => t('Export'), '#submit' => array('macro_export_action_submit')); 117 118 if (!variable_get('macro_enabled', FALSE)) { 119 $form['macro-actions']['export-session-data'] = array('#type' => 'submit', '#value' => t('Start session'), '#submit' => array('macro_export_session_action_submit')); 120 } 121 } 122 123 // Add the record callback on submit. 124 if ($form_id != 'macro_import_macro' && variable_get('macro_enabled', FALSE)) { 125 $form['#submit'][] = 'macro_record_macro'; 126 } 127 128 // Clear the current sessions. 129 if (variable_get('macro_delete', FALSE)) { 130 variable_set('macro_submissions', array()); 131 variable_set('macro_delete', FALSE); 132 } 133 break; 134 } 135 } 136 137 138 /** 139 * Form submit handler to redirect to the import form. 140 */ 141 function macro_import_action_submit($form, &$form_state) { 142 drupal_goto('admin/build/macro/import', drupal_get_destination()); 143 } 144 145 146 /** 147 * Form callback to handle macro export functionality. 148 */ 149 function macro_export_action_submit($form, &$form_state) { 150 // Start a fresh session. 151 variable_set('macro_submissions', array()); 152 153 // Record the single macro. 154 macro_record_macro($form, $form_state); 155 156 // Send straight to the export form. 157 drupal_goto('admin/build/macro/export'); 158 } 159 160 161 /** 162 * Form callback to handle macro export session functionality. 163 */ 164 function macro_export_session_action_submit($form, &$form_state) { 165 // Start recording submissions and clear the saved submissions for a fresh session. 166 variable_set('macro_enabled', TRUE); 167 variable_set('macro_submissions', array()); 168 } 169 170 171 /** 172 * A form submission handler, that stores the form submissions into the variables table 173 */ 174 function macro_record_macro($form, &$form_state) { 175 $macros = variable_get('macro_submissions', array()); 176 177 // Remove the $form_state as it will be rebuilt on import. 178 array_shift($form['#parameters']); 179 180 // TODO: Why is it when the record method is called through the $form['#submit'] when 181 // the action buttons are not displayed do these exception values not show up in the 'values' ? 182 $exceptions = array('export', 'export-data', 'export-session-data', 'import-data', 183 'form_id', 'submit', 'reset', 'form_build_id', 'form_token', 'delete'); 184 185 // Remove the unneeded values that this module implements. 186 foreach ($exceptions as $exception) { 187 unset($form_state['values'][$exception]); 188 } 189 190 $macro = array( 191 'form_id' => $form['form_id']['#value'], 192 'path' => $_GET['q'], 193 'parameters' => $form['#parameters'], 194 'values' => $form_state['values'], 195 ); 196 197 // Support for multistep. 198 if ($form_state['storage']) { 199 $macro['storage'] = $form_state['storage']; 200 } 201 202 $macros[] = $macro; 203 variable_set('macro_submissions', $macros); 204 205 return $macro; 206 } 207 208 209 /** 210 * This recursively runs thru an object and converts it into an array. 211 * This is to be called for form entries as we do not want varexport to treat any element 212 * as an object. If varexport sees an object, it will output stdClass::__set_state, which is 213 * not defined and we cannot define it either. So we recursively cast all objects to arrays. 214 */ 215 function _macro_recursively_convert_objects_to_arrays($entity) { 216 $converted = array(); 217 218 foreach (((array) $entity) as $key => $value) { 219 if (is_array($value) || is_object($value)) { 220 $converted[$key] = _macro_recursively_convert_objects_to_arrays($value); 221 } 222 else { 223 $converted[$key] = $value; 224 } 225 } 226 227 return($converted); 228 } 229 230 231 /** 232 * A form callback that displays the macro exported. 233 * 234 * The output of this callback should be saved to the profiles/$profile/macros.inc file, to be 235 * automatically played back upon completed install. 236 * @return a textarea containing the recorded macros 237 */ 238 function macro_export_macro() { 239 $form['code'] = array( 240 '#type' => 'textarea', 241 '#title' => 'macros exported', 242 '#default_value' => macro_get_macro(), 243 '#rows' => 20, 244 ); 245 return $form; 246 247 } 248 249 250 /** 251 * The output of this callback should be saved to the profiles/$profile/macros.inc file, to be 252 * automatically played back upon completed install. 253 * @return a code representation of the recorded macro. 254 */ 255 function macro_get_macro() { 256 $subs = variable_get('macro_submissions', array()); 257 $string = ''; 258 foreach ($subs as $key => $form) { 259 $string .= "\$macro[$key]['form_id'] = " . var_export($form['form_id'], TRUE) . ";\n"; 260 $string .= "\$macro[$key]['path'] = " . var_export($form['path'], TRUE) . ";\n"; 261 $string .= "\$macro[$key]['values'] = " . var_export(_macro_recursively_convert_objects_to_arrays((array) $form['values']), TRUE) . ";\n"; 262 // Add multistep support. 263 if ($form['storage']) { 264 $string .= "\$macro[$key]['storage'] = " . var_export(_macro_recursively_convert_objects_to_arrays((array) $form['storage']), TRUE) . ";\n"; 265 } 266 // the form parameters are being used here. 267 array_shift($form['parameters']); 268 $string .= "\$macro[$key]['parameters'] = " . var_export(serialize($form['parameters']), TRUE) . ";\n\n"; 269 270 } 271 272 return $string; 273 } 274 275 276 /** 277 * A form callback that displays the macro import form. 278 * 279 * @return a form for importing a previously recorded macro 280 */ 281 function macro_import_macro() { 282 $form['macro'] = array( 283 '#type' => 'textarea', 284 '#title' => 'macro to import', 285 '#rows' => 20, 286 ); 287 $form['submit'] = array( 288 '#type' => 'submit', 289 '#value' => t('play macro'), 290 ); 291 return $form; 292 } 293 294 295 /** 296 * Implementation of macro_import_macro hook_submit function. 297 * 298 * Plays back the submitted macro. 299 */ 300 function macro_import_macro_submit($form, &$form_state) { 301 include_once './includes/install.inc'; 302 eval($form_state['values']['macro']); 303 drupal_execute_macro($macro); 304 } 305 306 307 /** 308 * Menu callback for the macro settings form. 309 */ 310 function macro_admin_settings() { 311 $form['settings_general'] = array( 312 '#type' => 'fieldset', 313 '#title' => t('Macro settings'), 314 '#collapsible' => TRUE, 315 ); 316 $form['settings_general']['macro_enabled'] = array( 317 '#type' => 'checkbox', 318 '#title' => t('Enable macro recording'), 319 '#default_value' => variable_get('macro_enabled', FALSE), 320 '#description' => t('Set whether the macro engine will record form submissions.'), 321 ); 322 $form['settings_general']['macro_display_actions'] = array( 323 '#type' => 'checkbox', 324 '#title' => 'Display Actions', 325 '#description' => 'Add "import / export" buttons at the bottom of each form that is displayed.', 326 '#default_value' => variable_get('macro_display_actions', FALSE), 327 ); 328 $form['settings_general']['macro_delete'] = array( 329 '#type' => 'checkbox', 330 '#title' => t('Delete recorded macro'), 331 '#default_value' => variable_get('macro_delete', FALSE), 332 '#description' => t('Set whether to clear previously recorded macro.'), 333 ); 334 335 return system_settings_form($form); 336 } 337 338 339 /** 340 * End a macro session from a page callback with a redirect to the export form. 341 */ 342 function macro_end_macro_session() { 343 variable_set('macro_enabled', FALSE); 344 drupal_goto('admin/build/macro/export'); 345 } 346 347 348 /** 349 * Attempts to programmatically submit all the forms that have been specified in the $macros collection. 350 * 351 * @param array 352 * - a list of macros to execute 353 * 354 * @return array 355 * - a list of results based on the macros provided. 356 */ 357 function drupal_execute_macro($macro) { 358 foreach ($macro as $key => $data) { 359 $item = menu_get_item($data['path']); 360 if ($item && !empty($item['file'])) { 361 include_once $item['file']; 362 } 363 } 364 365 $results = array(); 366 foreach ($macro as $key => $data) { 367 $param = unserialize($data['parameters']); 368 $form_values = array('values' => $data['values']); 369 // Support for multistep. 370 if ($data['storage']) { 371 $form_values['storage'] = $data['storage']; 372 } 373 $args = array($data['form_id'], $form_values); 374 $args = array_merge($args, $param); 375 376 $results[] = call_user_func_array('drupal_execute', $args); 377 if (form_get_errors()) { 378 drupal_set_message(t("An error has occured with macro #%macro_number , form_id %form_id. Please check the errors displayed for more details.", array('%macro_number' => $key, '%form_id' => $data['form_id']))); 379 } 380 } 381 382 return $results; 383 } 384
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Thu Mar 24 11:18:33 2011 | Cross-referenced by PHPXref 0.7 |