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