00001 <?php
00002
00003
00069 function drupal_get_form($form_id) {
00070 $form_state = array('storage' => NULL, 'submitted' => FALSE);
00071
00072 $args = func_get_args();
00073 $cacheable = FALSE;
00074
00075 if (isset($_SESSION['batch_form_state'])) {
00076
00077
00078
00079 $form_state = $_SESSION['batch_form_state'];
00080 unset($_SESSION['batch_form_state']);
00081 }
00082 else {
00083
00084
00085
00086
00087
00088 if (isset($_POST['form_id']) && $_POST['form_id'] == $form_id && !empty($_POST['form_build_id'])) {
00089 $form = form_get_cache($_POST['form_build_id'], $form_state);
00090 }
00091
00092
00093
00094
00095 if (!isset($form)) {
00096 $form_state['post'] = $_POST;
00097
00098 $args_temp = $args;
00099 $args_temp[0] = &$form_state;
00100 array_unshift($args_temp, $form_id);
00101
00102 $form = call_user_func_array('drupal_retrieve_form', $args_temp);
00103 $form_build_id = 'form-' . md5(mt_rand());
00104 $form['#build_id'] = $form_build_id;
00105 drupal_prepare_form($form_id, $form, $form_state);
00106
00107
00108 $original_form = $form;
00109 $cacheable = TRUE;
00110 unset($form_state['post']);
00111 }
00112 $form['#post'] = $_POST;
00113
00114
00115
00116
00117
00118
00119 drupal_process_form($form_id, $form, $form_state);
00120 if ($cacheable && !empty($form['#cache'])) {
00121
00122
00123
00124 form_set_cache($form_build_id, $original_form, NULL);
00125 }
00126 }
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142 if (!empty($form_state['rebuild']) || !empty($form_state['storage'])) {
00143 $form = drupal_rebuild_form($form_id, $form_state, $args);
00144 }
00145
00146
00147
00148 return drupal_render_form($form_id, $form);
00149 }
00150
00188 function drupal_rebuild_form($form_id, &$form_state, $args, $form_build_id = NULL) {
00189
00190
00191
00192 $args[0] = &$form_state;
00193
00194 array_unshift($args, $form_id);
00195 $form = call_user_func_array('drupal_retrieve_form', $args);
00196
00197 if (!isset($form_build_id)) {
00198
00199 $form_build_id = 'form-' . md5(mt_rand());
00200 }
00201 $form['#build_id'] = $form_build_id;
00202 drupal_prepare_form($form_id, $form, $form_state);
00203
00204
00205
00206
00207 form_set_cache($form_build_id, $form, $form_state);
00208
00209
00210
00211
00212 $_POST = array();
00213 $form['#post'] = array();
00214 drupal_process_form($form_id, $form, $form_state);
00215 return $form;
00216 }
00217
00221 function form_get_cache($form_build_id, &$form_state) {
00222 if ($cached = cache_get('form_' . $form_build_id, 'cache_form')) {
00223 $form = $cached->data;
00224 if ($cached = cache_get('storage_' . $form_build_id, 'cache_form')) {
00225 $form_state['storage'] = $cached->data;
00226 }
00227 return $form;
00228 }
00229 }
00230
00234 function form_set_cache($form_build_id, $form, $form_state) {
00235
00236 $expire = 21600;
00237
00238 cache_set('form_' . $form_build_id, $form, 'cache_form', time() + $expire);
00239 if (!empty($form_state['storage'])) {
00240 cache_set('storage_' . $form_build_id, $form_state['storage'], 'cache_form', time() + $expire);
00241 }
00242 }
00243
00286 function drupal_execute($form_id, &$form_state) {
00287 $args = func_get_args();
00288 $form = call_user_func_array('drupal_retrieve_form', $args);
00289 $form['#post'] = $form_state['values'];
00290 drupal_prepare_form($form_id, $form, $form_state);
00291 drupal_process_form($form_id, $form, $form_state);
00292 }
00293
00313 function drupal_retrieve_form($form_id, &$form_state) {
00314 static $forms;
00315
00316
00317
00318
00319
00320
00321 $args = func_get_args();
00322 $saved_args = $args;
00323 array_shift($args);
00324 if (isset($form_state)) {
00325 array_shift($args);
00326 }
00327
00328
00329
00330 if (!drupal_function_exists($form_id)) {
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342 if (!isset($forms) || !isset($forms[$form_id])) {
00343 $forms = module_invoke_all('forms', $form_id, $args);
00344 }
00345 $form_definition = $forms[$form_id];
00346 if (isset($form_definition['callback arguments'])) {
00347 $args = array_merge($form_definition['callback arguments'], $args);
00348 }
00349 if (isset($form_definition['callback'])) {
00350 $callback = $form_definition['callback'];
00351 drupal_function_exists($callback);
00352 }
00353 }
00354
00355 array_unshift($args, NULL);
00356 $args[0] = &$form_state;
00357
00358
00359
00360 $form = call_user_func_array(isset($callback) ? $callback : $form_id, $args);
00361
00362
00363
00364
00365
00366 $form['#parameters'] = $saved_args;
00367 return $form;
00368 }
00369
00385 function drupal_process_form($form_id, &$form, &$form_state) {
00386 $form_state['values'] = array();
00387
00388 $form = form_builder($form_id, $form, $form_state);
00389
00390
00391 if ((!empty($form['#programmed'])) || (!empty($form['#post']) && (isset($form['#post']['form_id']) && ($form['#post']['form_id'] == $form_id)))) {
00392 drupal_validate_form($form_id, $form, $form_state);
00393
00394
00395
00396
00397
00398
00399 form_clean_id(NULL, TRUE);
00400
00401 if ((!empty($form_state['submitted'])) && !form_get_errors() && empty($form_state['rebuild'])) {
00402 $form_state['redirect'] = NULL;
00403 form_execute_handlers('submit', $form, $form_state);
00404
00405
00406
00407
00408 if (variable_get('cache', CACHE_DISABLED) == CACHE_DISABLED && !empty($form_state['values']['form_build_id'])) {
00409 cache_clear_all('form_' . $form_state['values']['form_build_id'], 'cache_form');
00410 cache_clear_all('storage_' . $form_state['values']['form_build_id'], 'cache_form');
00411 }
00412
00413
00414
00415
00416
00417 if ($batch =& batch_get() && !isset($batch['current_set'])) {
00418
00419
00420 $batch['form'] = $form;
00421 $batch['form_state'] = $form_state;
00422 $batch['progressive'] = !$form['#programmed'];
00423 batch_process();
00424
00425
00426
00427
00428 }
00429
00430
00431
00432
00433
00434
00435
00436
00437 if (!$form['#programmed'] && empty($form_state['rebuild']) && empty($form_state['storage'])) {
00438 drupal_redirect_form($form, $form_state['redirect']);
00439 }
00440 }
00441 }
00442 }
00443
00458 function drupal_prepare_form($form_id, &$form, &$form_state) {
00459 global $user;
00460
00461 $form['#type'] = 'form';
00462 $form['#programmed'] = isset($form['#post']);
00463
00464 if (isset($form['#build_id'])) {
00465 $form['form_build_id'] = array(
00466 '#type' => 'hidden',
00467 '#value' => $form['#build_id'],
00468 '#id' => $form['#build_id'],
00469 '#name' => 'form_build_id',
00470 );
00471 }
00472
00473
00474
00475
00476
00477 if (isset($form['#token'])) {
00478 if ($form['#token'] === FALSE || $user->uid == 0 || $form['#programmed']) {
00479 unset($form['#token']);
00480 }
00481 else {
00482 $form['form_token'] = array('#type' => 'token', '#default_value' => drupal_get_token($form['#token']));
00483 }
00484 }
00485 else if (isset($user->uid) && $user->uid && !$form['#programmed']) {
00486 $form['#token'] = $form_id;
00487 $form['form_token'] = array(
00488 '#id' => form_clean_id('edit-' . $form_id . '-form-token'),
00489 '#type' => 'token',
00490 '#default_value' => drupal_get_token($form['#token']),
00491 );
00492 }
00493
00494 if (isset($form_id)) {
00495 $form['form_id'] = array(
00496 '#type' => 'hidden',
00497 '#value' => $form_id,
00498 '#id' => form_clean_id("edit-$form_id"),
00499 );
00500 }
00501 if (!isset($form['#id'])) {
00502 $form['#id'] = form_clean_id($form_id);
00503 }
00504
00505 $form += _element_info('form');
00506
00507 if (!isset($form['#validate'])) {
00508 if (drupal_function_exists($form_id . '_validate')) {
00509 $form['#validate'] = array($form_id . '_validate');
00510 }
00511 }
00512
00513 if (!isset($form['#submit'])) {
00514 if (drupal_function_exists($form_id . '_submit')) {
00515
00516 $form['#submit'] = array($form_id . '_submit');
00517 }
00518 }
00519
00520
00521
00522
00523
00524
00525
00526 $data = &$form;
00527 $data['__drupal_alter_by_ref'] = array(&$form_state);
00528 drupal_alter('form_' . $form_id, $data);
00529
00530
00531
00532 $data['__drupal_alter_by_ref'] = array(&$form_state);
00533 drupal_alter('form', $data, $form_id);
00534 }
00535
00536
00557 function drupal_validate_form($form_id, $form, &$form_state) {
00558 static $validated_forms = array();
00559
00560 if (isset($validated_forms[$form_id])) {
00561 return;
00562 }
00563
00564
00565
00566 if (isset($form['#token'])) {
00567 if (!drupal_valid_token($form_state['values']['form_token'], $form['#token'])) {
00568
00569 form_set_error('form_token', t('Validation error, please try again. If this error persists, please contact the site administrator.'));
00570 }
00571 }
00572
00573 _form_validate($form, $form_state, $form_id);
00574 $validated_forms[$form_id] = TRUE;
00575 }
00576
00589 function drupal_render_form($form_id, &$form) {
00590
00591 if (!isset($form['#theme'])) {
00592 init_theme();
00593 $registry = theme_get_registry();
00594 if (isset($registry[$form_id])) {
00595 $form['#theme'] = $form_id;
00596 }
00597 }
00598
00599 $output = drupal_render($form);
00600 return $output;
00601 }
00602
00612 function drupal_redirect_form($form, $redirect = NULL) {
00613 $goto = NULL;
00614 if (isset($redirect)) {
00615 $goto = $redirect;
00616 }
00617 if ($goto !== FALSE && isset($form['#redirect'])) {
00618 $goto = $form['#redirect'];
00619 }
00620 if (!isset($goto) || ($goto !== FALSE)) {
00621 if (isset($goto)) {
00622 if (is_array($goto)) {
00623 call_user_func_array('drupal_goto', $goto);
00624 }
00625 else {
00626 drupal_goto($goto);
00627 }
00628 }
00629 drupal_goto($_GET['q']);
00630 }
00631 }
00632
00654 function _form_validate($elements, &$form_state, $form_id = NULL) {
00655 static $complete_form;
00656
00657
00658 $t = get_t();
00659
00660
00661 foreach (element_children($elements) as $key) {
00662 if (isset($elements[$key]) && $elements[$key]) {
00663 _form_validate($elements[$key], $form_state);
00664 }
00665 }
00666
00667 if (!isset($elements['#validated']) || !$elements['#validated']) {
00668 if (isset($elements['#needs_validation'])) {
00669
00670
00671
00672
00673 if ($elements['#required'] && (!count($elements['#value']) || (is_string($elements['#value']) && strlen(trim($elements['#value'])) == 0))) {
00674 form_error($elements, $t('!name field is required.', array('!name' => $elements['#title'])));
00675 }
00676
00677
00678 if (isset($elements['#maxlength']) && drupal_strlen($elements['#value']) > $elements['#maxlength']) {
00679 form_error($elements, $t('!name cannot be longer than %max characters but is currently %length characters long.', array('!name' => empty($elements['#title']) ? $elements['#parents'][0] : $elements['#title'], '%max' => $elements['#maxlength'], '%length' => drupal_strlen($elements['#value']))));
00680 }
00681
00682 if (isset($elements['#options']) && isset($elements['#value'])) {
00683 if ($elements['#type'] == 'select') {
00684 $options = form_options_flatten($elements['#options']);
00685 }
00686 else {
00687 $options = $elements['#options'];
00688 }
00689 if (is_array($elements['#value'])) {
00690 $value = $elements['#type'] == 'checkboxes' ? array_keys(array_filter($elements['#value'])) : $elements['#value'];
00691 foreach ($value as $v) {
00692 if (!isset($options[$v])) {
00693 form_error($elements, $t('An illegal choice has been detected. Please contact the site administrator.'));
00694 watchdog('form', 'Illegal choice %choice in !name element.', array('%choice' => $v, '!name' => empty($elements['#title']) ? $elements['#parents'][0] : $elements['#title']), WATCHDOG_ERROR);
00695 }
00696 }
00697 }
00698 elseif (!isset($options[$elements['#value']])) {
00699 form_error($elements, $t('An illegal choice has been detected. Please contact the site administrator.'));
00700 watchdog('form', 'Illegal choice %choice in %name element.', array('%choice' => $elements['#value'], '%name' => empty($elements['#title']) ? $elements['#parents'][0] : $elements['#title']), WATCHDOG_ERROR);
00701 }
00702 }
00703 }
00704
00705
00706
00707
00708 if (isset($form_id)) {
00709 form_execute_handlers('validate', $elements, $form_state);
00710 $complete_form = $elements;
00711 }
00712
00713
00714 elseif (isset($elements['#element_validate'])) {
00715 foreach ($elements['#element_validate'] as $function) {
00716 if (drupal_function_exists($function)) {
00717 $function($elements, $form_state, $complete_form);
00718 }
00719 }
00720 }
00721 $elements['#validated'] = TRUE;
00722 }
00723 }
00724
00740 function form_execute_handlers($type, &$form, &$form_state) {
00741 $return = FALSE;
00742 if (isset($form_state[$type . '_handlers'])) {
00743 $handlers = $form_state[$type . '_handlers'];
00744 }
00745 elseif (isset($form['#' . $type])) {
00746 $handlers = $form['#' . $type];
00747 }
00748 else {
00749 $handlers = array();
00750 }
00751
00752 foreach ($handlers as $function) {
00753 if (drupal_function_exists($function)) {
00754 if ($type == 'submit' && ($batch =& batch_get())) {
00755
00756
00757
00758 $batch['sets'][] = array('form_submit' => $function);
00759 }
00760 else {
00761 $function($form, $form_state);
00762 }
00763 $return = TRUE;
00764 }
00765 }
00766 return $return;
00767 }
00768
00783 function form_set_error($name = NULL, $message = '') {
00784 static $form = array();
00785 if (isset($name) && !isset($form[$name])) {
00786 $form[$name] = $message;
00787 if ($message) {
00788 drupal_set_message($message, 'error');
00789 }
00790 }
00791 return $form;
00792 }
00793
00797 function form_get_errors() {
00798 $form = form_set_error();
00799 if (!empty($form)) {
00800 return $form;
00801 }
00802 }
00803
00807 function form_get_error($element) {
00808 $form = form_set_error();
00809 $key = $element['#parents'][0];
00810 if (isset($form[$key])) {
00811 return $form[$key];
00812 }
00813 $key = implode('][', $element['#parents']);
00814 if (isset($form[$key])) {
00815 return $form[$key];
00816 }
00817 }
00818
00822 function form_error(&$element, $message = '') {
00823 form_set_error(implode('][', $element['#parents']), $message);
00824 }
00825
00842 function form_builder($form_id, $form, &$form_state) {
00843 static $complete_form, $cache;
00844
00845
00846 $form['#processed'] = FALSE;
00847
00848
00849 if ((!empty($form['#type'])) && ($info = _element_info($form['#type']))) {
00850
00851 $form += $info;
00852 }
00853
00854 if (isset($form['#type']) && $form['#type'] == 'form') {
00855 $complete_form = $form;
00856 if (!empty($form['#programmed'])) {
00857 $form_state['submitted'] = TRUE;
00858 }
00859 }
00860
00861 if (isset($form['#input']) && $form['#input']) {
00862 _form_builder_handle_input_element($form_id, $form, $form_state, $complete_form);
00863 }
00864 $form['#defaults_loaded'] = TRUE;
00865
00866
00867 $form['#sorted'] = TRUE;
00868
00869
00870 $count = 0;
00871 foreach (element_children($form) as $key) {
00872 $form[$key]['#post'] = $form['#post'];
00873 $form[$key]['#programmed'] = $form['#programmed'];
00874
00875 if (!isset($form[$key]['#tree'])) {
00876 $form[$key]['#tree'] = $form['#tree'];
00877 }
00878
00879
00880 if (isset($form['#access']) && !$form['#access']) {
00881 $form[$key]['#access'] = FALSE;
00882 }
00883
00884
00885 if (!isset($form[$key]['#parents'])) {
00886
00887
00888 $form[$key]['#parents'] = $form[$key]['#tree'] && $form['#tree'] ? array_merge($form['#parents'], array($key)) : array($key);
00889 $array_parents = isset($form['#array_parents']) ? $form['#array_parents'] : array();
00890 $array_parents[] = $key;
00891 $form[$key]['#array_parents'] = $array_parents;
00892 }
00893
00894
00895 if (!isset($form[$key]['#weight'])) {
00896 $form[$key]['#weight'] = $count/1000;
00897 }
00898 else {
00899
00900
00901 unset($form['#sorted']);
00902 }
00903 $form[$key] = form_builder($form_id, $form[$key], $form_state);
00904 $count++;
00905 }
00906
00907
00908
00909 if (isset($form['#after_build']) && !isset($form['#after_build_done'])) {
00910 foreach ($form['#after_build'] as $function) {
00911 $form = $function($form, $form_state);
00912 $form['#after_build_done'] = TRUE;
00913 }
00914 }
00915
00916
00917
00918 _form_builder_ie_cleanup($form, $form_state);
00919
00920
00921
00922
00923
00924 if (!empty($form_state['submitted'])) {
00925 unset($form_state['buttons']);
00926 }
00927
00928
00929
00930 if (isset($form['#cache'])) {
00931 $cache = $form['#cache'];
00932 }
00933
00934 if (isset($form['#type']) && $form['#type'] == 'form' && isset($cache)) {
00935 $form['#cache'] = TRUE;
00936 }
00937 return $form;
00938 }
00939
00945 function _form_builder_handle_input_element($form_id, &$form, &$form_state, $complete_form) {
00946 if (!isset($form['#name'])) {
00947 $name = array_shift($form['#parents']);
00948 $form['#name'] = $name;
00949 if ($form['#type'] == 'file') {
00950
00951
00952
00953 $form['#name'] = 'files[' . $form['#name'] . ']';
00954 }
00955 elseif (count($form['#parents'])) {
00956 $form['#name'] .= '[' . implode('][', $form['#parents']) . ']';
00957 }
00958 array_unshift($form['#parents'], $name);
00959 }
00960 if (!isset($form['#id'])) {
00961 $form['#id'] = form_clean_id('edit-' . implode('-', $form['#parents']));
00962 }
00963
00964 unset($edit);
00965 if (!empty($form['#disabled'])) {
00966 $form['#attributes']['disabled'] = 'disabled';
00967 }
00968
00969 if (!isset($form['#value']) && !array_key_exists('#value', $form)) {
00970 $function = !empty($form['#value_callback']) ? $form['#value_callback'] : 'form_type_' . $form['#type'] . '_value';
00971 if (($form['#programmed']) || ((!isset($form['#access']) || $form['#access']) && isset($form['#post']) && (isset($form['#post']['form_id']) && $form['#post']['form_id'] == $form_id))) {
00972 $edit = $form['#post'];
00973 foreach ($form['#parents'] as $parent) {
00974 $edit = isset($edit[$parent]) ? $edit[$parent] : NULL;
00975 }
00976 if (!$form['#programmed'] || isset($edit)) {
00977
00978 if (function_exists($function)) {
00979 $form['#value'] = $function($form, $edit);
00980 }
00981 if (!isset($form['#value']) && isset($edit)) {
00982 $form['#value'] = $edit;
00983 }
00984 }
00985
00986 if (isset($form['#value']) || (isset($form['#required']) && $form['#required'])) {
00987 $form['#needs_validation'] = TRUE;
00988 }
00989 }
00990
00991 if (!isset($form['#value'])) {
00992
00993 if (function_exists($function)) {
00994 $form['#value'] = $function($form);
00995 }
00996
00997
00998
00999 if (!isset($form['#value']) && empty($form['#has_garbage_value'])) {
01000 $form['#value'] = isset($form['#default_value']) ? $form['#default_value'] : '';
01001 }
01002 }
01003 }
01004
01005
01006
01007
01008
01009 if (!empty($form['#post']) && isset($form['#executes_submit_callback'])) {
01010
01011
01012 $button_type = $form['#executes_submit_callback'] ? 'submit' : 'button';
01013 $form_state['buttons'][$button_type][] = $form;
01014
01015 if (_form_button_was_clicked($form)) {
01016 $form_state['submitted'] = $form_state['submitted'] || $form['#executes_submit_callback'];
01017
01018
01019
01020
01021 $form_state['values'][$form['#name']] = $form['#value'];
01022 $form_state['clicked_button'] = $form;
01023
01024 if (isset($form['#validate'])) {
01025 $form_state['validate_handlers'] = $form['#validate'];
01026 }
01027 if (isset($form['#submit'])) {
01028 $form_state['submit_handlers'] = $form['#submit'];
01029 }
01030 }
01031 }
01032
01033
01034 if (isset($form['#process']) && !$form['#processed']) {
01035 foreach ($form['#process'] as $process) {
01036 if (drupal_function_exists($process)) {
01037 $form = $process($form, isset($edit) ? $edit : NULL, $form_state, $complete_form);
01038 }
01039 }
01040 $form['#processed'] = TRUE;
01041 }
01042 form_set_value($form, $form['#value'], $form_state);
01043 }
01044
01054 function _form_button_was_clicked($form) {
01055
01056
01057
01058
01059
01060 if (isset($form['#post'][$form['#name']]) && $form['#post'][$form['#name']] == $form['#value']) {
01061 return TRUE;
01062 }
01063
01064
01065
01066
01067
01068 elseif (!empty($form['#has_garbage_value']) && isset($form['#value']) && $form['#value'] !== '') {
01069 return TRUE;
01070 }
01071 return FALSE;
01072 }
01073
01082 function _form_builder_ie_cleanup($form, &$form_state) {
01083
01084
01085 if (!empty($form['#type']) && $form['#type'] == 'form') {
01086
01087
01088
01089 if (empty($form_state['submitted']) && !empty($form_state['buttons']['submit']) && empty($form_state['buttons']['button'])) {
01090 $button = $form_state['buttons']['submit'][0];
01091
01092
01093
01094 $form_state['submitted'] = TRUE;
01095 $form_state['submit_handlers'] = empty($button['#submit']) ? NULL : $button['#submit'];
01096 $form_state['validate_handlers'] = empty($button['#validate']) ? NULL : $button['#validate'];
01097 $form_state['values'][$button['#name']] = $button['#value'];
01098 $form_state['clicked_button'] = $button;
01099 }
01100 }
01101 }
01102
01115 function form_type_image_button_value($form, $edit = FALSE) {
01116 if ($edit !== FALSE) {
01117 if (!empty($edit)) {
01118
01119
01120 return $form['#return_value'];
01121 }
01122 else {
01123
01124
01125
01126
01127
01128 $post = $form['#post'];
01129 foreach (split('\[', $form['#name']) as $element_name) {
01130
01131 if (substr($element_name, -1) == ']') {
01132 $element_name = substr($element_name, 0, -1);
01133 }
01134
01135 if (!isset($post[$element_name])) {
01136 if (isset($post[$element_name . '_x'])) {
01137 return $form['#return_value'];
01138 }
01139 return NULL;
01140 }
01141 $post = $post[$element_name];
01142 }
01143 return $form['#return_value'];
01144 }
01145 }
01146 }
01147
01160 function form_type_checkbox_value($form, $edit = FALSE) {
01161 if ($edit !== FALSE) {
01162 return !empty($edit) ? $form['#return_value'] : 0;
01163 }
01164 }
01165
01178 function form_type_checkboxes_value($form, $edit = FALSE) {
01179 if ($edit === FALSE) {
01180 $value = array();
01181 $form += array('#default_value' => array());
01182 foreach ($form['#default_value'] as $key) {
01183 $value[$key] = 1;
01184 }
01185 return $value;
01186 }
01187 elseif (!isset($edit)) {
01188 return array();
01189 }
01190 }
01191
01205 function form_type_password_confirm_value($form, $edit = FALSE) {
01206 if ($edit === FALSE) {
01207 $form += array('#default_value' => array());
01208 return $form['#default_value'] + array('pass1' => '', 'pass2' => '');
01209 }
01210 }
01211
01224 function form_type_select_value($form, $edit = FALSE) {
01225 if ($edit !== FALSE) {
01226 if (isset($form['#multiple']) && $form['#multiple']) {
01227 return (is_array($edit)) ? drupal_map_assoc($edit) : array();
01228 }
01229 else {
01230 return $edit;
01231 }
01232 }
01233 }
01234
01247 function form_type_textfield_value($form, $edit = FALSE) {
01248 if ($edit !== FALSE) {
01249
01250
01251 return str_replace(array("\r", "\n"), '', $edit);
01252 }
01253 }
01254
01267 function form_type_token_value($form, $edit = FALSE) {
01268 if ($edit !== FALSE) {
01269 return (string)$edit;
01270 }
01271 }
01272
01296 function form_set_value($form_item, $value, &$form_state) {
01297 _form_set_value($form_state['values'], $form_item, $form_item['#parents'], $value);
01298 }
01299
01307 function _form_set_value(&$form_values, $form_item, $parents, $value) {
01308 $parent = array_shift($parents);
01309 if (empty($parents)) {
01310 $form_values[$parent] = $value;
01311 }
01312 else {
01313 if (!isset($form_values[$parent])) {
01314 $form_values[$parent] = array();
01315 }
01316 _form_set_value($form_values[$parent], $form_item, $parents, $value);
01317 }
01318 }
01319
01323 function _element_info($type, $refresh = NULL) {
01324 static $cache;
01325
01326 $basic_defaults = array(
01327 '#description' => NULL,
01328 '#attributes' => array(),
01329 '#required' => FALSE,
01330 '#tree' => FALSE,
01331 '#parents' => array()
01332 );
01333 if (!isset($cache) || $refresh) {
01334 $cache = array();
01335 foreach (module_implements('elements') as $module) {
01336 $elements = module_invoke($module, 'elements');
01337 if (isset($elements) && is_array($elements)) {
01338 $cache = array_merge_recursive($cache, $elements);
01339 }
01340 }
01341 if (sizeof($cache)) {
01342 foreach ($cache as $element_type => $info) {
01343 $cache[$element_type] = array_merge_recursive($basic_defaults, $info);
01344 }
01345 }
01346 }
01347
01348 return $cache[$type];
01349 }
01350
01351 function form_options_flatten($array, $reset = TRUE) {
01352 static $return;
01353
01354 if ($reset) {
01355 $return = array();
01356 }
01357
01358 foreach ($array as $key => $value) {
01359 if (is_object($value)) {
01360 form_options_flatten($value->option, FALSE);
01361 }
01362 else if (is_array($value)) {
01363 form_options_flatten($value, FALSE);
01364 }
01365 else {
01366 $return[$key] = 1;
01367 }
01368 }
01369
01370 return $return;
01371 }
01372
01388 function theme_select($element) {
01389 $select = '';
01390 $size = $element['#size'] ? ' size="' . $element['#size'] . '"' : '';
01391 _form_set_class($element, array('form-select'));
01392 $multiple = $element['#multiple'];
01393 return theme('form_element', $element, '<select name="' . $element['#name'] . '' . ($multiple ? '[]' : '') . '"' . ($multiple ? ' multiple="multiple" ' : '') . drupal_attributes($element['#attributes']) . ' id="' . $element['#id'] . '" ' . $size . '>' . form_select_options($element) . '</select>');
01394 }
01395
01396 function form_select_options($element, $choices = NULL) {
01397 if (!isset($choices)) {
01398 $choices = $element['#options'];
01399 }
01400
01401
01402 $value_valid = isset($element['#value']) || array_key_exists('#value', $element);
01403 $value_is_array = is_array($element['#value']);
01404 $options = '';
01405 foreach ($choices as $key => $choice) {
01406 if (is_array($choice)) {
01407 $options .= '<optgroup label="' . $key . '">';
01408 $options .= form_select_options($element, $choice);
01409 $options .= '</optgroup>';
01410 }
01411 elseif (is_object($choice)) {
01412 $options .= form_select_options($element, $choice->option);
01413 }
01414 else {
01415 $key = (string)$key;
01416 if ($value_valid && (!$value_is_array && (string)$element['#value'] === $key || ($value_is_array && in_array($key, $element['#value'])))) {
01417 $selected = ' selected="selected"';
01418 }
01419 else {
01420 $selected = '';
01421 }
01422 $options .= '<option value="' . check_plain($key) . '"' . $selected . '>' . check_plain($choice) . '</option>';
01423 }
01424 }
01425 return $options;
01426 }
01427
01460 function form_get_options($element, $key) {
01461 $keys = array();
01462 foreach ($element['#options'] as $index => $choice) {
01463 if (is_array($choice)) {
01464 return FALSE;
01465 }
01466 else if (is_object($choice)) {
01467 if (isset($choice->option[$key])) {
01468 $keys[] = $index;
01469 }
01470 }
01471 else if ($index == $key) {
01472 $keys[] = $index;
01473 }
01474 }
01475 return $keys;
01476 }
01477
01489 function theme_fieldset($element) {
01490 if ($element['#collapsible']) {
01491 drupal_add_js('misc/collapse.js');
01492
01493 if (!isset($element['#attributes']['class'])) {
01494 $element['#attributes']['class'] = '';
01495 }
01496
01497 $element['#attributes']['class'] .= ' collapsible';
01498 if ($element['#collapsed']) {
01499 $element['#attributes']['class'] .= ' collapsed';
01500 }
01501 }
01502
01503 return '<fieldset' . drupal_attributes($element['#attributes']) . '>' . ($element['#title'] ? '<legend>' . $element['#title'] . '</legend>' : '') . (isset($element['#description']) && $element['#description'] ? '<div class="description">' . $element['#description'] . '</div>' : '') . (!empty($element['#children']) ? $element['#children'] : '') . $element['#value'] . "</fieldset>\n";
01504 }
01505
01517 function theme_radio($element) {
01518 _form_set_class($element, array('form-radio'));
01519 $output = '<input type="radio" ';
01520 $output .= 'name="' . $element['#name'] . '" ';
01521 $output .= 'value="' . $element['#return_value'] . '" ';
01522 $output .= (check_plain($element['#value']) == $element['#return_value']) ? ' checked="checked" ' : ' ';
01523 $output .= drupal_attributes($element['#attributes']) . ' />';
01524 if (!is_null($element['#title'])) {
01525 $output = '<label class="option">' . $output . ' ' . $element['#title'] . '</label>';
01526 }
01527
01528 unset($element['#title']);
01529 return theme('form_element', $element, $output);
01530 }
01531
01543 function theme_radios($element) {
01544 $class = 'form-radios';
01545 if (isset($element['#attributes']['class'])) {
01546 $class .= ' ' . $element['#attributes']['class'];
01547 }
01548 $element['#children'] = '<div class="' . $class . '">' . (!empty($element['#children']) ? $element['#children'] : '') . '</div>';
01549 if ($element['#title'] || $element['#description']) {
01550 unset($element['#id']);
01551 return theme('form_element', $element, $element['#children']);
01552 }
01553 else {
01554 return $element['#children'];
01555 }
01556 }
01557
01569 function theme_password_confirm($element) {
01570 return theme('form_element', $element, $element['#children']);
01571 }
01572
01576 function expand_password_confirm($element) {
01577 $element['pass1'] = array(
01578 '#type' => 'password',
01579 '#title' => t('Password'),
01580 '#value' => empty($element['#value']) ? NULL : $element['#value']['pass1'],
01581 '#required' => $element['#required'],
01582 '#attributes' => array('class' => 'password-field'),
01583 );
01584 $element['pass2'] = array(
01585 '#type' => 'password',
01586 '#title' => t('Confirm password'),
01587 '#value' => empty($element['#value']) ? NULL : $element['#value']['pass2'],
01588 '#required' => $element['#required'],
01589 '#attributes' => array('class' => 'password-confirm'),
01590 );
01591 $element['#element_validate'] = array('password_confirm_validate');
01592 $element['#tree'] = TRUE;
01593
01594 if (isset($element['#size'])) {
01595 $element['pass1']['#size'] = $element['pass2']['#size'] = $element['#size'];
01596 }
01597
01598 return $element;
01599 }
01600
01604 function password_confirm_validate($form, &$form_state) {
01605 $pass1 = trim($form['pass1']['#value']);
01606 if (!empty($pass1)) {
01607 $pass2 = trim($form['pass2']['#value']);
01608 if ($pass1 != $pass2) {
01609 form_error($form, t('The specified passwords do not match.'));
01610 }
01611 }
01612 elseif ($form['#required'] && !empty($form['#post'])) {
01613 form_error($form, t('Password field is required.'));
01614 }
01615
01616
01617
01618 form_set_value($form['pass1'], NULL, $form_state);
01619 form_set_value($form['pass2'], NULL, $form_state);
01620 form_set_value($form, $pass1, $form_state);
01621
01622 return $form;
01623
01624 }
01625
01637 function theme_date($element) {
01638 return theme('form_element', $element, '<div class="container-inline">' . $element['#children'] . '</div>');
01639 }
01640
01644 function expand_date($element) {
01645
01646 if (empty($element['#value'])) {
01647 $element['#value'] = array('day' => format_date(time(), 'custom', 'j'),
01648 'month' => format_date(time(), 'custom', 'n'),
01649 'year' => format_date(time(), 'custom', 'Y'));
01650 }
01651
01652 $element['#tree'] = TRUE;
01653
01654
01655 $format = variable_get('date_format_short', 'm/d/Y - H:i');
01656 $sort = array();
01657 $sort['day'] = max(strpos($format, 'd'), strpos($format, 'j'));
01658 $sort['month'] = max(strpos($format, 'm'), strpos($format, 'M'));
01659 $sort['year'] = strpos($format, 'Y');
01660 asort($sort);
01661 $order = array_keys($sort);
01662
01663
01664 foreach ($order as $type) {
01665 switch ($type) {
01666 case 'day':
01667 $options = drupal_map_assoc(range(1, 31));
01668 break;
01669 case 'month':
01670 $options = drupal_map_assoc(range(1, 12), 'map_month');
01671 break;
01672 case 'year':
01673 $options = drupal_map_assoc(range(1900, 2050));
01674 break;
01675 }
01676 $parents = $element['#parents'];
01677 $parents[] = $type;
01678 $element[$type] = array(
01679 '#type' => 'select',
01680 '#value' => $element['#value'][$type],
01681 '#attributes' => $element['#attributes'],
01682 '#options' => $options,
01683 );
01684 }
01685
01686 return $element;
01687 }
01688
01692 function date_validate($form) {
01693 if (!checkdate($form['#value']['month'], $form['#value']['day'], $form['#value']['year'])) {
01694 form_error($form, t('The specified date is invalid.'));
01695 }
01696 }
01697
01701 function map_month($month) {
01702 return format_date(gmmktime(0, 0, 0, $month, 2, 1970), 'custom', 'M', 0);
01703 }
01704
01708 function weight_value(&$form) {
01709 if (isset($form['#default_value'])) {
01710 $form['#value'] = $form['#default_value'];
01711 }
01712 else {
01713 $form['#value'] = 0;
01714 }
01715 }
01716
01721 function expand_radios($element) {
01722 if (count($element['#options']) > 0) {
01723 foreach ($element['#options'] as $key => $choice) {
01724 if (!isset($element[$key])) {
01725
01726
01727 $parents_for_id = array_merge($element['#parents'], array($key));
01728 $element[$key] = array(
01729 '#type' => 'radio',
01730 '#title' => $choice,
01731 '#return_value' => check_plain($key),
01732 '#default_value' => isset($element['#default_value']) ? $element['#default_value'] : NULL,
01733 '#attributes' => $element['#attributes'],
01734 '#parents' => $element['#parents'],
01735 '#id' => form_clean_id('edit-' . implode('-', $parents_for_id)),
01736 );
01737 }
01738 }
01739 }
01740 return $element;
01741 }
01742
01757 function form_expand_ahah($element) {
01758 static $js_added = array();
01759
01760 if (isset($element['#ahah']['path']) && !isset($element['#ahah']['event'])) {
01761 switch ($element['#type']) {
01762 case 'submit':
01763 case 'button':
01764 case 'image_button':
01765
01766
01767
01768 $element['#ahah']['event'] = 'mousedown';
01769
01770
01771 $element['#ahah']['keypress'] = TRUE;
01772 break;
01773 case 'password':
01774 case 'textfield':
01775 case 'textarea':
01776 $element['#ahah']['event'] = 'blur';
01777 break;
01778 case 'radio':
01779 case 'checkbox':
01780 case 'select':
01781 $element['#ahah']['event'] = 'change';
01782 break;
01783 }
01784 }
01785
01786
01787
01788 if (isset($element['#ahah']['path']) && isset($element['#ahah']['event']) && !isset($js_added[$element['#id']])) {
01789 drupal_add_js('misc/jquery.form.js');
01790 drupal_add_js('misc/ahah.js');
01791
01792 $ahah_binding = array(
01793 'url' => url($element['#ahah']['path']),
01794 'event' => $element['#ahah']['event'],
01795 'keypress' => empty($element['#ahah']['keypress']) ? NULL : $element['#ahah']['keypress'],
01796 'wrapper' => empty($element['#ahah']['wrapper']) ? NULL : $element['#ahah']['wrapper'],
01797 'selector' => empty($element['#ahah']['selector']) ? '#' . $element['#id'] : $element['#ahah']['selector'],
01798 'effect' => empty($element['#ahah']['effect']) ? 'none' : $element['#ahah']['effect'],
01799 'method' => empty($element['#ahah']['method']) ? 'replace' : $element['#ahah']['method'],
01800 'progress' => empty($element['#ahah']['progress']) ? array('type' => 'throbber') : $element['#ahah']['progress'],
01801 'button' => isset($element['#executes_submit_callback']) ? array($element['#name'] => $element['#value']) : FALSE,
01802 );
01803
01804
01805 if (is_string($ahah_binding['progress'])) {
01806 $ahah_binding['progress'] = array('type' => $ahah_binding['progress']);
01807 }
01808
01809 if (isset($ahah_binding['progress']['path'])) {
01810 $ahah_binding['progress']['url'] = url($ahah_binding['progress']['path']);
01811 }
01812
01813
01814 if ($ahah_binding['progress']['type'] == 'bar') {
01815 drupal_add_js('misc/progress.js');
01816 }
01817
01818 drupal_add_js(array('ahah' => array($element['#id'] => $ahah_binding)), 'setting');
01819
01820 $js_added[$element['#id']] = TRUE;
01821 $element['#cache'] = TRUE;
01822 }
01823 return $element;
01824 }
01825
01837 function theme_item($element) {
01838 return theme('form_element', $element, $element['#value'] . (!empty($element['#children']) ? $element['#children'] : ''));
01839 }
01840
01852 function theme_checkbox($element) {
01853 _form_set_class($element, array('form-checkbox'));
01854 $checkbox = '<input ';
01855 $checkbox .= 'type="checkbox" ';
01856 $checkbox .= 'name="' . $element['#name'] . '" ';
01857 $checkbox .= 'id="' . $element['#id'] . '" ' ;
01858 $checkbox .= 'value="' . $element['#return_value'] . '" ';
01859 $checkbox .= $element['#value'] ? ' checked="checked" ' : ' ';
01860 $checkbox .= drupal_attributes($element['#attributes']) . ' />';
01861
01862 if (!is_null($element['#title'])) {
01863 $checkbox = '<label class="option">' . $checkbox . ' ' . $element['#title'] . '</label>';
01864 }
01865
01866 unset($element['#title']);
01867 return theme('form_element', $element, $checkbox);
01868 }
01869
01880 function theme_checkboxes($element) {
01881 $class = 'form-checkboxes';
01882 if (isset($element['#attributes']['class'])) {
01883 $class .= ' ' . $element['#attributes']['class'];
01884 }
01885 $element['#children'] = '<div class="' . $class . '">' . (!empty($element['#children']) ? $element['#children'] : '') . '</div>';
01886 if ($element['#title'] || $element['#description']) {
01887 unset($element['#id']);
01888 return theme('form_element', $element, $element['#children']);
01889 }
01890 else {
01891 return $element['#children'];
01892 }
01893 }
01894
01895 function expand_checkboxes($element) {
01896 $value = is_array($element['#value']) ? $element['#value'] : array();
01897 $element['#tree'] = TRUE;
01898 if (count($element['#options']) > 0) {
01899 if (!isset($element['#default_value']) || $element['#default_value'] == 0) {
01900 $element['#default_value'] = array();
01901 }
01902 foreach ($element['#options'] as $key => $choice) {
01903 if (!isset($element[$key])) {
01904 $element[$key] = array('#type' => 'checkbox', '#processed' => TRUE, '#title' => $choice, '#return_value' => $key, '#default_value' => isset($value[$key]), '#attributes' => $element['#attributes']);
01905 }
01906 }
01907 }
01908 return $element;
01909 }
01910
01916 function theme_submit($element) {
01917 return theme('button', $element);
01918 }
01919
01925 function theme_button($element) {
01926
01927 if (isset($element['#attributes']['class'])) {
01928 $element['#attributes']['class'] = 'form-' . $element['#button_type'] . ' ' . $element['#attributes']['class'];
01929 }
01930 else {
01931 $element['#attributes']['class'] = 'form-' . $element['#button_type'];
01932 }
01933
01934 return '<input type="submit" ' . (empty($element['#name']) ? '' : 'name="' . $element['#name'] . '" ') . 'id="' . $element['#id'] . '" value="' . check_plain($element['#value']) . '" ' . drupal_attributes($element['#attributes']) . " />\n";
01935 }
01936
01942 function theme_image_button($element) {
01943
01944 if (isset($element['#attributes']['class'])) {
01945 $element['#attributes']['class'] = 'form-' . $element['#button_type'] . ' ' . $element['#attributes']['class'];
01946 }
01947 else {
01948 $element['#attributes']['class'] = 'form-' . $element['#button_type'];
01949 }
01950
01951 return '<input type="image" name="' . $element['#name'] . '" ' .
01952 (!empty($element['#value']) ? ('value="' . check_plain($element['#value']) . '" ') : '') .
01953 'id="' . $element['#id'] . '" ' .
01954 drupal_attributes($element['#attributes']) .
01955 ' src="' . base_path() . $element['#src'] . '" ' .
01956 (!empty($element['#title']) ? 'alt="' . check_plain($element['#title']) . '" title="' . check_plain($element['#title']) . '" ' : '' ) .
01957 "/>\n";
01958 }
01959
01971 function theme_hidden($element) {
01972 return '<input type="hidden" name="' . $element['#name'] . '" id="' . $element['#id'] . '" value="' . check_plain($element['#value']) . "\" " . drupal_attributes($element['#attributes']) . " />\n";
01973 }
01974
01980 function theme_token($element) {
01981 return theme('hidden', $element);
01982 }
01983
01995 function theme_textfield($element) {
01996 $size = empty($element['#size']) ? '' : ' size="' . $element['#size'] . '"';
01997 $maxlength = empty($element['#maxlength']) ? '' : ' maxlength="' . $element['#maxlength'] . '"';
01998 $class = array('form-text');
01999 $extra = '';
02000 $output = '';
02001
02002 if ($element['#autocomplete_path']) {
02003 drupal_add_js('misc/autocomplete.js');
02004 $class[] = 'form-autocomplete';
02005 $extra = '<input class="autocomplete" type="hidden" id="' . $element['#id'] . '-autocomplete" value="' . check_url(url($element['#autocomplete_path'], array('absolute' => TRUE))) . '" disabled="disabled" />';
02006 }
02007 _form_set_class($element, $class);
02008
02009 if (isset($element['#field_prefix'])) {
02010 $output .= '<span class="field-prefix">' . $element['#field_prefix'] . '</span> ';
02011 }
02012
02013 $output .= '<input type="text"' . $maxlength . ' name="' . $element['#name'] . '" id="' . $element['#id'] . '"' . $size . ' value="' . check_plain($element['#value']) . '"' . drupal_attributes($element['#attributes']) . ' />';
02014
02015 if (isset($element['#field_suffix'])) {
02016 $output .= ' <span class="field-suffix">' . $element['#field_suffix'] . '</span>';
02017 }
02018
02019 return theme('form_element', $element, $output) . $extra;
02020 }
02021
02033 function theme_form($element) {
02034
02035 $action = $element['#action'] ? 'action="' . check_url($element['#action']) . '" ' : '';
02036 return '<form ' . $action . ' accept-charset="UTF-8" method="' . $element['#method'] . '" id="' . $element['#id'] . '"' . drupal_attributes($element['#attributes']) . ">\n<div>" . $element['#children'] . "\n</div></form>\n";
02037 }
02038
02050 function theme_textarea($element) {
02051 $class = array('form-textarea');
02052
02053
02054 if (!empty($element['#teaser'])) {
02055 drupal_add_js('misc/teaser.js');
02056
02057 drupal_add_js(array('teaserCheckbox' => array($element['#id'] => $element['#teaser_checkbox'])), 'setting');
02058 drupal_add_js(array('teaser' => array($element['#id'] => $element['#teaser'])), 'setting');
02059 $class[] = 'teaser';
02060 }
02061
02062
02063 if ($element['#resizable'] !== FALSE) {
02064 drupal_add_js('misc/textarea.js');
02065 $class[] = 'resizable';
02066 }
02067
02068 _form_set_class($element, $class);
02069 return theme('form_element', $element, '<textarea cols="' . $element['#cols'] . '" rows="' . $element['#rows'] . '" name="' . $element['#name'] . '" id="' . $element['#id'] . '" ' . drupal_attributes($element['#attributes']) . '>' . check_plain($element['#value']) . '</textarea>');
02070 }
02071
02086 function theme_markup($element) {
02087 return (isset($element['#value']) ? $element['#value'] : '') . (isset($element['#children']) ? $element['#children'] : '');
02088 }
02089
02101 function theme_password($element) {
02102 $size = $element['#size'] ? ' size="' . $element['#size'] . '" ' : '';
02103 $maxlength = $element['#maxlength'] ? ' maxlength="' . $element['#maxlength'] . '" ' : '';
02104
02105 _form_set_class($element, array('form-text'));
02106 $output = '<input type="password" name="' . $element['#name'] . '" id="' . $element['#id'] . '" ' . $maxlength . $size . drupal_attributes($element['#attributes']) . ' />';
02107 return theme('form_element', $element, $output);
02108 }
02109
02113 function process_weight($element) {
02114 for ($n = (-1 * $element['#delta']); $n <= $element['#delta']; $n++) {
02115 $weights[$n] = $n;
02116 }
02117 $element['#options'] = $weights;
02118 $element['#type'] = 'select';
02119 $element['#is_weight'] = TRUE;
02120 $element += _element_info('select');
02121 return $element;
02122 }
02123
02145 function theme_file($element) {
02146 _form_set_class($element, array('form-file'));
02147 return theme('form_element', $element, '<input type="file" name="' . $element['#name'] . '"' . ($element['#attributes'] ? ' ' . drupal_attributes($element['#attributes']) : '') . ' id="' . $element['#id'] . '" size="' . $element['#size'] . "\" />\n");
02148 }
02149
02163 function theme_form_element($element, $value) {
02164
02165 $t = get_t();
02166
02167 $output = '<div class="form-item"';
02168 if (!empty($element['#id'])) {
02169 $output .= ' id="' . $element['#id'] . '-wrapper"';
02170 }
02171 $output .= ">\n";
02172 $required = !empty($element['#required']) ? '<span class="form-required" title="' . $t('This field is required.') . '">*</span>' : '';
02173
02174 if (!empty($element['#title'])) {
02175 $title = $element['#title'];
02176 if (!empty($element['#id'])) {
02177 $output .= ' <label for="' . $element['#id'] . '">' . $t('!title: !required', array('!title' => filter_xss_admin($title), '!required' => $required)) . "</label>\n";
02178 }
02179 else {
02180 $output .= ' <label>' . $t('!title: !required', array('!title' => filter_xss_admin($title), '!required' => $required)) . "</label>\n";
02181 }
02182 }
02183
02184 $output .= " $value\n";
02185
02186 if (!empty($element['#description'])) {
02187 $output .= ' <div class="description">' . $element['#description'] . "</div>\n";
02188 }
02189
02190 $output .= "</div>\n";
02191
02192 return $output;
02193 }
02194
02205 function _form_set_class(&$element, $class = array()) {
02206 if ($element['#required']) {
02207 $class[] = 'required';
02208 }
02209 if (form_get_error($element)) {
02210 $class[] = 'error';
02211 }
02212 if (isset($element['#attributes']['class'])) {
02213 $class[] = $element['#attributes']['class'];
02214 }
02215 $element['#attributes']['class'] = implode(' ', $class);
02216 }
02217
02234 function form_clean_id($id = NULL, $flush = FALSE) {
02235 static $seen_ids = array();
02236
02237 if ($flush) {
02238 $seen_ids = array();
02239 return;
02240 }
02241 $id = str_replace(array('][', '_', ' '), '-', $id);
02242
02243
02244
02245
02246
02247
02248
02249 if (isset($seen_ids[$id])) {
02250 $id = $id . '-' . $seen_ids[$id]++;
02251 }
02252 else {
02253 $seen_ids[$id] = 1;
02254 }
02255
02256 return $id;
02257 }
02258
02399 function batch_set($batch_definition) {
02400 if ($batch_definition) {
02401 $batch =& batch_get();
02402
02403 if (empty($batch)) {
02404 $batch = array(
02405 'sets' => array(),
02406 );
02407 }
02408
02409 $init = array(
02410 'sandbox' => array(),
02411 'results' => array(),
02412 'success' => FALSE,
02413 );
02414
02415 $t = get_t();
02416 $defaults = array(
02417 'title' => $t('Processing'),
02418 'init_message' => $t('Initializing.'),
02419 'progress_message' => $t('Remaining @remaining of @total.'),
02420 'error_message' => $t('An error has occurred.'),
02421 );
02422 $batch_set = $init + $batch_definition + $defaults;
02423
02424
02425 $batch_set['init_message'] .= '<br/> ';
02426 $batch_set['total'] = count($batch_set['operations']);
02427
02428
02429
02430 if (isset($batch['current_set'])) {
02431
02432 $slice1 = array_slice($batch['sets'], 0, $batch['current_set'] + 1);
02433 $slice2 = array_slice($batch['sets'], $batch['current_set'] + 1);
02434 $batch['sets'] = array_merge($slice1, array($batch_set), $slice2);
02435 }
02436 else {
02437 $batch['sets'][] = $batch_set;
02438 }
02439 }
02440 }
02441
02457 function batch_process($redirect = NULL, $url = NULL) {
02458 $batch =& batch_get();
02459
02460 if (isset($batch)) {
02461
02462 $url = isset($url) ? $url : 'batch';
02463 $process_info = array(
02464 'current_set' => 0,
02465 'progressive' => TRUE,
02466 'url' => isset($url) ? $url : 'batch',
02467 'source_page' => $_GET['q'],
02468 'redirect' => $redirect,
02469 );
02470 $batch += $process_info;
02471
02472 if ($batch['progressive']) {
02473
02474
02475
02476 if (isset($_REQUEST['destination'])) {
02477 $batch['destination'] = $_REQUEST['destination'];
02478 unset($_REQUEST['destination']);
02479 }
02480 elseif (isset($_REQUEST['edit']['destination'])) {
02481 $batch['destination'] = $_REQUEST['edit']['destination'];
02482 unset($_REQUEST['edit']['destination']);
02483 }
02484
02485
02486
02487 db_query("INSERT INTO {batch} (token, timestamp) VALUES ('', %d)", time());
02488 $batch['id'] = db_last_insert_id('batch', 'bid');
02489
02490
02491
02492 $t = get_t();
02493 $batch['error_message'] = $t('Please continue to <a href="@error_url">the error page</a>', array('@error_url' => url($url, array('query' => array('id' => $batch['id'], 'op' => 'finished')))));
02494
02495
02496 db_query("UPDATE {batch} SET token = '%s', batch = '%s' WHERE bid = %d", drupal_get_token($batch['id']), serialize($batch), $batch['id']);
02497
02498 drupal_goto($batch['url'], 'op=start&id=' . $batch['id']);
02499 }
02500 else {
02501
02502
02503 require_once './includes/batch.inc';
02504 _batch_process();
02505 }
02506 }
02507 }
02508
02512 function &batch_get() {
02513 static $batch = array();
02514 return $batch;
02515 }
02516