/[cvs]/nfo/php/libs/com.newsblob.phphtmllib/form/FormProcessor.inc
ViewVC logotype

Diff of /nfo/php/libs/com.newsblob.phphtmllib/form/FormProcessor.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.2 by jonen, Thu Apr 3 23:47:38 2003 UTC revision 1.4 by jonen, Thu May 6 16:27:22 2004 UTC
# Line 13  Line 13 
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
# Line 30  define(FORM_CONFIRM, "_form_confirm"); Line 30  define(FORM_CONFIRM, "_form_confirm");
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
# Line 77  class FormProcessor extends Container { Line 71  class FormProcessor extends Container {
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
# Line 84  class FormProcessor extends Container { Line 105  class FormProcessor extends Container {
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
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       * This method does the logic of
146       * doing the form processing       * doing the form processing
147       */       */
148      function _process_form() {      function _process_form() {
149          //let the form build the FormElement objects          $this->_init_form_content();
         //that will be used by the form  
         $this->_form_content->form_init_elements();  
   
         //first we need to  
         if (!$_POST[FORM_VISITED]) {  
             $this->_form_content->form_init_data();  
         }  
   
150    
151          //we only need to process the form          //we only need to process the form
152          //if it has been visited. Otherwise          //if it has been visited. Otherwise
153          //it just gets rendered.          //it just gets rendered.
154          if ($_POST[FORM_VISITED] == 1) {          if (!empty($_REQUEST[FORM_VISITED]) && $_REQUEST[FORM_VISITED] == 1) {
155              $this->_form_content->set_action($_POST[FORM_ACTION]);              $this->_set_action();
156    
157              //let see if this was a confirmation page.              //let see if this was a confirmation page.
158              if ( $_POST[FORM_CONFIRM] == 1 ) {              if ( !empty($_REQUEST[FORM_CONFIRM]) ) {
159                  //looks like this was a submit on a                  if ($_REQUEST[FORM_CONFIRM] == 1) {
160                  //confirmation page.  we don't need                      //looks like this was a submit on a
161                  //to do form field validation.                      //confirmation page.  we don't need
162                  $this->_confirmed = TRUE;                      //to do form field validation.
163                        $this->_confirmed = TRUE;
164                    } else {
165                        //looks like the confirmation was aborted.
166                        xxx("aborted");
167                    }
168              }              }
169    
170              //now do the validation              //now do the validation
# Line 147  class FormProcessor extends Container { Line 186  class FormProcessor extends Container {
186                          $this->_has_errors = !$this->_form_content->form_backend_validation();                          $this->_has_errors = !$this->_form_content->form_backend_validation();
187                          if (!$this->_has_errors) {                          if (!$this->_has_errors) {
188                              $this->_has_errors = !$this->_process_action();                              $this->_has_errors = !$this->_process_action();
189                                if (!$this->_has_errors) {
190                                    $this->_set_confirmed_success(TRUE);
191                                }
192                          }                          }
193                      }                      }
194                  }                  }
# Line 155  class FormProcessor extends Container { Line 197  class FormProcessor extends Container {
197                  $this->_has_errors = !$this->_form_content->form_backend_validation();                  $this->_has_errors = !$this->_form_content->form_backend_validation();
198                  if (!$this->_has_errors) {                  if (!$this->_has_errors) {
199                      $this->_has_errors = !$this->_process_action();                      $this->_has_errors = !$this->_process_action();
200                        if (!$this->_has_errors) {
201                            $this->_set_confirmed_success(TRUE);
202                        }
203                  }                                  }                
204              }              }
205          }          }
# Line 179  class FormProcessor extends Container { Line 224  class FormProcessor extends Container {
224       * confirmation page is rendered       * confirmation page is rendered
225       */       */
226      function _pre_confirm() {      function _pre_confirm() {
227    
228            if ($this->_form_content->_has_file_element) {
229                //we need to allow any/all of the file elements
230                //save the temp files during a confirmation.
231                //if we don't, then the web server may delete
232                //them before confirmation has been accepted.
233                $this->_form_content->_pre_confirm();
234            }
235    
236            //call the user defineable FormContent pre_confirm.
237          return $this->_form_content->pre_confirm();          return $this->_form_content->pre_confirm();
238      }      }
239    
# Line 191  class FormProcessor extends Container { Line 246  class FormProcessor extends Container {
246      function do_validation() {      function do_validation() {
247          $keys = array_keys( $this->_form_content->_elements );          $keys = array_keys( $this->_form_content->_elements );
248          foreach( $keys as $key ) {          foreach( $keys as $key ) {
249              $valid = $this->_form_content->_elements[$key]->_do_validation();              $valid = $this->_form_content->_elements[$key]->_do_validation($this->_FormValidation);
250              if (!$valid) {              if (!$valid) {
251                  $this->_has_errors = TRUE;                  $this->_has_errors = TRUE;
252              }              }
# Line 204  class FormProcessor extends Container { Line 259  class FormProcessor extends Container {
259       *       *
260       */       */
261      function render($indent_level=0, $output_debug=0) {      function render($indent_level=0, $output_debug=0) {
         //now process the form  
         $this->_process_form();  
   
262          if ($this->_has_errors) {          if ($this->_has_errors) {
263              //we need to render the form errors              //we need to render the form errors
264              //and then the form              //and then the form
265              return $this->render_error($indent_level, $output_debug);              return $this->render_error($indent_level, $output_debug);
266          } else {          } else {
267              //there are no errors!              //there are no errors!
268              if ($_POST[FORM_VISITED] == 1) {              if (@$_REQUEST[FORM_VISITED] == 1) {
269                  //looks like the form has been processed?                  //looks like the form has been processed?
270                  if ($this->_form_content->has_confirm() && !$this->_confirmed) {                  if ($this->_form_content->has_confirm() && !$this->_confirmed) {
271                      return $this->render_confirm($indent_level, $output_debug);                      return $this->render_confirm($indent_level, $output_debug);
272                  } else {                  } else {
273                      //Looks like the action worked                      //Looks like the action worked
274                      $success = $this->_form_content->form_success($this->_message);                      $success = $this->_form_content->form_success();
275                                            
276                      if ($this->_form_success_render) {                      if ($this->_form_success_render) {
277                          return $this->render_form($indent_level, $output_debug,                          return $this->render_form($indent_level, $output_debug,
# Line 257  class FormProcessor extends Container { Line 309  class FormProcessor extends Container {
309          //build the $this->_form object.          //build the $this->_form object.
310          $this->_build_form_tag();          $this->_build_form_tag();
311    
312            //check to see if the form_content
313            //has any js, or any of the form elements
314            //have js.
315            $form_js = $this->_build_javascript();
316            if (strlen($form_js) > 0) {
317                $script = html_script();
318                $script->add( $form_js );
319                //$this->_form->add( $script );
320            }
321    
322          if ($obj) {          if ($obj) {
323              $this->_form->add_reference( $obj );              $this->_form->add_reference( $obj );
324          }          }
# Line 270  class FormProcessor extends Container { Line 332  class FormProcessor extends Container {
332          //Ok lets add our hidden vars          //Ok lets add our hidden vars
333          $this->__hidden_fields();          $this->__hidden_fields();
334    
335          return $this->_form->render($indent_level, $output_debug);          if (isset($script)) {
336                $c = container( $script, $this->_form );
337                return $c->render($indent_level, $output_debug);
338            } else {
339                return $this->_form->render($indent_level, $output_debug);
340            }
341            
342      }      }
343    
344      /**      /**
# Line 291  class FormProcessor extends Container { Line 359  class FormProcessor extends Container {
359          $this->_build_form_tag();          $this->_build_form_tag();
360    
361          //add the confirm object/html          //add the confirm object/html
362          $confirm = &$this->_form_content->form_confirm( $this->data );          $confirm = &$this->_form_content->form_confirm( );
363          $this->_form->add_reference( $confirm );          $this->_form->add_reference( $confirm );
364    
365          //ok add all of the submitted data as hidden form fields          //ok add all of the submitted data as hidden form fields
# Line 316  class FormProcessor extends Container { Line 384  class FormProcessor extends Container {
384       * @return raw html       * @return raw html
385       */       */
386      function render_error( $indent_level, $output_debug) {      function render_error( $indent_level, $output_debug) {
387          $wrapper_div = new DIVtag;          
388            if ($this->_auto_show_errors) {
389          //Ok first lets build the error table              //Ok first lets build the error table
390          $errors = &$this->_form_content->form_errors();              $wrapper = new DIVtag;
391          if ($errors != NULL) $wrapper_div->add( $errors, html_br() );              $errors = &$this->_form_content->form_errors();
392                if ($errors != NULL) $wrapper->add( $errors, html_br() );
393            } else {
394                $wrapper = NULL;
395            }
396            
397    
398          return $this->render_form( $indent_level, $output_debug, $wrapper_div);          return $this->render_form( $indent_level, $output_debug, $wrapper);
399      }      }
400    
401    
# Line 351  class FormProcessor extends Container { Line 424  class FormProcessor extends Container {
424       * is off       * is off
425       *       *
426       */       */
427      function set_render_form_after_success() {      function set_render_form_after_success($flag=TRUE) {
428          $this->_form_success_render = TRUE;          $this->_form_success_render = $flag;
429        }
430    
431        /**
432         * This is used to test to see if the form action
433         * was processed succesfully.
434         * This is usefull for external entities to determine
435         * if the form was processed, and it was successfull.
436         *
437         * @return boolean
438         */
439        function is_action_successfull() {
440            return $this->_confirmed_successfull;
441        }
442    
443        /**
444         * This flag sets the flag that tells
445         * if we successfully confirmed the form,
446         * and processed the action
447         *
448         * @param boolean
449         */
450        function _set_confirmed_success($flag=TRUE) {
451            $this->_confirmed_successfull = $flag;
452        }
453    
454        /**
455         * This sets the flag that tells this class
456         * to automatically call the form contents
457         * form errors and display it or not
458         *
459         * @param boolean - show errors?
460         */
461        function set_auto_error_display($flag=TRUE) {
462            $this->_auto_show_errors = $flag;
463        }
464    
465        /**
466         * This gets the current value of the flag
467         * that tells us to show form errors automatically
468         * or not.
469         *
470         * @return boolean
471         */
472        function get_auto_error_display() {
473            return $this->_auto_show_errors;
474        }
475    
476    
477        /**
478         * This method allows us to get access to the
479         * errors display object that is generated by
480         * the form content.  This is the display
481         * object that is meant to be rendered directly.
482         * If there are no errors. we will return NULL
483         *
484         * @return object
485         */
486        function &get_error_display_object() {
487            if ($this->_has_errors) {
488                return $this->_form_content->form_errors();
489            } else {
490                return NULL;
491            }
492        }
493    
494    
495        /**
496         * This method returns an array of errors that
497         * happened in the form.
498         *
499         * @return array
500         */
501        function get_error_array() {
502            return $this->_form_content->get_error_array();
503        }
504    
505        /**
506         * This returns the flag that tells us that
507         * the form has errors during processing
508         *
509         * @return boolean
510         */
511        function has_errors() {
512            return $this->_has_errors;
513        }
514    
515    
516    
517    
518        //************************************************//
519        //*       FORMtag Attributes for this class      *//
520        //************************************************//
521    
522        /**
523         * This function is used to set the
524         * form name
525         *
526         * @param string
527         */
528        function set_form_name($name) {
529            $this->_form_attributes["name"] = $name;
530        }
531    
532        /**
533         * This function is used to get
534         * the form name
535         *
536         * @return string
537         */
538        function get_form_name() {
539            return $this->_form_attributes["name"];
540        }
541    
542        /**
543         * This function is used to set the
544         * form target
545         *
546         * @param string
547         */
548        function set_form_target($target) {
549            $this->_form_attributes["target"] = $target;
550        }
551    
552        /**
553         * This function is used to get
554         * the form target
555         *
556         * @return string
557         */
558        function get_form_target() {
559            return $this->_form_attributes["target"];
560        }
561    
562        /**
563         * This function is used to set the
564         * form method
565         *
566         * @param string (POST or GET)
567         */
568        function set_form_method($method) {
569            if ( strcasecmp($method,"GET") !=0 && strcasecmp($method,"POST") !=0 ) {
570                user_error("FormProcessor::set_form_method() - INVALID Form method ".$method);
571            } else {
572                $this->_form_attributes["method"] = $method;
573            }
574        }
575    
576        /**
577         * This function is used to get
578         * the form method
579         *
580         * @return string (POST or GET)
581         */
582        function get_form_method() {
583            return $this->_form_attributes["method"];
584        }
585    
586        /**
587         * Sets the form action
588         *
589         * @param string
590         */
591        function set_form_action($action) {
592            $this->_form_attributes["action"] = $action;
593        }
594    
595        /**
596         * This function is used to get
597         * the form action
598         *
599         * @return string (POST or GET)
600         */
601        function get_form_action() {
602            return $this->_form_attributes["action"];
603        }
604    
605        /**
606         * Sets the form enctype
607         *
608         * @param string
609         */
610        function set_form_enctype($enctype) {
611            $this->_form_attributes["enctype"] = $enctype;
612        }
613    
614        /**
615         * This function is used to get
616         * the form enctype value
617         *
618         * @return string
619         */
620        function get_form_enctype() {
621            return $this->_form_attributes["enctype"];
622        }
623    
624    
625        /**
626         * This is used to set the action
627         * submitted by the user
628         *
629         */
630        function _set_action() {
631            $this->_form_submit_action = $_REQUEST[FORM_ACTION];
632            $this->_form_content->set_action($this->_form_submit_action);
633        }
634    
635        /**
636         * This is used to get the action that was
637         * processed by the form
638         *
639         * @return string
640         */
641        function get_action() {
642            return $this->_form_submit_action;
643        }
644    
645    
646        /**
647         * Set the onsubmit attribute to the form
648         * NOTE: The FormContent child can automatically
649         * set this value depending on the FormElement
650         * children it contains.
651         *
652         * @param string
653         * @return none
654         */
655        function set_onsubmit($js) {
656            $this->_form_attributes["onsubmit"] = $js;
657        }
658    
659        /**
660         * Gets the current value of the form tag's
661         * onsubmit value
662         *
663         * @return string
664         */
665        function get_onsubmit() {
666            return $this->_form_attributes["onsubmit"];
667        }
668    
669    
670        //************************************//
671        //*       Some Private methods       *//
672        //************************************//
673    
674        /**
675         * This method initializes the FormContent
676         * during processing.
677         *
678         * @return none
679         */
680        function _init_form_content() {
681            //let the form build the FormElement objects
682            //that will be used by the form
683            $this->_form_content->form_init_elements();
684    
685            //first we need to
686            if (!@$_REQUEST[FORM_VISITED]) {
687                $this->_form_content->form_init_data();
688            }
689    
690            //see if the form content has a child of the
691            //FEFile element, so we can automatically
692            //add the enctype to the form tag attribute
693            if ($this->_form_content->_has_file_element) {
694                $this->set_form_enctype("multipart/form-data");
695            }      
696      }      }
697    
698    
# Line 363  class FormProcessor extends Container { Line 703  class FormProcessor extends Container {
703       * @return FORMtag object.       * @return FORMtag object.
704       */       */
705      function _build_form_tag() {      function _build_form_tag() {
706          $form_attrs = array("method" => "POST",          //see if we need to add the onsubmit attribute to the form
707                              "action" => $this->_form_action,          //this only needs to happen on the non-confirmation
708                              "name" => $this->_form_name,          //portion of the forms.
709                                                          "style" => "margin: 0px 0px 0px 0px;");          if (!isset($_REQUEST[FORM_VISITED])) {
710          if ($this->_enctype != NULL)              if (strlen($this->_form_content->_form_on_submit) > 0) {
711              $form_attrs["enctype"] = $this->_enctype;                  $set = TRUE;
712          if ($this->_target != NULL)                  $this->set_onsubmit( $this->get_onsubmit().$this->_form_content->_form_on_submit );
713              $form_attrs["target"] = $this->_target;              }
714          if ($this->_onsubmit != NULL)          } else {
715              $form_attrs["onSubmit"] = $this->_onsubmit;              //re-rendering the form and it has errors.
716                //we need the onsubmit if they have it.
717                if ($_REQUEST[FORM_VISITED] && $this->_has_errors) {
718                    if (strlen($this->_form_content->_form_on_submit) > 0) {
719                        $set = TRUE;
720                        $this->set_onsubmit( $this->get_onsubmit().$this->_form_content->_form_on_submit );
721                    }
722                }
723    
724                //form has been confirmed lets add it
725                //in case we are showing the form again
726                if (isset($_REQUEST[FORM_CONFIRM]) && $_REQUEST[FORM_CONFIRM] == 1) {
727                    if (strlen($this->_form_content->_form_on_submit) > 0) {
728                        $set = TRUE;
729                        $this->set_onsubmit( $this->get_onsubmit().$this->_form_content->_form_on_submit );
730                    }
731                }
732            }
733    
734            $form_attrs = array();
735            foreach( $this->_form_attributes as $name => $value) {
736                if ($value) {
737                    $form_attrs[$name] = $value;
738                }
739            }      
740          $this->_form = new FORMtag( $form_attrs );          $this->_form = new FORMtag( $form_attrs );
741      }      }
742    
# Line 385  class FormProcessor extends Container { Line 748  class FormProcessor extends Container {
748      function _add_confirm_data() {      function _add_confirm_data() {
749          $keys = array_keys( $this->_form_content->_elements );          $keys = array_keys( $this->_form_content->_elements );
750          foreach( $keys as $key ) {          foreach( $keys as $key ) {
751              $element_name = $this->_form_content->_elements[$key]->get_element_name();              $this->_form->add($this->_form_content->_elements[$key]->get_confirm_element());
             $values = $this->_form_content->_elements[$key]->get_value();  
             if (is_array($values)) {  
                 foreach($values as $value) {  
                     $this->_form->add( form_hidden($element_name, $value) );  
                 }                  
             } else {  
                 $this->_form->add( form_hidden($element_name, $values) );  
   
             }  
752          }          }
753    
754          $keys = array_keys( $this->_form_content->_hidden_elements );          $keys = array_keys( $this->_form_content->_hidden_elements );
755          foreach( $keys as $key ) {          foreach( $keys as $key ) {
756              $this->_form->add( $this->_form_content->_hidden_elements[$key]->get_element() );              $this->_form->add( $this->_form_content->_hidden_elements[$key]->get_confirm_element() );
757          }          }
758      }      }
759    
# Line 428  class FormProcessor extends Container { Line 782  class FormProcessor extends Container {
782                             form_hidden(FORM_VISITED,1) );                             form_hidden(FORM_VISITED,1) );
783    
784          if ($this->_form_content->has_confirm() && !$this->_confirmed) {          if ($this->_form_content->has_confirm() && !$this->_confirmed) {
785              if (!$_POST[FORM_VISITED] || $this->_has_errors) {              if (@!$_REQUEST[FORM_VISITED] || $this->_has_errors) {
786                  $this->_form->add( form_hidden(FORM_CONFIRM, 0 ) );                  $this->_form->add( form_hidden(FORM_CONFIRM, 0 ) );
787              } else {              } else {
788                  $this->_form->add( form_hidden(FORM_CONFIRM, 1 ) );                  $this->_form->add( form_hidden(FORM_CONFIRM, 1 ) );
# Line 438  class FormProcessor extends Container { Line 792  class FormProcessor extends Container {
792              $this->_form->add( form_hidden(FORM_CONFIRM, 0 ) );              $this->_form->add( form_hidden(FORM_CONFIRM, 0 ) );
793          }          }
794      }      }
795    
796        /**
797         * This method is used to build any Javascript
798         * that is used by the form and/or the form elements
799         * used in the form.
800         *
801         * @return string
802         */
803        function _build_javascript() {
804            $form_js = $this->_form_content->javascript();
805    
806            //now walk each form element and try to get any js
807            foreach( $this->_form_content->_elements as $element ) {
808                $form_js .= $element->javascript();
809            }
810    
811            return $form_js;
812        }
813  }  }
814  ?>  ?>

Legend:
Removed from v.1.2  
changed lines
  Added in v.1.4

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed