| [ Index ] |
PHP Cross Reference of Drupal 6 (yi-drupal) |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * Callback function from hook_menu to display admin options. 5 * 6 * @return 7 * Themed output of the table of forms with possible action links. 8 */ 9 function form_manager_admin() { 10 // Get the full list of all available forms 11 $forms = _form_manager_list_forms(); 12 13 // print table of forms: Form ID, Form Status, Action 14 $headers = array( 15 t('Form ID'), 16 t('Status'), 17 t('Actions'), 18 ); 19 20 $rows = array(); 21 22 // Create the table rows 23 foreach ($forms as $form_id => $form) { 24 $status = $form['source']; 25 $delete = t('delete'); 26 $edit = l(t('edit'), 'admin/build/forms/edit/' . $form_id); 27 $export = l(t('export FAPI code'), 'admin/build/forms/exportfapi/' . $form_id); 28 $exportable = l(t('export form'), 'admin/build/forms/export/' . $form_id); 29 30 // Determine modified status 31 if (($form['file'] || $form['database']) && $form['fbcache']) { 32 $status = t('database'); 33 $delete = t('revert'); 34 } 35 elseif ($form['file'] && $form['database']) { 36 $status = t('modified'); 37 $delete = t('revert'); 38 } 39 elseif ($form['file']) { 40 $delete = ''; 41 } 42 elseif ($form['fbcache']) { 43 $status = t('unsaved'); 44 } 45 46 // Link delete/revert button 47 $delete = (!empty($delete)) ? l($delete, "admin/build/forms/delete/$form_id") : ''; 48 49 // Link $form_id 50 $form_path = db_result(db_query("SELECT path FROM {form_manager_settings} WHERE form_id='%s'", $form_id)); 51 $form_id = (!empty($form_path)) ? l($form_id, $form_path) : l($form_id, "form/$form_id"); 52 // Create the actual table rows. 53 $rows[] = array( 54 $form_id, 55 $status, 56 $edit . ' ' . $export . ' ' . $exportable . ' ' . $delete, 57 ); 58 } 59 60 $bottom_links = l(t('Create a new form'), 'admin/build/forms/add') . ' ' . l(t('Import a form'), 'admin/build/forms/import'); 61 $rows[] = array( 62 array('data' => $bottom_links, 'colspan' => 3), 63 ); 64 65 // Spit out the themed table for output. 66 $output = theme('table', $headers, $rows); 67 68 return $output; 69 } 70 71 72 /** 73 * Helper function to list the forms of all available forms. When the same form exists in multiple 74 * locations (file, db, etc), the following precedence is used to return the form once: 75 * - file 76 * - Form Manager table (database) 77 * - Form Builder cache (temporary storage) 78 * 79 * @return 80 * An associative array containing all the forms. 81 * - Key: form_id 82 * - name: form_id 83 * - source: the source (database or file) 84 */ 85 function _form_manager_list_forms() { 86 // Get list of forms in files 87 $file_forms = _form_manager_list_forms_by_type('file'); 88 89 // Get list of forms in form manager table 90 $db_forms = _form_manager_list_forms_by_type('database'); 91 92 // Get list of forms in form builder cache 93 $fb_forms = _form_manager_list_forms_by_type('fbcache'); 94 95 // Shuffle the sources together. Put db forms after file forms, so that they'll take precedence. 96 $forms = array_merge($file_forms, $db_forms, $fb_forms); 97 foreach ($forms as $form_id => $form) { 98 $forms[$form_id]['file'] = $file_forms[$form_id]['file']; 99 $forms[$form_id]['database'] = $db_forms[$form_id]['database']; 100 $forms[$form_id]['fbcache'] = $fb_forms[$form_id]['fbcache']; 101 } 102 103 return $forms; 104 } 105 106 107 /** 108 * Helper function to list the forms of a given source of a given type (file, database, etc). 109 * 110 * @param $type 111 * The source type of the form. 112 * - file: The forms stored in code in the forms directory with the filename of form_id.inc 113 * - database: The forms stored in the database. These may be unique or may override forms with the same form_id stored in a file. Forms stored in the database will take precedence. 114 * - fbcache: The forms stored in Form Builder's cache. Generally, these are forms that haven't been saved yet. These will take prcedence over all other forms. 115 * 116 * @return 117 * An associative array containing all the forms of the given source type. 118 * - Key: form_id 119 * - name: form_id 120 * - source: the source (form builder cache, database, or file) 121 */ 122 function _form_manager_list_forms_by_type($type = 'file') { 123 $forms = array(); 124 125 switch($type) { 126 case 'file': 127 $forms_path = drupal_get_path('module', 'form_manager') . '/forms'; 128 $mask = '.*\.inc$'; 129 $files = file_scan_directory($forms_path, $mask); 130 131 foreach ($files as $file) { 132 $forms[$file->name] = array( 133 'name' => $file->name, 134 'source' => 'file', 135 $type => TRUE, 136 ); 137 } 138 break; 139 140 case 'database': 141 $results = db_query("SELECT form_id FROM {form_manager_forms}"); 142 while ($data = db_fetch_array($results)) { 143 $forms[$data['form_id']] = array( 144 'name' => $data['form_id'], 145 'source' => 'database', 146 $type => TRUE, 147 ); 148 } 149 break; 150 151 case 'fbcache': 152 $results = db_query("SELECT form_id FROM {form_builder_cache} WHERE sid='%s'", session_id()); 153 while ($data = db_fetch_array($results)) { 154 $forms[$data['form_id']] = array( 155 'name' => $data['form_id'], 156 'source' => 'file builder cache', 157 $type => TRUE, 158 ); 159 } 160 break; 161 } 162 163 return $forms; 164 } 165 166 167 /** 168 * form function for exporting an entire FAPI array. 169 */ 170 function form_manager_export_form($form, $form_id) { 171 module_load_include('inc', 'form_builder', 'includes/form_builder.api'); 172 module_load_include('inc', 'form_builder', 'includes/form_builder.cache'); 173 module_load_include('inc', 'form_manager', 'includes/form_manager.pages'); 174 175 $form = array(); 176 177 $form_state = array(); 178 $current = array(); 179 $current = form_builder_cache_load('form_manager', $form_id); 180 if (empty($current)) { 181 form_manager_get_form($current, $form_state, $form_id); 182 } 183 184 $form['form_manager'] = array( 185 '#type' => 'textarea', 186 '#title' => t('Export code'), 187 '#default_value' => form_manager_export($current), 188 '#attributes' => array('readonly' => 'readonly', 'style' => 'font-family: monospace;'), 189 '#rows' => 20, 190 ); 191 192 return $form; 193 } 194 195 /** 196 * Menu callback for exporting an entire FAPI array. 197 * 198 * @param $form_id 199 * The form_id of the form to retreive. 200 * 201 * @return 202 * HTML output; the content of the page. 203 */ 204 function form_manager_export_form_page($form_id) { 205 $links = array(); 206 $links[] = array( 207 'title' => t('list forms'), 208 'href' => 'admin/build/forms', 209 'attributes' => '', 210 'query' => '', 211 'fragment' => '', 212 ); 213 $links[] = array( 214 'title' => t('edit'), 215 'href' => 'admin/build/forms/edit/' . $form_id, 216 'attributes' => '', 217 'query' => '', 218 'fragment' => '', 219 ); 220 221 $output = ''; 222 $output .= theme('links', $links, array('class' => 'tabs secondary')); 223 $output .= drupal_get_form('form_manager_export_form', $form_id); 224 225 return $output; 226 } 227 228 229 function form_manager_export($form) { 230 $output = ''; 231 $output .= form_manager_export_recurse($form); 232 //$output .= 'return $form;'; 233 return $output; 234 } 235 236 237 /** 238 * Recursive function for pretty-printing of FAPI arrays. 239 */ 240 function form_manager_export_recurse($form, $parents = array()) { 241 $output = ''; 242 243 form_manager_reset_element_keys($form); 244 245 // Sort this level of the array according to weight. 246 uasort($form, 'element_sort'); 247 248 // Print out this parent element and it's properties. 249 $properties = element_properties($form); 250 $omit = array('#form_builder', '#key', '#form_manager_form_id', '#element_name'); 251 if (count($properties)) { 252 $output .= form_manager_export_variable_name($parents, $form) . " = array(\n"; 253 foreach (element_properties($form) as $property) { 254 if (!in_array($property, $omit)) { 255 if (is_array($form[$property])) { 256 $output .= " '". $property . "' => array(\n"; 257 foreach ($form[$property] as $key => $value) { 258 $output .= " '" . $key . "' => '". str_replace("'", "\'", $value) ."',\n"; 259 } 260 $output .= " ),\n"; 261 } 262 else { 263 $output .= " '". $property . "' => '" . str_replace("'", "\'", $form[$property]) ."',\n"; 264 } 265 } 266 } 267 $output .= ");\n"; 268 } 269 else { 270 //$output .= form_manager_export_variable_name($parents) . " = array();\n"; 271 } 272 273 foreach (element_children($form) as $key) { 274 $parents[] = $key; 275 $output .= form_manager_export_recurse($form[$key], $parents); 276 array_pop($parents); 277 } 278 279 return $output; 280 } 281 282 283 function form_manager_export_variable_name($parents, $form = array()) { 284 $output = '$form'; 285 foreach ($parents as $parent) { 286 $output .= "['". $parent ."']"; 287 } 288 return $output; 289 } 290 291 /** 292 * Look for element_name and change the Form Builder default key name to this value. 293 * 294 * This really just makes a copy of the sub-array with the new key name, and unsets 295 * the original sub-array. It also unsets the element_name element just to make sure 296 * this doesn't act multiple times on the same element. 297 */ 298 function form_manager_reset_element_keys(&$form) { 299 foreach ($form as $key => $element) { 300 if (!empty($element['#element_name']) && is_array($element) && $element['#element_name'] != $key) { 301 $element_name = $element['#element_name']; 302 unset($element['#element_name']); 303 $form[$element_name] = $element; 304 unset($form[$key]); 305 } 306 } 307 } 308 309 /** 310 * Main form building interface. Can be used as a menu callback. 311 * 312 * @param $form_type 313 * The type of form being edited. Usually the name of the providing module. 314 * @param $form_id 315 * The unique identifier for the form being edited with the type. 316 */ 317 function form_manager_edit($form_type, $form_id) { 318 module_load_include('inc', 'form_builder', 'includes/form_builder.api'); 319 module_load_include('inc', 'form_builder', 'includes/form_builder.cache'); 320 module_load_include('inc', 'form_builder', 'includes/form_builder.admin'); 321 322 // Set the current form type (used for display of the sidebar block). 323 form_builder_active_form($form_type, $form_id); // Is this a noop? 324 325 $form = _form_manager_load_form($form_type, $form_id); 326 327 $output = ''; 328 $output .= drupal_get_form('form_builder_preview', $form, $form_type, $form_id); 329 $output .= drupal_get_form('form_builder_positions', $form, $form_type, $form_id); 330 $output .= drupal_get_form('form_manager_edit_actions'); 331 332 return $output; 333 } 334 335 336 function _form_manager_load_form($form_type, $form_id) { 337 // Load the current state of the form, or create a new cache if needed. 338 $form = form_builder_cache_load($form_type, $form_id); 339 if (!$form) { 340 $form = form_builder_load_form($form_type, $form_id); 341 form_builder_cache_save($form_type, $form_id, $form); 342 } 343 344 return $form; 345 } 346 347 /** 348 * Form function to generate some options below the Form Builder set of forms to: 349 * - Save -- Copies the Form Builder cache into the Form Manager table (after making some slight alterations) 350 * - Export -- Generates a FAPI-appropriate array to copy 351 * - Cancel -- Cancel the form changes and go back to the main admin page 352 */ 353 function form_manager_edit_actions() { 354 $form = array(); 355 356 $form['formid'] = array( 357 '#type' => 'hidden', 358 '#value' => check_plain(arg(4)), 359 ); 360 361 // Buttons 362 $form['save'] = array( 363 '#type' => 'submit', 364 '#value' => t('Save'), 365 ); 366 $form['export'] = array( 367 '#type' => 'submit', 368 '#value' => t('Export'), 369 ); 370 $form['cancel'] = array( 371 '#type' => 'submit', 372 '#value' => t('Cancel'), 373 ); 374 375 return $form; 376 } 377 378 /** 379 * Implementation of hook_submit to handle the form_manager_edit_actions form. 380 */ 381 function form_manager_edit_actions_submit(&$form, &$form_values) { 382 $values = $form_values['values']; 383 $op = $form_values['values']['op']; 384 385 switch($op) { 386 case 'Cancel': 387 // TODO: Use a confirm form to confirm and delete the form_builder_cache cached form 388 drupal_goto('admin/build/forms'); 389 break; 390 391 case 'Save': 392 // TODO: Use a confirm form to confirm and delete the form_builder_cache cached form 393 _form_manager_edit_actions_submit_save($values); 394 break; 395 396 case 'Export': 397 // TODO: Use a confirm form to confirm and delete the form_builder_cache cached form 398 drupal_goto('admin/build/forms/export/' . $values['formid']); 399 break; 400 } 401 402 } 403 404 /** 405 * Helper function to save the form sent to form_manager_edit_actions_submit() 406 */ 407 function _form_manager_edit_actions_submit_save($values) { 408 $session = session_id(); 409 // Copy the form_builder_cache'ed form to the form_manager_forms table (updating if necesary) 410 $fid = db_result(db_query("SELECT fid FROM {form_manager_forms} WHERE form_id='%s'", $values['formid'])); 411 $form_cache = db_fetch_object(db_query("SELECT * FROM {form_builder_cache} WHERE sid='%s' AND form_id='%s'", $session, $values['formid'])); 412 413 // Rename the element keys if they have been explicitely defined in the Form Builder edit page. 414 $form = unserialize($form_cache->data); 415 form_manager_reset_element_keys($form); 416 foreach (element_children($form) as $key) { 417 if (is_array($form[$key])) { 418 form_manager_reset_element_keys($form[$key]); 419 } 420 } 421 $form_cache->data = serialize($form); 422 423 // We don't care about session ID since the form will be saved permanently and not cached by session. Chuck it. 424 unset($form_cache->sid); 425 426 // Update the timestamp on the form. 427 $form_cache->updated = time(); 428 429 // We need to know if this is a new form or if we're updating an existing form so that drupal_write_record 430 // can do the appropriate thing to save it. 431 $update = array(); 432 if ($fid) { 433 $form_cache->fid = $fid; 434 $update[] = 'fid'; 435 } 436 drupal_write_record('form_manager_forms', $form_cache, $update); 437 438 // Remove the form_builder_cache form (all for this session ID only) 439 db_query("DELETE FROM {form_builder_cache} WHERE sid='%s' AND form_id='%s'", $session, $values['formid']); 440 441 // redirect to admin page 442 drupal_goto('admin/build/forms'); 443 } 444 445 446 /** 447 * confirm_delete function 448 * Confirm that the admin wants to delete the given form 449 */ 450 function form_manager_confirm_delete(&$form_state) { 451 $form_id = check_plain(arg(4)); 452 $form_data = array( 453 'form_id' => $form_id, 454 'database' => db_result(db_query("SELECT fid FROM {form_manager_forms} WHERE form_id='%s'", $form_id)), 455 'fbcache' => db_result(db_query("SELECT form_id FROM {form_builder_cache} WHERE sid='%s' AND form_id='%s'", session_id(), $form_id)), 456 ); 457 458 if (!empty($form_data['database']) || !empty($form_data['fbcache'])) { 459 $form_state['storage']['form_data'] = $form_data; 460 $form = array(); 461 return confirm_form( 462 $form, 463 t('Are you sure you want to delete the form %form_id?', array('%form_id' => $form_id)), 464 'admin/build/forms', 465 t('You are about to delete the delete the form %form_id. This action cannot be undone.', array('%form_id' => $form_id)), t('Delete') 466 ); 467 } 468 else { 469 drupal_set_message(t('That form does not exist in the database.'), $type = 'error', FALSE); 470 drupal_goto('admin/build/forms'); 471 } 472 } 473 474 /** 475 * Form submit handler for the confirm delete form. 476 * Handles deletion of the specified form from the database. 477 */ 478 function form_manager_confirm_delete_submit($form, &$form_state) { 479 $form_data = $form_state['storage']['form_data']; 480 481 if (!empty($form_data['database'])) { 482 db_query("DELETE FROM {form_manager_forms} WHERE form_id='%s'", $form_data['form_id']); 483 } 484 485 if (!empty($form_data['fbcache'])) { 486 db_query("DELETE FROM {form_builder_cache} WHERE sid='%s' AND form_id='%s'", session_id(), $form_data['form_id']); 487 } 488 489 drupal_set_message(t('The form %formid has been deleted from the database.', array('%formid' => $form_data['form_id'])), $type = 'status', FALSE); 490 drupal_goto('admin/build/forms'); 491 } 492 493 494 /** 495 * Form function to allow admin users to create a new form from scratch. 496 */ 497 function form_manager_add() { 498 $form = array(); 499 500 // use 'formid' instead of 'form_id' in order to not confuse it with *this* form's form_id 501 $form['formid'] = array( 502 '#type' => 'textfield', 503 '#title' => t('Form ID'), 504 '#description' => t('This is the internal form_id of the form that will be used to uniquely identify it. <strong>Only letters, numbers, and underscores are allowed.</strong>'), 505 '#required' => TRUE, 506 ); 507 508 $form['path'] = array( 509 '#type' => 'textfield', 510 '#title' => t('Path'), 511 '#description' => t('The path to the page that will display the form. <em>ex. form/form_id</em>'), 512 '#required' => TRUE, 513 ); 514 515 $form['published'] = array( 516 '#type' => 'checkbox', 517 '#title' => t('Published?'), 518 '#description' => t('Whether or not the form is published. If it is not published, then only users with the <em>administer form_manager forms</em> will be able to view it. <strong>NOTE:</strong> This is not currently used. Future versions will allow you to enable/disable forms, but this functionality does not yet exist.'), 519 '#default_value' => TRUE, 520 ); 521 522 $form['multistep'] = array( 523 '#type' => 'checkbox', 524 '#title' => t('Multi-Step?'), 525 '#description' => t('Whether or not the form is multi-step. Top-level fieldsets will be used to determine "pages" of form elements. <strong>NOTE:</strong> this is not currently implemented; all forms with fieldsets are currently assumed to be multi-step forms. This will change in future versions, and this checkbox is here to provide future compatability.'), 526 '#default_value' => TRUE, 527 ); 528 529 $form['submit'] = array( 530 '#type' => 'submit', 531 '#value' => t('Create form'), 532 ); 533 534 return $form; 535 } 536 537 /** 538 * @todo Ensure that the form_id submitted doesn't exist already. 539 * - as a file-based form 540 * - in the database or in Form Builder cache 541 * - as a hook or form from another module 542 */ 543 function form_manager_add_validate($form, &$form_state) { 544 // TODO: this function 545 } 546 547 548 /** 549 * Submit handler for add form function 550 */ 551 function form_manager_add_submit($form, &$form_state) { 552 // Save settings in the form_manager_settings table 553 $form_data = array( 554 'form_id' => $form_state['values']['formid'], 555 'path' => $form_state['values']['path'], 556 'status' => $form_state['values']['published'], 557 'multistep' => $form_state['values']['multistep'], 558 'update' => time(), 559 ); 560 561 // TODO: Save the data in the form_manager_settings table, table needs to be added to hook_schema 562 //$form_data = (object) $form_data; 563 drupal_write_record('form_manager_settings', $form_data); 564 565 // Make sure the new form's path will be available 566 // TODO: I don't like rebuilding the menu_router table for the simple addition of a new callback function path. 567 // Find a better way to do this, like inserting it as a single row in {menu_router}. 568 menu_router_build(); 569 570 // Send the user to the Form Builder edit page 571 drupal_goto('admin/build/forms/edit/' . $form_data['form_id']); 572 } 573 574 function form_manager_admin_exportable($formid = FALSE) { 575 $output = ''; 576 577 ctools_include('export'); 578 $result = ctools_export_load_object('form_manager_forms', 'conditions', array('form_id' => $formid)); 579 drupal_set_title(check_plain($result->description)); 580 $code = ctools_export_object('form_manager_forms', array_shift($result), ''); 581 $lines = substr_count($code, "\n"); 582 583 return drupal_get_form('ctools_export_form', $code, 'Form Manager form'); 584 } 585 586 587 function form_manager_admin_import() { 588 $form['formid'] = array( 589 '#type' => 'textfield', 590 '#title' => t('Form name'), 591 '#description' => t('Enter the name of the new form. This is optional and is not necessary if you do not wish to rename the object.'), 592 ); 593 594 $form['object'] = array( 595 '#type' => 'textarea', 596 '#title' => t('Paste form code here'), 597 '#rows' => 15, 598 ); 599 600 $form['submit'] = array( 601 '#value' => t('Import'), 602 '#type' => 'submit', 603 ); 604 605 return $form; 606 } 607 608 /** 609 * Make sure that an import actually provides a handler. 610 */ 611 function form_manager_admin_import_validate(&$form, &$form_state) { 612 // First, run the PHP and turn the input code into an object. 613 ob_start(); 614 eval($form_state['values']['object']); 615 ob_end_clean(); 616 617 // The object should appear as $formmanager. This was the "identifier" set in the export section of the schema. 618 if (empty($formmanager)) { 619 $errors = ob_contents(); 620 if (empty($errors)) { 621 $errors = t('No formmanager object found.'); 622 } 623 form_error($form['object'], t('Unable to get a formmanager from the import. Errors reported: @errors', array('@errors' => $errors))); 624 } 625 626 $formmanager->data = str_replace("\r\n", ' ', $formmanager->data); 627 628 // make sure we're not importing an existing form 629 if (!empty($form_state['values']['formid'])) { 630 $formmanager->form_id = $form_state['values']['formid']; 631 } 632 if (form_manager_formid_exists($formmanager->form_id)) { 633 $errors = t('That form_id @formid already exists.', array('@formid' => $formmanager->form_id)); 634 form_error($form['object'], t('Unable to get a formmanager object from the import. Errors reported: @errors', array('@errors' => $errors))); 635 } 636 637 $form_state['obj'] = $formmanager; 638 } 639 640 /** 641 * Save the imported object. 642 */ 643 function form_manager_admin_import_submit(&$form, &$form_state) { 644 $formmanager = $form_state['obj']; 645 646 if (!empty($form_state['values']['name'])) { 647 $formmanager->form_id = $form_state['values']['name']; 648 } 649 650 form_manager_admin_import_save($formmanager); 651 $form_state['redirect'] = 'admin/build/forms/edit/' . $formmanager->form_id; 652 } 653 654 function form_manager_admin_import_save($formmanager) { 655 $update = array(); 656 if (!empty($formmanager->name)) { 657 if (form_manager_formid_exists($formmanager->form_id)) { 658 $update[] = 'fid'; 659 } 660 } 661 662 drupal_write_record('form_manager_forms', $formmanager, $update); 663 } 664 665 function form_manager_formid_exists($formid) { 666 $fid = db_result(db_query("SELECT fid FROM {form_manager_forms} WHERE form_id='%s'", $formid)); 667 668 if (!empty($fid)) { 669 return 'fid'; 670 } 671 return FALSE; 672 } 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
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 |