13 |
* |
* |
14 |
*/ |
*/ |
15 |
|
|
16 |
define(FORM_ACTION, "_form_action"); |
define("FORM_ACTION", "_form_action"); |
17 |
define(FORM_VISITED, "_form_visited"); |
define("FORM_VISITED", "_form_visited"); |
18 |
define(FORM_CONFIRM, "_form_confirm"); |
define("FORM_CONFIRM", "_form_confirm"); |
19 |
|
|
20 |
/** |
/** |
21 |
* This is the main engine for the processing |
* This is the main engine for the processing |
30 |
*/ |
*/ |
31 |
class FormProcessor extends Container { |
class FormProcessor extends Container { |
32 |
|
|
|
/** |
|
|
* This holds the name of the form |
|
|
* for js that needs it |
|
|
*/ |
|
|
var $_form_name="forms[0]"; |
|
|
|
|
33 |
|
|
34 |
/** |
/** |
35 |
* The action for the form tag |
* This array holds the FORMtag |
36 |
*/ |
* attributes for this form |
|
var $_form_action = ""; |
|
|
|
|
|
/** |
|
|
* The form's enctype attribute. |
|
|
* ie <form enctype="multipart/form_data"> |
|
37 |
* |
* |
38 |
*/ |
*/ |
39 |
var $_form_enctype = NULL; |
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 |
* This holds the FormContent Object |
71 |
*/ |
*/ |
72 |
var $_form_success_render = TRUE; |
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 |
* The constructor for the FormProcessor |
105 |
* @param FormContent object |
* @param FormContent object |
106 |
* @param string the form name |
* @param string the form name |
107 |
*/ |
*/ |
108 |
function FormProcessor(&$form_content, $form_name="forms[0]", |
function FormProcessor(&$form_content, $form_name="myform", |
109 |
$form_action=NULL) { |
$form_action=NULL) { |
110 |
|
|
|
$this->_form_name = $form_name; |
|
111 |
$this->_form_content = &$form_content; |
$this->_form_content = &$form_content; |
112 |
|
$this->set_form_name( $form_name ); |
113 |
$this->_form_content->set_form_name($form_name); |
$this->_form_content->set_form_name($form_name); |
114 |
|
|
115 |
if ($form_action != NULL) { |
if ($form_action != NULL) { |
116 |
$this->_form_action = $form_action; |
$this->set_form_action( $form_action ); |
117 |
} else { |
} else { |
118 |
$this->_form_action = $_SERVER["PHP_SELF"]; |
$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 |
//now process the form |
127 |
$this->_process_form(); |
$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 |
* This method does the logic of |
146 |
* doing the form processing |
* doing the form processing |
147 |
*/ |
*/ |
151 |
$this->_form_content->form_init_elements(); |
$this->_form_content->form_init_elements(); |
152 |
|
|
153 |
//first we need to |
//first we need to |
154 |
if (!$_POST[FORM_VISITED]) { |
if (!@$_REQUEST[FORM_VISITED]) { |
155 |
$this->_form_content->form_init_data(); |
$this->_form_content->form_init_data(); |
156 |
} |
} |
157 |
|
|
159 |
//we only need to process the form |
//we only need to process the form |
160 |
//if it has been visited. Otherwise |
//if it has been visited. Otherwise |
161 |
//it just gets rendered. |
//it just gets rendered. |
162 |
if ($_POST[FORM_VISITED] == 1) { |
if (!empty($_REQUEST[FORM_VISITED]) && $_REQUEST[FORM_VISITED] == 1) { |
163 |
$this->_form_content->set_action($_POST[FORM_ACTION]); |
$this->_set_action(); |
164 |
|
|
165 |
//let see if this was a confirmation page. |
//let see if this was a confirmation page. |
166 |
if ( $_POST[FORM_CONFIRM] == 1 ) { |
if ( !empty($_REQUEST[FORM_CONFIRM]) && $_REQUEST[FORM_CONFIRM] == 1 ) { |
167 |
//looks like this was a submit on a |
//looks like this was a submit on a |
168 |
//confirmation page. we don't need |
//confirmation page. we don't need |
169 |
//to do form field validation. |
//to do form field validation. |
189 |
$this->_has_errors = !$this->_form_content->form_backend_validation(); |
$this->_has_errors = !$this->_form_content->form_backend_validation(); |
190 |
if (!$this->_has_errors) { |
if (!$this->_has_errors) { |
191 |
$this->_has_errors = !$this->_process_action(); |
$this->_has_errors = !$this->_process_action(); |
192 |
|
if (!$this->_has_errors) { |
193 |
|
$this->_set_confirmed_success(TRUE); |
194 |
|
} |
195 |
} |
} |
196 |
} |
} |
197 |
} |
} |
200 |
$this->_has_errors = !$this->_form_content->form_backend_validation(); |
$this->_has_errors = !$this->_form_content->form_backend_validation(); |
201 |
if (!$this->_has_errors) { |
if (!$this->_has_errors) { |
202 |
$this->_has_errors = !$this->_process_action(); |
$this->_has_errors = !$this->_process_action(); |
203 |
|
if (!$this->_has_errors) { |
204 |
|
$this->_set_confirmed_success(TRUE); |
205 |
|
} |
206 |
} |
} |
207 |
} |
} |
208 |
} |
} |
239 |
function do_validation() { |
function do_validation() { |
240 |
$keys = array_keys( $this->_form_content->_elements ); |
$keys = array_keys( $this->_form_content->_elements ); |
241 |
foreach( $keys as $key ) { |
foreach( $keys as $key ) { |
242 |
$valid = $this->_form_content->_elements[$key]->_do_validation(); |
$valid = $this->_form_content->_elements[$key]->_do_validation($this->_FormValidation); |
243 |
if (!$valid) { |
if (!$valid) { |
244 |
$this->_has_errors = TRUE; |
$this->_has_errors = TRUE; |
245 |
} |
} |
258 |
return $this->render_error($indent_level, $output_debug); |
return $this->render_error($indent_level, $output_debug); |
259 |
} else { |
} else { |
260 |
//there are no errors! |
//there are no errors! |
261 |
if ($_POST[FORM_VISITED] == 1) { |
if (@$_REQUEST[FORM_VISITED] == 1) { |
262 |
//looks like the form has been processed? |
//looks like the form has been processed? |
263 |
if ($this->_form_content->has_confirm() && !$this->_confirmed) { |
if ($this->_form_content->has_confirm() && !$this->_confirmed) { |
264 |
return $this->render_confirm($indent_level, $output_debug); |
return $this->render_confirm($indent_level, $output_debug); |
265 |
} else { |
} else { |
266 |
//Looks like the action worked |
//Looks like the action worked |
267 |
$success = $this->_form_content->form_success($this->_message); |
$success = $this->_form_content->form_success(); |
268 |
|
|
269 |
if ($this->_form_success_render) { |
if ($this->_form_success_render) { |
270 |
return $this->render_form($indent_level, $output_debug, |
return $this->render_form($indent_level, $output_debug, |
302 |
//build the $this->_form object. |
//build the $this->_form object. |
303 |
$this->_build_form_tag(); |
$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) { |
if ($obj) { |
316 |
$this->_form->add_reference( $obj ); |
$this->_form->add_reference( $obj ); |
317 |
} |
} |
325 |
//Ok lets add our hidden vars |
//Ok lets add our hidden vars |
326 |
$this->__hidden_fields(); |
$this->__hidden_fields(); |
327 |
|
|
328 |
return $this->_form->render($indent_level, $output_debug); |
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 |
/** |
/** |
352 |
$this->_build_form_tag(); |
$this->_build_form_tag(); |
353 |
|
|
354 |
//add the confirm object/html |
//add the confirm object/html |
355 |
$confirm = &$this->_form_content->form_confirm( $this->data ); |
$confirm = &$this->_form_content->form_confirm( ); |
356 |
$this->_form->add_reference( $confirm ); |
$this->_form->add_reference( $confirm ); |
357 |
|
|
358 |
//ok add all of the submitted data as hidden form fields |
//ok add all of the submitted data as hidden form fields |
377 |
* @return raw html |
* @return raw html |
378 |
*/ |
*/ |
379 |
function render_error( $indent_level, $output_debug) { |
function render_error( $indent_level, $output_debug) { |
380 |
$wrapper_div = new DIVtag; |
|
381 |
|
if ($this->_auto_show_errors) { |
382 |
//Ok first lets build the error table |
//Ok first lets build the error table |
383 |
$errors = &$this->_form_content->form_errors(); |
$wrapper = new DIVtag; |
384 |
if ($errors != NULL) $wrapper_div->add( $errors, html_br() ); |
$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_div); |
return $this->render_form( $indent_level, $output_debug, $wrapper); |
392 |
} |
} |
393 |
|
|
394 |
|
|
417 |
* is off |
* is off |
418 |
* |
* |
419 |
*/ |
*/ |
420 |
function set_render_form_after_success() { |
function set_render_form_after_success($flag=TRUE) { |
421 |
$this->_form_success_render = TRUE; |
$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 |
|
|
643 |
* @return FORMtag object. |
* @return FORMtag object. |
644 |
*/ |
*/ |
645 |
function _build_form_tag() { |
function _build_form_tag() { |
646 |
$form_attrs = array("method" => "POST", |
$form_attrs = array(); |
647 |
"action" => $this->_form_action, |
foreach( $this->_form_attributes as $name => $value) { |
648 |
"name" => $this->_form_name, |
if ($value) { |
649 |
"style" => "margin: 0px 0px 0px 0px;"); |
$form_attrs[$name] = $value; |
650 |
if ($this->_enctype != NULL) |
} |
651 |
$form_attrs["enctype"] = $this->_enctype; |
} |
|
if ($this->_target != NULL) |
|
|
$form_attrs["target"] = $this->_target; |
|
|
if ($this->_onsubmit != NULL) |
|
|
$form_attrs["onSubmit"] = $this->_onsubmit; |
|
|
|
|
652 |
$this->_form = new FORMtag( $form_attrs ); |
$this->_form = new FORMtag( $form_attrs ); |
653 |
} |
} |
654 |
|
|
703 |
form_hidden(FORM_VISITED,1) ); |
form_hidden(FORM_VISITED,1) ); |
704 |
|
|
705 |
if ($this->_form_content->has_confirm() && !$this->_confirmed) { |
if ($this->_form_content->has_confirm() && !$this->_confirmed) { |
706 |
if (!$_POST[FORM_VISITED] || $this->_has_errors) { |
if (@!$_REQUEST[FORM_VISITED] || $this->_has_errors) { |
707 |
$this->_form->add( form_hidden(FORM_CONFIRM, 0 ) ); |
$this->_form->add( form_hidden(FORM_CONFIRM, 0 ) ); |
708 |
} else { |
} else { |
709 |
$this->_form->add( form_hidden(FORM_CONFIRM, 1 ) ); |
$this->_form->add( form_hidden(FORM_CONFIRM, 1 ) ); |
713 |
$this->_form->add( form_hidden(FORM_CONFIRM, 0 ) ); |
$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 |
?> |
?> |