1 |
<?php |
2 |
/** |
3 |
* This file contains the FormProcessor class. |
4 |
* |
5 |
* $Id: FormProcessor.inc,v 1.32 2003/06/16 17:15:56 hemna Exp $ |
6 |
* |
7 |
* @author Walter A. Boring IV <waboring@buildabetterweb.com> |
8 |
* @author Suren Markossian <suren@cbestwhat?> |
9 |
* @package phpHtmlLib |
10 |
* @subpackage FormProcessing |
11 |
* |
12 |
* @copyright LGPL - See LICENCE |
13 |
* |
14 |
*/ |
15 |
|
16 |
define("FORM_ACTION", "_form_action"); |
17 |
define("FORM_VISITED", "_form_visited"); |
18 |
define("FORM_CONFIRM", "_form_confirm"); |
19 |
|
20 |
/** |
21 |
* This is the main engine for the processing |
22 |
* of Forms. It builds the form tag, and calls |
23 |
* the appropriate FormContent methods to build |
24 |
* the FormElement's and validation, as well as |
25 |
* backend processing to do the action after the |
26 |
* data has been validated. |
27 |
* |
28 |
* @package phpHtmlLib |
29 |
* @subpackage FormProcessing |
30 |
*/ |
31 |
class FormProcessor extends Container { |
32 |
|
33 |
|
34 |
/** |
35 |
* This array holds the FORMtag |
36 |
* attributes for this form |
37 |
* |
38 |
*/ |
39 |
var $_form_attributes = array("method" => "post", |
40 |
"action" => "", |
41 |
"name" => "myform", |
42 |
"target" => "", |
43 |
"onsubmit" => "", |
44 |
"style" => "margin: 0px 0px 0px 0px;"); |
45 |
|
46 |
/** |
47 |
* This holds the FormContent Object |
48 |
* that knows how to render the |
49 |
* form. |
50 |
*/ |
51 |
var $_form_content = NULL; |
52 |
|
53 |
|
54 |
/** |
55 |
* This flag lets us know there |
56 |
* were errors during processing or |
57 |
* validating the form. |
58 |
*/ |
59 |
var $_has_errors = FALSE; |
60 |
|
61 |
/** |
62 |
* Flag to let us know the form |
63 |
* has been confirmed. |
64 |
*/ |
65 |
var $_confirmed = FALSE; |
66 |
|
67 |
/** |
68 |
* Flag to let us know if we should |
69 |
* render the form after it was |
70 |
* successfully processed |
71 |
*/ |
72 |
var $_form_success_render = TRUE; |
73 |
|
74 |
/** |
75 |
* This is the FormValidation object |
76 |
* used to validate the form elements |
77 |
*/ |
78 |
var $_FormValidation = NULL; |
79 |
|
80 |
/** |
81 |
* The action that was taken |
82 |
* for the form |
83 |
*/ |
84 |
var $_form_submit_action = NULL; |
85 |
|
86 |
/** |
87 |
* The form was processed and passed |
88 |
* the confirmation if any, and |
89 |
* it was successfull ? |
90 |
*/ |
91 |
var $_confirmed_successfull = FALSE; |
92 |
|
93 |
/** |
94 |
* This tells us to show or not to |
95 |
* show the form errors autmatically. |
96 |
* The user of the FormProcessor |
97 |
* may want to deal with errors manually |
98 |
*/ |
99 |
var $_auto_show_errors = TRUE; |
100 |
|
101 |
|
102 |
/** |
103 |
* The constructor for the FormProcessor |
104 |
* |
105 |
* @param FormContent object |
106 |
* @param string the form name |
107 |
*/ |
108 |
function FormProcessor(&$form_content, $form_name="myform", |
109 |
$form_action=NULL) { |
110 |
|
111 |
$this->_form_content = &$form_content; |
112 |
$this->set_form_name( $form_name ); |
113 |
$this->_form_content->set_form_name($form_name); |
114 |
|
115 |
if ($form_action != NULL) { |
116 |
$this->set_form_action( $form_action ); |
117 |
} else { |
118 |
$this->set_form_action( $_SERVER["PHP_SELF"] ); |
119 |
} |
120 |
|
121 |
//Set up the Validation object |
122 |
//and the FormErrors object to |
123 |
//be used by this form. |
124 |
$this->setup_validation(); |
125 |
|
126 |
//now process the form |
127 |
$this->_process_form(); |
128 |
} |
129 |
|
130 |
/** |
131 |
* This function is used to setup |
132 |
* the validation object and the |
133 |
* form errors object that is to be |
134 |
* used by this form. |
135 |
* |
136 |
* You can override this method to |
137 |
* use a different FormErrors object |
138 |
* for localization. |
139 |
*/ |
140 |
function setup_validation() { |
141 |
$this->_FormValidation = new FormValidation( new FormErrors ); |
142 |
} |
143 |
|
144 |
/** |
145 |
* This method does the logic of |
146 |
* doing the form processing |
147 |
*/ |
148 |
function _process_form() { |
149 |
//let the form build the FormElement objects |
150 |
//that will be used by the form |
151 |
$this->_form_content->form_init_elements(); |
152 |
|
153 |
//first we need to |
154 |
if (!@$_REQUEST[FORM_VISITED]) { |
155 |
$this->_form_content->form_init_data(); |
156 |
} |
157 |
|
158 |
|
159 |
//we only need to process the form |
160 |
//if it has been visited. Otherwise |
161 |
//it just gets rendered. |
162 |
if (!empty($_REQUEST[FORM_VISITED]) && $_REQUEST[FORM_VISITED] == 1) { |
163 |
$this->_set_action(); |
164 |
|
165 |
//let see if this was a confirmation page. |
166 |
if ( !empty($_REQUEST[FORM_CONFIRM]) && $_REQUEST[FORM_CONFIRM] == 1 ) { |
167 |
//looks like this was a submit on a |
168 |
//confirmation page. we don't need |
169 |
//to do form field validation. |
170 |
$this->_confirmed = TRUE; |
171 |
} |
172 |
|
173 |
//now do the validation |
174 |
if (!$this->_confirmed) { |
175 |
//we haven't been confirmed, so we |
176 |
//need to validate the form. |
177 |
if ($this->can_validate()) { |
178 |
//looks like we should do validation |
179 |
$this->do_validation(); |
180 |
} |
181 |
if (!$this->_has_errors) { |
182 |
//no errors were found |
183 |
if ($this->_form_content->has_confirm()) { |
184 |
//the form content has a confirmation |
185 |
//we need to process |
186 |
$this->_has_errors = !$this->_pre_confirm(); |
187 |
} else { |
188 |
//make sure we don't have any backend errors |
189 |
$this->_has_errors = !$this->_form_content->form_backend_validation(); |
190 |
if (!$this->_has_errors) { |
191 |
$this->_has_errors = !$this->_process_action(); |
192 |
if (!$this->_has_errors) { |
193 |
$this->_set_confirmed_success(TRUE); |
194 |
} |
195 |
} |
196 |
} |
197 |
} |
198 |
} else { |
199 |
//make sure we don't have any backend errors |
200 |
$this->_has_errors = !$this->_form_content->form_backend_validation(); |
201 |
if (!$this->_has_errors) { |
202 |
$this->_has_errors = !$this->_process_action(); |
203 |
if (!$this->_has_errors) { |
204 |
$this->_set_confirmed_success(TRUE); |
205 |
} |
206 |
} |
207 |
} |
208 |
} |
209 |
} |
210 |
|
211 |
|
212 |
/** |
213 |
* This function is responsible for |
214 |
* processing the form action |
215 |
* after validation, and form confirmation |
216 |
* happens. |
217 |
* |
218 |
*/ |
219 |
function _process_action() { |
220 |
//There were no validation errors. |
221 |
return $this->_form_content->form_action(); |
222 |
} |
223 |
|
224 |
/** |
225 |
* This method calls the FormContent |
226 |
* to let it do any data munging before the |
227 |
* confirmation page is rendered |
228 |
*/ |
229 |
function _pre_confirm() { |
230 |
return $this->_form_content->pre_confirm(); |
231 |
} |
232 |
|
233 |
|
234 |
/** |
235 |
* This method walks the FormContent's visible elements |
236 |
* and calls the validation function for the element |
237 |
* |
238 |
*/ |
239 |
function do_validation() { |
240 |
$keys = array_keys( $this->_form_content->_elements ); |
241 |
foreach( $keys as $key ) { |
242 |
$valid = $this->_form_content->_elements[$key]->_do_validation($this->_FormValidation); |
243 |
if (!$valid) { |
244 |
$this->_has_errors = TRUE; |
245 |
} |
246 |
} |
247 |
} |
248 |
|
249 |
|
250 |
/** |
251 |
* This method is called to render the form's html |
252 |
* |
253 |
*/ |
254 |
function render($indent_level=0, $output_debug=0) { |
255 |
if ($this->_has_errors) { |
256 |
//we need to render the form errors |
257 |
//and then the form |
258 |
return $this->render_error($indent_level, $output_debug); |
259 |
} else { |
260 |
//there are no errors! |
261 |
if (@$_REQUEST[FORM_VISITED] == 1) { |
262 |
//looks like the form has been processed? |
263 |
if ($this->_form_content->has_confirm() && !$this->_confirmed) { |
264 |
return $this->render_confirm($indent_level, $output_debug); |
265 |
} else { |
266 |
//Looks like the action worked |
267 |
$success = $this->_form_content->form_success(); |
268 |
|
269 |
if ($this->_form_success_render) { |
270 |
return $this->render_form($indent_level, $output_debug, |
271 |
$success); |
272 |
} else { |
273 |
if (method_exists($success,"render")) { |
274 |
//looks like this is an object. |
275 |
//we'll assume it has a render function. |
276 |
return $success->render($indent_level, $output_debug); |
277 |
} else { |
278 |
//since its not an object, |
279 |
//lets just display it. |
280 |
return $success; |
281 |
} |
282 |
} |
283 |
} |
284 |
} else { |
285 |
return $this->render_form($indent_level, $output_debug); |
286 |
} |
287 |
} |
288 |
} |
289 |
|
290 |
|
291 |
/** |
292 |
* This renders the form |
293 |
* |
294 |
* @param the FormContent->form() object |
295 |
* @param int - $indent_level |
296 |
* @param int - $output_debug |
297 |
* @param object - the form errors object. |
298 |
* @return raw html |
299 |
*/ |
300 |
function render_form( $indent_level, $output_debug, $obj=NULL ) { |
301 |
|
302 |
//build the $this->_form object. |
303 |
$this->_build_form_tag(); |
304 |
|
305 |
//check to see if the form_content |
306 |
//has any js, or any of the form elements |
307 |
//have js. |
308 |
$form_js = $this->_build_javascript(); |
309 |
if (strlen($form_js) > 0) { |
310 |
$script = html_script(); |
311 |
$script->add( $form_js ); |
312 |
//$this->_form->add( $script ); |
313 |
} |
314 |
|
315 |
if ($obj) { |
316 |
$this->_form->add_reference( $obj ); |
317 |
} |
318 |
|
319 |
$this->_form->add_reference( $this->_form_content->form() ); |
320 |
|
321 |
//add the FormContent's hidden declared |
322 |
//hidden form fields. |
323 |
$this->_add_hidden_fields(); |
324 |
|
325 |
//Ok lets add our hidden vars |
326 |
$this->__hidden_fields(); |
327 |
|
328 |
if (isset($script)) { |
329 |
$c = container( $script, $this->_form ); |
330 |
return $c->render($indent_level, $output_debug); |
331 |
} else { |
332 |
return $this->_form->render($indent_level, $output_debug); |
333 |
} |
334 |
|
335 |
} |
336 |
|
337 |
/** |
338 |
* This function renders the confirmation |
339 |
* page. This page sits in between the |
340 |
* front end form, and the action handler. |
341 |
* This only gets called after a form |
342 |
* and its data has been successfully |
343 |
* validated. |
344 |
* |
345 |
* @param int - $indent_level |
346 |
* @param int - $output_debug |
347 |
* |
348 |
* @return string - the raw html |
349 |
*/ |
350 |
function render_confirm( $indent_level, $output_debug ) { |
351 |
//build the $this->_form object. |
352 |
$this->_build_form_tag(); |
353 |
|
354 |
//add the confirm object/html |
355 |
$confirm = &$this->_form_content->form_confirm( ); |
356 |
$this->_form->add_reference( $confirm ); |
357 |
|
358 |
//ok add all of the submitted data as hidden form fields |
359 |
$this->_add_confirm_data(); |
360 |
|
361 |
//Ok lets add our hidden vars |
362 |
$this->__hidden_fields(); |
363 |
|
364 |
return $this->_form->render($indent_level, $output_debug); |
365 |
} |
366 |
|
367 |
|
368 |
/** |
369 |
* This renders the error table |
370 |
* and then the form with the fields |
371 |
* |
372 |
* @param array - the form field vlues. |
373 |
* @param array - array of errors. |
374 |
* @param int - $indent_level |
375 |
* @param int - $output_debug |
376 |
* |
377 |
* @return raw html |
378 |
*/ |
379 |
function render_error( $indent_level, $output_debug) { |
380 |
|
381 |
if ($this->_auto_show_errors) { |
382 |
//Ok first lets build the error table |
383 |
$wrapper = new DIVtag; |
384 |
$errors = &$this->_form_content->form_errors(); |
385 |
if ($errors != NULL) $wrapper->add( $errors, html_br() ); |
386 |
} else { |
387 |
$wrapper = NULL; |
388 |
} |
389 |
|
390 |
|
391 |
return $this->render_form( $indent_level, $output_debug, $wrapper); |
392 |
} |
393 |
|
394 |
|
395 |
|
396 |
|
397 |
//***********************************************// |
398 |
//* utility functions for this class *// |
399 |
//***********************************************// |
400 |
|
401 |
|
402 |
/** |
403 |
* This method lets us turn on/off the |
404 |
* ability to do validation for the form |
405 |
* |
406 |
* @return BOOLEAN |
407 |
*/ |
408 |
function can_validate() { |
409 |
return TRUE; |
410 |
} |
411 |
|
412 |
|
413 |
/** |
414 |
* This function turns on the ability to |
415 |
* render the form after the success |
416 |
* of the action. Normally this feature |
417 |
* is off |
418 |
* |
419 |
*/ |
420 |
function set_render_form_after_success($flag=TRUE) { |
421 |
$this->_form_success_render = $flag; |
422 |
} |
423 |
|
424 |
/** |
425 |
* This is used to test to see if the form action |
426 |
* was processed succesfully. |
427 |
* This is usefull for external entities to determine |
428 |
* if the form was processed, and it was successfull. |
429 |
* |
430 |
* @return boolean |
431 |
*/ |
432 |
function is_action_successfull() { |
433 |
return $this->_confirmed_successfull; |
434 |
} |
435 |
|
436 |
/** |
437 |
* This flag sets the flag that tells |
438 |
* if we successfully confirmed the form, |
439 |
* and processed the action |
440 |
* |
441 |
* @param boolean |
442 |
*/ |
443 |
function _set_confirmed_success($flag=TRUE) { |
444 |
$this->_confirmed_successfull = $flag; |
445 |
} |
446 |
|
447 |
/** |
448 |
* This sets the flag that tells this class |
449 |
* to automatically call the form contents |
450 |
* form errors and display it or not |
451 |
* |
452 |
* @param boolean - show errors? |
453 |
*/ |
454 |
function set_auto_error_display($flag=TRUE) { |
455 |
$this->_auto_show_errors = $flag; |
456 |
} |
457 |
|
458 |
/** |
459 |
* This gets the current value of the flag |
460 |
* that tells us to show form errors automatically |
461 |
* or not. |
462 |
* |
463 |
* @return boolean |
464 |
*/ |
465 |
function get_auto_error_display() { |
466 |
return $this->_auto_show_errors; |
467 |
} |
468 |
|
469 |
|
470 |
/** |
471 |
* This method allows us to get access to the |
472 |
* errors display object that is generated by |
473 |
* the form content. This is the display |
474 |
* object that is meant to be rendered directly. |
475 |
* If there are no errors. we will return NULL |
476 |
* |
477 |
* @return object |
478 |
*/ |
479 |
function &get_error_display_object() { |
480 |
if ($this->_has_errors) { |
481 |
return $this->_form_content->form_errors(); |
482 |
} else { |
483 |
return NULL; |
484 |
} |
485 |
} |
486 |
|
487 |
|
488 |
/** |
489 |
* This method returns an array of errors that |
490 |
* happened in the form. |
491 |
* |
492 |
* @return array |
493 |
*/ |
494 |
function get_error_array() { |
495 |
return $this->_form_content->get_error_array(); |
496 |
} |
497 |
|
498 |
/** |
499 |
* This returns the flag that tells us that |
500 |
* the form has errors during processing |
501 |
* |
502 |
* @return boolean |
503 |
*/ |
504 |
function has_errors() { |
505 |
return $this->_has_errors; |
506 |
} |
507 |
|
508 |
|
509 |
|
510 |
|
511 |
//************************************************// |
512 |
//* FORMtag Attributes for this class *// |
513 |
//************************************************// |
514 |
|
515 |
/** |
516 |
* This function is used to set the |
517 |
* form name |
518 |
* |
519 |
* @param string |
520 |
*/ |
521 |
function set_form_name($name) { |
522 |
$this->_form_attributes["name"] = $name; |
523 |
} |
524 |
|
525 |
/** |
526 |
* This function is used to get |
527 |
* the form name |
528 |
* |
529 |
* @return string |
530 |
*/ |
531 |
function get_form_name() { |
532 |
return $this->_form_attributes["name"]; |
533 |
} |
534 |
|
535 |
/** |
536 |
* This function is used to set the |
537 |
* form target |
538 |
* |
539 |
* @param string |
540 |
*/ |
541 |
function set_form_target($target) { |
542 |
$this->_form_attributes["target"] = $target; |
543 |
} |
544 |
|
545 |
/** |
546 |
* This function is used to get |
547 |
* the form target |
548 |
* |
549 |
* @return string |
550 |
*/ |
551 |
function get_form_target() { |
552 |
return $this->_form_attributes["target"]; |
553 |
} |
554 |
|
555 |
/** |
556 |
* This function is used to set the |
557 |
* form method |
558 |
* |
559 |
* @param string (POST or GET) |
560 |
*/ |
561 |
function set_form_method($method) { |
562 |
if ( strcasecmp($method,"GET") !=0 && strcasecmp($method,"POST") !=0 ) { |
563 |
user_error("FormProcessor::set_form_method() - INVALID Form method ".$method); |
564 |
} else { |
565 |
$this->_form_attributes["method"] = $method; |
566 |
} |
567 |
} |
568 |
|
569 |
/** |
570 |
* This function is used to get |
571 |
* the form method |
572 |
* |
573 |
* @return string (POST or GET) |
574 |
*/ |
575 |
function get_form_method() { |
576 |
return $this->_form_attributes["method"]; |
577 |
} |
578 |
|
579 |
/** |
580 |
* Sets the form action |
581 |
* |
582 |
* @param string |
583 |
*/ |
584 |
function set_form_action($action) { |
585 |
$this->_form_attributes["action"] = $action; |
586 |
} |
587 |
|
588 |
/** |
589 |
* This function is used to get |
590 |
* the form action |
591 |
* |
592 |
* @return string (POST or GET) |
593 |
*/ |
594 |
function get_form_action() { |
595 |
return $this->_form_attributes["action"]; |
596 |
} |
597 |
|
598 |
/** |
599 |
* Sets the form enctype |
600 |
* |
601 |
* @param string |
602 |
*/ |
603 |
function set_form_enctype($enctype) { |
604 |
$this->_form_attributes["enctype"] = $enctype; |
605 |
} |
606 |
|
607 |
/** |
608 |
* This function is used to get |
609 |
* the form enctype value |
610 |
* |
611 |
* @return string |
612 |
*/ |
613 |
function get_form_enctype() { |
614 |
return $this->_form_attributes["enctype"]; |
615 |
} |
616 |
|
617 |
|
618 |
/** |
619 |
* This is used to set the action |
620 |
* submitted by the user |
621 |
* |
622 |
*/ |
623 |
function _set_action() { |
624 |
$this->_form_submit_action = $_REQUEST[FORM_ACTION]; |
625 |
$this->_form_content->set_action($this->_form_submit_action); |
626 |
} |
627 |
|
628 |
/** |
629 |
* This is used to get the action that was |
630 |
* processed by the form |
631 |
* |
632 |
* @return string |
633 |
*/ |
634 |
function get_action() { |
635 |
return $this->_form_submit_action; |
636 |
} |
637 |
|
638 |
|
639 |
/** |
640 |
* this function builds the FORMtag object |
641 |
* and its attributes. |
642 |
* |
643 |
* @return FORMtag object. |
644 |
*/ |
645 |
function _build_form_tag() { |
646 |
$form_attrs = array(); |
647 |
foreach( $this->_form_attributes as $name => $value) { |
648 |
if ($value) { |
649 |
$form_attrs[$name] = $value; |
650 |
} |
651 |
} |
652 |
$this->_form = new FORMtag( $form_attrs ); |
653 |
} |
654 |
|
655 |
/** |
656 |
* This adds all of the submitted data as |
657 |
* hidden form fields |
658 |
* |
659 |
*/ |
660 |
function _add_confirm_data() { |
661 |
$keys = array_keys( $this->_form_content->_elements ); |
662 |
foreach( $keys as $key ) { |
663 |
$element_name = $this->_form_content->_elements[$key]->get_element_name(); |
664 |
$values = $this->_form_content->_elements[$key]->get_value(); |
665 |
if (is_array($values)) { |
666 |
foreach($values as $value) { |
667 |
$this->_form->add( form_hidden($element_name, $value) ); |
668 |
} |
669 |
} else { |
670 |
$this->_form->add( form_hidden($element_name, $values) ); |
671 |
|
672 |
} |
673 |
} |
674 |
|
675 |
$keys = array_keys( $this->_form_content->_hidden_elements ); |
676 |
foreach( $keys as $key ) { |
677 |
$this->_form->add( $this->_form_content->_hidden_elements[$key]->get_element() ); |
678 |
} |
679 |
} |
680 |
|
681 |
|
682 |
/** |
683 |
* This function adds the form content's |
684 |
* hidden form fields to the |
685 |
* form automatically |
686 |
* |
687 |
*/ |
688 |
function _add_hidden_fields() { |
689 |
//Lets add the form's hidden vars it wants |
690 |
foreach ($this->_form_content->_hidden_elements as $element) { |
691 |
$this->_form->add( $element->get_element() ); |
692 |
} |
693 |
} |
694 |
|
695 |
|
696 |
/** |
697 |
* This method adds the processor specific |
698 |
* hidden fields. |
699 |
* |
700 |
*/ |
701 |
function __hidden_fields() { |
702 |
$this->_form->add( form_hidden(FORM_ACTION), |
703 |
form_hidden(FORM_VISITED,1) ); |
704 |
|
705 |
if ($this->_form_content->has_confirm() && !$this->_confirmed) { |
706 |
if (@!$_REQUEST[FORM_VISITED] || $this->_has_errors) { |
707 |
$this->_form->add( form_hidden(FORM_CONFIRM, 0 ) ); |
708 |
} else { |
709 |
$this->_form->add( form_hidden(FORM_CONFIRM, 1 ) ); |
710 |
} |
711 |
} else if ($this->_form_content->has_confirm() && !$this->_confirmed) { |
712 |
//reset this so they can submit again |
713 |
$this->_form->add( form_hidden(FORM_CONFIRM, 0 ) ); |
714 |
} |
715 |
} |
716 |
|
717 |
/** |
718 |
* This method is used to build any Javascript |
719 |
* that is used by the form and/or the form elements |
720 |
* used in the form. |
721 |
* |
722 |
* @return string |
723 |
*/ |
724 |
function _build_javascript() { |
725 |
$form_js = $this->_form_content->javascript(); |
726 |
|
727 |
//now walk each form element and try to get any js |
728 |
foreach( $this->_form_content->_elements as $element ) { |
729 |
$form_js .= $element->javascript(); |
730 |
} |
731 |
|
732 |
return $form_js; |
733 |
} |
734 |
} |
735 |
?> |