PageRenderTime 41ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 1ms

/drupal/sites/all/modules/contrib/views/includes/form.inc

https://github.com/seanberto/watershednow
Pascal | 301 lines | 120 code | 24 blank | 157 comment | 25 complexity | 138e05ccb46ba211852a2eacf588b2ed MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. // $Id: form.inc,v 1.11.2.1 2010/01/05 01:33:46 merlinofchaos Exp $
  3. /**
  4. * @file form.inc
  5. * Views' replacements for Drupal's form functions.
  6. *
  7. */
  8. function _drupal_build_form($form_id, &$form_state) {
  9. // Ensure that we have some defaults.
  10. // These are defaults only; if already set they will not be overridden.
  11. $form_state += array('storage' => NULL, 'submitted' => FALSE, 'input' => $_POST, 'method' => 'post');
  12. $args = isset($form_state['args']) ? $form_state['args'] : array();
  13. $cacheable = FALSE;
  14. if (isset($_SESSION['batch_form_state'])) {
  15. // We've been redirected here after a batch processing : the form has
  16. // already been processed, so we grab the post-process $form_state value
  17. // and move on to form display. See _batch_finished() function.
  18. $form_state = $_SESSION['batch_form_state'];
  19. unset($_SESSION['batch_form_state']);
  20. }
  21. else {
  22. // If the incoming $_POST contains a form_build_id, we'll check the
  23. // cache for a copy of the form in question. If it's there, we don't
  24. // have to rebuild the form to proceed. In addition, if there is stored
  25. // form_state data from a previous step, we'll retrieve it so it can
  26. // be passed on to the form processing code.
  27. if (isset($_POST['form_id']) && $_POST['form_id'] == $form_id && !empty($_POST['form_build_id'])) {
  28. $form = form_get_cache($_POST['form_build_id'], $form_state);
  29. if (!empty($form['#no_cache']) || empty($form)) {
  30. unset($form);
  31. }
  32. }
  33. // If the previous bit of code didn't result in a populated $form
  34. // object, we're hitting the form for the first time and we need
  35. // to build it from scratch.
  36. if (!isset($form)) {
  37. $form_state['post'] = $form_state['input'];
  38. // Use a copy of the function's arguments for manipulation
  39. $args_temp = $args;
  40. $args_temp[0] = &$form_state;
  41. array_unshift($args_temp, $form_id);
  42. $form = call_user_func_array('drupal_retrieve_form', $args_temp);
  43. $form_build_id = 'form-' . md5(mt_rand());
  44. $form['#build_id'] = $form_build_id;
  45. if ($form_state['method'] == 'get' && !isset($form['#method'])) {
  46. $form['#method'] = 'get';
  47. }
  48. drupal_prepare_form($form_id, $form, $form_state);
  49. // Store a copy of the unprocessed form for caching and indicate that it
  50. // is cacheable if #cache will be set.
  51. $original_form = $form;
  52. $cacheable = TRUE;
  53. unset($form_state['post']);
  54. }
  55. $form['#post'] = $form_state['input'];
  56. // Now that we know we have a form, we'll process it (validating,
  57. // submitting, and handling the results returned by its submission
  58. // handlers. Submit handlers accumulate data in the form_state by
  59. // altering the $form_state variable, which is passed into them by
  60. // reference.
  61. drupal_process_form_new($form_id, $form, $form_state);
  62. // If we were told not to redirect, but not told to re-render, return
  63. // here.
  64. if (!empty($form_state['executed']) && empty($form_state['rerender'])) {
  65. return;
  66. }
  67. if ($cacheable && !empty($form['#cache']) && empty($form['#no_cache'])) {
  68. // Caching is done past drupal_process_form so #process callbacks can
  69. // set #cache. By not sending the form state, we avoid storing
  70. // $form_state['storage'].
  71. form_set_cache($form_build_id, $original_form, NULL);
  72. }
  73. }
  74. // Most simple, single-step forms will be finished by this point --
  75. // drupal_process_form() usually redirects to another page (or to
  76. // a 'fresh' copy of the form) once processing is complete. If one
  77. // of the form's handlers has set $form_state['redirect'] to FALSE,
  78. // the form will simply be re-rendered with the values still in its
  79. // fields.
  80. //
  81. // If $form_state['storage'] or $form_state['rebuild'] have been
  82. // set by any submit or validate handlers, however, we know that
  83. // we're in a complex multi-part process of some sort and the form's
  84. // workflow is NOT complete. We need to construct a fresh copy of
  85. // the form, passing in the latest $form_state in addition to any
  86. // other variables passed into drupal_get_form().
  87. if (!empty($form_state['rebuild']) || !empty($form_state['storage'])) {
  88. $form = drupal_rebuild_form_new($form_id, $form_state, $args);
  89. }
  90. // If we haven't redirected to a new location by now, we want to
  91. // render whatever form array is currently in hand.
  92. return drupal_render_form($form_id, $form);
  93. }
  94. /**
  95. * Views' replacement of drupal_rebuild_form.
  96. *
  97. * This change merely respects a form's wishes not to be cached.
  98. */
  99. function drupal_rebuild_form_new($form_id, &$form_state, $args, $form_build_id = NULL) {
  100. // Remove the first argument. This is $form_id.when called from
  101. // drupal_get_form and the original $form_state when called from some AHAH
  102. // callback. Neither is needed. After that, put in the current state.
  103. $args[0] = &$form_state;
  104. // And the form_id.
  105. array_unshift($args, $form_id);
  106. $form = call_user_func_array('drupal_retrieve_form', $args);
  107. if (!isset($form_build_id)) {
  108. // We need a new build_id for the new version of the form.
  109. $form_build_id = 'form-' . md5(mt_rand());
  110. }
  111. $form['#build_id'] = $form_build_id;
  112. drupal_prepare_form($form_id, $form, $form_state);
  113. if (empty($form['#no_cache'])) {
  114. // Now, we cache the form structure so it can be retrieved later for
  115. // validation. If $form_state['storage'] is populated, we'll also cache
  116. // it so that it can be used to resume complex multi-step processes.
  117. form_set_cache($form_build_id, $form, $form_state);
  118. }
  119. // Originally this called drupal_process_form, but all that happens there
  120. // is form_builder and then submission; and the rebuilt form is not
  121. // allowed to submit. Therefore, just do this:
  122. $form['#post'] = $form_state['input'];
  123. $form = form_builder($form_id, $form, $form_state);
  124. return $form;
  125. }
  126. /**
  127. * Views' replacement for drupal_process_form that accepts commands
  128. * not to redirect, as well as forcing processing of 'get' method forms.
  129. */
  130. function drupal_process_form_new($form_id, &$form, &$form_state) {
  131. // submitting, and handling the results returned by its submission
  132. // handlers. Submit handlers accumulate data in the form_state by
  133. // altering the $form_state variable, which is passed into them by
  134. // reference.
  135. $form_state['values'] = array();
  136. // With $_GET, these forms are always submitted.
  137. if ($form_state['method'] == 'get') {
  138. if (!isset($form['#post']['form_build_id'])) {
  139. $form['#post']['form_build_id'] = $form['#build_id'];
  140. }
  141. if (!isset($form['#post']['form_id'])) {
  142. $form['#post']['form_id'] = $form_id;
  143. }
  144. if (!isset($form['#post']['form_token']) && isset($form['#token'])) {
  145. $form['#post']['form_token'] = drupal_get_token($form['#token']);
  146. }
  147. }
  148. $form = form_builder($form_id, $form, $form_state);
  149. // Only process the form if it is programmed or the form_id coming
  150. // from the POST data is set and matches the current form_id.
  151. if ((!empty($form['#programmed'])) || (!empty($form['#post']) && (isset($form['#post']['form_id']) && ($form['#post']['form_id'] == $form_id)))) {
  152. drupal_validate_form_new($form_id, $form, $form_state);
  153. // form_clean_id() maintains a cache of element IDs it has seen,
  154. // so it can prevent duplicates. We want to be sure we reset that
  155. // cache when a form is processed, so scenerios that result in
  156. // the form being built behind the scenes and again for the
  157. // browser don't increment all the element IDs needlessly.
  158. form_clean_id(NULL, TRUE);
  159. if ((!empty($form_state['submitted'])) && !form_get_errors() && empty($form_state['rebuild'])) {
  160. $form_state['redirect'] = NULL;
  161. form_execute_handlers('submit', $form, $form_state);
  162. // We'll clear out the cached copies of the form and its stored data
  163. // here, as we've finished with them. The in-memory copies are still
  164. // here, though.
  165. if (variable_get('cache', CACHE_DISABLED) == CACHE_DISABLED && !empty($form_state['values']['form_build_id'])) {
  166. cache_clear_all('form_' . $form_state['values']['form_build_id'], 'cache_form');
  167. cache_clear_all('storage_' . $form_state['values']['form_build_id'], 'cache_form');
  168. }
  169. // If batches were set in the submit handlers, we process them now,
  170. // possibly ending execution. We make sure we do not react to the batch
  171. // that is already being processed (if a batch operation performs a
  172. // drupal_execute).
  173. if ($batch =& batch_get() && !isset($batch['current_set'])) {
  174. // The batch uses its own copies of $form and $form_state for
  175. // late execution of submit handers and post-batch redirection.
  176. $batch['form'] = $form;
  177. $batch['form_state'] = $form_state;
  178. $batch['progressive'] = !$form['#programmed'];
  179. batch_process();
  180. // Execution continues only for programmatic forms.
  181. // For 'regular' forms, we get redirected to the batch processing
  182. // page. Form redirection will be handled in _batch_finished(),
  183. // after the batch is processed.
  184. }
  185. // If no submit handlers have populated the $form_state['storage']
  186. // bundle, and the $form_state['rebuild'] flag has not been set,
  187. // we're finished and should redirect to a new destination page
  188. // if one has been set (and a fresh, unpopulated copy of the form
  189. // if one hasn't). If the form was called by drupal_execute(),
  190. // however, we'll skip this and let the calling function examine
  191. // the resulting $form_state bundle itself.
  192. if (!$form['#programmed'] && empty($form_state['rebuild']) && empty($form_state['storage'])) {
  193. if (!empty($form_state['no_redirect'])) {
  194. $form_state['executed'] = TRUE;
  195. }
  196. else {
  197. drupal_redirect_form($form, $form_state['redirect']);
  198. }
  199. }
  200. }
  201. }
  202. }
  203. /**
  204. * The original version of drupal_validate_form does not have an override for
  205. * the static check to only validate a form id once. Unfortunately, we need
  206. * to be able to overridet his.
  207. */
  208. function drupal_validate_form_new($form_id, $form, &$form_state) {
  209. static $validated_forms = array();
  210. if (isset($validated_forms[$form_id]) && empty($form_state['must_validate'])) {
  211. return;
  212. }
  213. // If the session token was set by drupal_prepare_form(), ensure that it
  214. // matches the current user's session.
  215. if (isset($form['#token'])) {
  216. if (!drupal_valid_token($form_state['values']['form_token'], $form['#token'])) {
  217. // Setting this error will cause the form to fail validation.
  218. form_set_error('form_token', t('Validation error, please try again. If this error persists, please contact the site administrator.'));
  219. }
  220. }
  221. _form_validate($form, $form_state, $form_id);
  222. $validated_forms[$form_id] = TRUE;
  223. }
  224. /**
  225. * Process callback to add dependency to form items.
  226. *
  227. * Usage:
  228. *
  229. * On any form item, add
  230. * - @code '#process' => 'views_process_dependency' @endcode
  231. * - @code '#dependency' => array('id-of-form-without-the-#' => array(list, of, values, that, make, this, gadget, visible)); @endcode
  232. */
  233. function views_process_dependency($element, $edit, &$form_state, &$form) {
  234. static $dependencies;
  235. if (isset($element['#dependency']) && !isset($dependencies[$element['#id']])) {
  236. if (!isset($element['#dependency_count'])) {
  237. $element['#dependency_count'] = 1;
  238. }
  239. if (!empty($form_state['ajax'])) {
  240. $form_state['js settings']['viewsAjax']['formRelationships'][$element['#id']] = array('num' => $element['#dependency_count'], 'values' => $element['#dependency']);
  241. }
  242. else {
  243. views_add_js('dependent');
  244. $options['viewsAjax']['formRelationships'][$element['#id']] = array('num' => $element['#dependency_count'], 'values' => $element['#dependency']);
  245. drupal_add_js($options, 'setting');
  246. }
  247. $dependencies[$element['#id']] = TRUE;
  248. }
  249. return $element;
  250. }
  251. /**
  252. * #process callback to see if we need to check_plain() the options.
  253. *
  254. * Since FAPI is inconsistent, the #options are sanitized for you in all cases
  255. * _except_ checkboxes. We have form elements that are sometimes 'select' and
  256. * sometimes 'checkboxes', so we need decide late in the form rendering cycle
  257. * if the options need to be sanitized before they're rendered. This callback
  258. * inspects the type, and if it's still 'checkboxes', does the sanitation.
  259. */
  260. function views_process_check_options($element, $edit, &$form_state, &$form) {
  261. if ($element['#type'] == 'checkboxes' || $element['#type'] == 'checkbox') {
  262. $element['#options'] = array_map('check_plain', $element['#options']);
  263. }
  264. return $element;
  265. }