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

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

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

revision 1.3 by jonen, Thu May 6 16:27:22 2004 UTC revision 1.4 by jonen, Thu Aug 11 14:09:26 2005 UTC
# Line 4  Line 4 
4   *   *
5   * $Id$   * $Id$
6   *   *
7   * @author Walter A. Boring IV <waboring@buildabetterweb.com>   * @author Walter A. Boring IV <waboring@newsblob.com>
8   * @author Suren Markossian <suren@cbestwhat?>   * @author Suren Markossian <suren@cbestwhat?>
9   * @package phpHtmlLib   * @package phpHtmlLib
10   * @subpackage FormProcessing   * @subpackage FormProcessing
# Line 21  require_once($phphtmllib."/form/FormProc Line 21  require_once($phphtmllib."/form/FormProc
21  /**  /**
22   * This class is used to build and render the form.   * This class is used to build and render the form.
23   * It builds a form by creating FormElement objects   * It builds a form by creating FormElement objects
24   * which have automatic validation.  It leaves the   * which have automatic validation.  It leaves the
25   * layout of the form up to the child class.  It has   * layout of the form up to the child class.  It has
26   * a mechanism in place to automagically show a   * a mechanism in place to automagically show a
27   * confirmation 'page' after the data has been submitted   * confirmation 'page' after the data has been submitted
28   * and validated.  It also provides a hook for any   * and validated.  It also provides a hook for any
29   * 'back end' validation of data.  Finally, it provides   * 'back end' validation of data.  Finally, it provides
30   * a function for handling the action of the form, which   * a function for handling the action of the form, which
31   * only gets call after ALL validation has passed.   * only gets call after ALL validation has passed.
# Line 36  require_once($phphtmllib."/form/FormProc Line 36  require_once($phphtmllib."/form/FormProc
36   *   build the FormElement objects that will be used   *   build the FormElement objects that will be used
37   *   by the form.  This function is called EVERY time   *   by the form.  This function is called EVERY time
38   *   the FormContent class is instantiated.  After you   *   the FormContent class is instantiated.  After you
39   *   create the FormElement, you call the   *   create the FormElement, you call the
40   *   FormContent::add_element() method, to add the   *   FormContent::add_element() method, to add the
41   *   FormElement object to the form.  You will then   *   FormElement object to the form.  You will then
42   *   call the 2 methods FormContent::element_label() and   *   call the 2 methods FormContent::element_label() and
43   *   FormContent::element_form() to get access to the   *   FormContent::element_form() to get access to the
# Line 60  require_once($phphtmllib."/form/FormProc Line 60  require_once($phphtmllib."/form/FormProc
60   *   *
61   *   *
62   * form_backend_validation() - This method enables you to   * form_backend_validation() - This method enables you to
63   *   do any "back end" validation of data.  Such as, check   *   do any "back end" validation of data.  Such as, check
64   *   for a duplicate in the DB on a create/new form.  This   *   for a duplicate in the DB on a create/new form.  This
65   *   is called after the FormElement's validation methods have   *   is called after the FormElement's validation methods have
66   *   passed.   *   passed.
67   *   *
68   * form_action() - This method is called after ALL validation   * form_action() - This method is called after ALL validation
69   *   was successfull, including each FormElement object's   *   was successfull, including each FormElement object's
70   *   validation methods, as well as the FormContent::form_backend_validation()   *   validation methods, as well as the FormContent::form_backend_validation()
71   *     *
72   *   *
73   *   *
74   * @package phpHtmlLib   * @package phpHtmlLib
# Line 171  class FormContent { Line 171  class FormContent {
171      var $_required_field_marker = "*";      var $_required_field_marker = "*";
172    
173      /**      /**
174         * The form Element's flag to add the :
175         * character at the end of the label.
176         */
177        var $_label_colon_flag = FALSE;
178    
179        /**
180       * This holds how many actions we have       * This holds how many actions we have
181       * for this form content       * for this form content
182       */       */
183      var $_action_counter = 0;      var $_action_counter = 0;
184    
185      /**      /**
186       * Automatically strip slashes from       * Automatically strip slashes from
187       * form values?       * form values?
188       */       */
189      var $_stripslashes = FALSE;      var $_stripslashes = FALSE;
# Line 197  class FormContent { Line 203  class FormContent {
203    
204      /**      /**
205       * The onsubmit value for the form       * The onsubmit value for the form
206       * tag.  FormElement childs can       * tag.  FormElement childs can
207       * automatically add to this       * automatically add to this
208       * by implementing the       * by implementing the
209       * form_tag_onsubmit() method       * form_tag_onsubmit() method
210       */       */
211      var $_form_on_submit = '';      var $_form_on_submit = '';
212        
213        /**
214         * The onsubmit value for the form
215         * tag. This is used by form action
216         * elements to add disable on submit    
217         */
218        var $_form_action_elements_on_submit = '';
219    
220        /**
221         * This is the FormValidation object
222         * used to validate the form elements
223         */
224        var $_FormValidation = NULL;
225    
226    
227            /**
228             * Indicates this entire FormContent
229             * is in readonly mode.  This forces
230             * all of the FormElement to be in
231             * readonly mode.
232             *
233             */
234            var $_is_readonly = FALSE;
235    
236    
237      function FormContent($width="100%", $cancel_action=NULL) {      function FormContent($width="100%", $cancel_action=NULL) {
# Line 267  class FormContent { Line 296  class FormContent {
296      }      }
297    
298      /**      /**
299       * This method allows this class to do any       * This method allows this class to do any
300       * data munging prior to the form_confirm       * data munging prior to the form_confirm
301       * method being called @ render time.       * method being called @ render time.
302       *       *
# Line 294  class FormContent { Line 323  class FormContent {
323              $this->_elements[$name]->_pre_confirm();              $this->_elements[$name]->_pre_confirm();
324          }          }
325      }      }
326        
327    
328          /**          /**
329           * This function is used to show an intermediary           * This function is used to show an intermediary
# Line 311  class FormContent { Line 340  class FormContent {
340       * @param string - the title for the table       * @param string - the title for the table
341       * @param boolean - show the action buttons?       * @param boolean - show the action buttons?
342           * @return mixed - either raw html, or some           * @return mixed - either raw html, or some
343           *                 container HTMLTag object.               *                 container HTMLTag object.
344           */           */
345          function form_confirm( $title = "Form Confirmation", $show_buttons=TRUE ) {          function form_confirm( $title = "Form Confirmation", $show_buttons=TRUE ) {
346          $table = new InfoTable($title, $this->_width);          $table = new InfoTable($title, $this->_width);
# Line 346  class FormContent { Line 375  class FormContent {
375       */       */
376      function build_confirm_table( &$table ) {      function build_confirm_table( &$table ) {
377          foreach( $this->_elements as $label => $element) {          foreach( $this->_elements as $label => $element) {
378              $c = container(_HTML_SPACE,_HTML_SPACE,              $c = container(_HTML_SPACE, $element->get_value_text());
                            $element->get_value_text());  
379              $c->set_collapse();              $c->set_collapse();
380              $div = html_div("", $element->get_label() );              $div = html_div("", $element->get_label($this) );
381              $div->set_style("white-space:nowrap;");              $div->set_style("white-space:nowrap;");
382              $table->add_row( $div, $c);              $table->add_row( $div, $c);
383          }          }
# Line 360  class FormContent { Line 388  class FormContent {
388       * have all been validated, and the form_confirm       * have all been validated, and the form_confirm
389       * has been confirmed.  It enables the form to       * has been confirmed.  It enables the form to
390       * validate any data against a DB or other backend       * validate any data against a DB or other backend
391       * processing.  This will always get called       * processing.  This will always get called
392       * before the form_action method is called to ensure       * before the form_action method is called to ensure
393       * that all form data is valid before form_action()       * that all form data is valid before form_action()
394       * is called.       * is called.
# Line 429  class FormContent { Line 457  class FormContent {
457           * @return TABLEtag object.           * @return TABLEtag object.
458           */           */
459          function form_errors() {          function form_errors() {
460          $table = new InfoTable($this->_form_errors_title, $this->get_form_errors_width());          $table = new ErrorBoxWidget($this->_form_errors_title,
461                                                                            $this->get_form_errors_width());
462          $errors = FALSE;          $errors = FALSE;
463    
464          //walk each visible form element and see if there is an error in it                  //walk each visible form element and see if there is an error in it
465          foreach( $this->_elements as $label => $element ) {                  foreach( $this->_elements as $label => $element ) {
466              if ($element->has_error()) {                          if ($element->has_error()) {
467                  $e_errors = $element->get_errors();                                  $e_errors = $element->get_errors();
468                  foreach( $e_errors as $err ) {                                  foreach( $e_errors as $err ) {
469                      $span = new SPANtag( array("style" => "padding-right:10px;white-space:nowrap;"),                                          $table->add($err['label'], $err['message']);
470                                           $err['label'] );                                  }
471                      $table->add_row($span, $err['message']);                                  $errors = TRUE;
472                  }                          }
473                                    }
474                  $errors = TRUE;                  if ($errors) {
475              }                          return $table;
476          }                  } else {
477          if ($errors) {                          return NULL;
478              return $table;                  }
         } else {  
             return NULL;  
         }  
479          }          }
480    
481    
# Line 467  class FormContent { Line 493  class FormContent {
493                  $errors = $element->get_errors();                  $errors = $element->get_errors();
494                  foreach ( $errors as $err ) {                  foreach ( $errors as $err ) {
495                      $ret[$err['label']] = $err['message'];                      $ret[$err['label']] = $err['message'];
496                  }                                  }
497              }              }
498          }          }
499          return $ret;          return $ret;
# Line 499  class FormContent { Line 525  class FormContent {
525          if ($element->_has_form_on_submit) {          if ($element->_has_form_on_submit) {
526              $this->_form_on_submit .= $element->form_tag_onsubmit();              $this->_form_on_submit .= $element->form_tag_onsubmit();
527          }          }
528    
529            if ($this->_label_colon_flag) {
530                $element->set_colon_flag(TRUE);            
531            }
532      }      }
533    
534      /**      /**
# Line 510  class FormContent { Line 540  class FormContent {
540      function add_hidden_element( $label, $value=NULL ) {      function add_hidden_element( $label, $value=NULL ) {
541          $element = new FEHidden( $label, $value );          $element = new FEHidden( $label, $value );
542          $element->set_stripslashes( $this->_stripslashes );          $element->set_stripslashes( $this->_stripslashes );
543            $element->set_form_name( $this->_form_name );
544          $this->_hidden_elements[$label] = &$element;          $this->_hidden_elements[$label] = &$element;
545      }      }
546        
547            /**
548             * This function adds a form submit button
549             * with the appropriate action.
550             *
551             * @param string - the labe/action for the submit
552             *                 button.
553             * @param bool disable the button
554         * @param string - the onSubmit function (if any)
555         * @param boolean - disable opon submit?
556             * @return INPUTtag object
557             */
558            function add_action_element($label, $disabled = false, $submit_function = false,
559                                    $disable_on_submit = TRUE) {
560    
561            $style="vertical-align:middle;";
562                    // here we mantain a fixed button size if a label length is reasonably small
563                    // i.e. OK or Cancel buttons or use padding otherwise
564                    if (strlen($label)<8)   $style .= "width:100px;";
565                    else $style     .= "padding-left:5px;padding-right:5px;";
566                    
567                    $button_name = FORM_ACTION . $this->_action_counter++;
568                    $button_id = $this->_form_name . "action_" . $button_name;
569    
570            $attributes = array("id" => $button_id,
571                                "onclick" =>"this.form." . FORM_ACTION . ".value='".$label."';",
572                                "style" => $style);
573    
574            if ($disabled) $attributes[] = "disabled";
575    
576            $this->_action_elements[$label] = form_submit($button_name, $label, $attributes);
577    
578            if ($disable_on_submit) {
579                $this->_form_action_elements_on_submit .= "if (document.getElementById('" . $button_id . "')) document.getElementById('" . $button_id . "').disabled=true;";
580            }
581            }
582            
583            /**
584             * This function returns a submit action
585             * by label
586             *
587             * @param string label
588             *
589             * @return object
590             */
591            function get_action_element($label) {
592                return $this->_action_elements[$label];
593            }    
594    
595      /**      /**
596       * This method returns the label object       * This method returns the label object
# Line 534  class FormContent { Line 613  class FormContent {
613       */       */
614      function hidden_element_label($label) {      function hidden_element_label($label) {
615          $this->_test_element($label, "element_label", TRUE);          $this->_test_element($label, "element_label", TRUE);
616          return $this->_hidden_elements[$label]->get_label();          return $this->_hidden_elements[$label]->get_label($this);
617      }      }
618    
619      /**      /**
# Line 547  class FormContent { Line 626  class FormContent {
626       */       */
627      function element_form($label) {      function element_form($label) {
628          $this->_test_element($label, "element_form");          $this->_test_element($label, "element_form");
629          return $this->_elements[$label]->get_element();          return $this->_elements[$label]->get_form_element($this->is_readonly());
630      }      }
631    
632      /**      /**
# Line 615  class FormContent { Line 694  class FormContent {
694      /**      /**
695       * This method is a helper method to set the       * This method is a helper method to set the
696       * FormElement's tabindex.       * FormElement's tabindex.
697       *       *
698       * @param string - the form label       * @param string - the form label
699       * @param int - the FormElement's tabindex       * @param int - the FormElement's tabindex
700       * @return none       * @return none
# Line 628  class FormContent { Line 707  class FormContent {
707      /**      /**
708       * This method is a helper method to get the       * This method is a helper method to get the
709       * FormElement's tabindex.       * FormElement's tabindex.
710       *       *
711       * @param string - the form label       * @param string - the form label
712       * @param int - the FormElement's tabindex       * @param int - the FormElement's tabindex
713       * @return the FormElement's current tabindex       * @return the FormElement's current tabindex
# Line 638  class FormContent { Line 717  class FormContent {
717          return $this->_elements[$label]->get_tabindex();          return $this->_elements[$label]->get_tabindex();
718      }      }
719    
720        /**
721         * This method is used to test
722         * if the element exists in a form
723         *
724         * @param string - the form label
725         * @return bool
726         */
727        function element_exists($label) {
728            return (isset($this->_elements[$label]) || isset($this->_hidden_elements[$label]));
729        }
730    
731      /**      /**
732       * This method is used to create a new error element       * This method is used to create a new error element
733       * during the call to form_action().  This enables us       * during the call to form_action().  This enables us
734       * to do error handling during a transaction into       * to do error handling during a transaction into
735       * a DB.       * a DB.
736       *       *
737       * @param string - the label       * @param string - the label
# Line 654  class FormContent { Line 742  class FormContent {
742              $this->_elements[$label]->set_error_message($message);              $this->_elements[$label]->set_error_message($message);
743          } else {          } else {
744              $this->add_element( new FEError($label, $message) );              $this->add_element( new FEError($label, $message) );
745          }                  }
746      }      }
747    
748    
749            /**
750             * This sets the readonly flag.
751             * When this flag is set, all of the
752             * FormElements will be set to readonly.
753             *
754             * @param bool TRUE = readonly
755             */
756            function set_readonly($flag=TRUE) {
757                    $this->_is_readonly = $flag;
758            }
759    
760            /**
761             * Is this element in read only mode?
762             *
763             * @return bool
764             */
765            function is_readonly() {
766                    return $this->_is_readonly;
767            }
768    
769    
770      /*****************************/      /*****************************/
771      /*       Util methods        */      /*       Util methods        */
772      /*****************************/      /*****************************/
# Line 666  class FormContent { Line 775  class FormContent {
775          $this->_form_name = $name;          $this->_form_name = $name;
776      }      }
777    
778            function get_form_name() {
779                    return $this->_form_name;
780            }
781    
782    
783          /**          /**
784           * Save the action for the form           * Save the action for the form
# Line 709  class FormContent { Line 822  class FormContent {
822              return $this->_width;              return $this->_width;
823          } else {          } else {
824              return $this->_form_errors_width;              return $this->_form_errors_width;
825          }                  }
826      }      }
827    
828      /**      /**
# Line 718  class FormContent { Line 831  class FormContent {
831       * By default it uses the width of the       * By default it uses the width of the
832       * form as its size.       * form as its size.
833       *       *
834       * @param string - the width       * @param string - the width
835       */       */
836      function set_form_errors_width($width) {      function set_form_errors_width($width) {
837          $this->_form_errors_width = $width;          $this->_form_errors_width = $width;
# Line 734  class FormContent { Line 847  class FormContent {
847      function set_form_errors_title($title) {      function set_form_errors_title($title) {
848          $this->_form_errors_title = $title;          $this->_form_errors_title = $title;
849      }      }
850        
851        
852    
853    
854          /**          /**
# Line 815  class FormContent { Line 928  class FormContent {
928      /**      /**
929       * This sets the action message.       * This sets the action message.
930       * This is called from withint the       * This is called from withint the
931       * form_action() method       * form_action() method
932       *       *
933       * @param string - the action message       * @param string - the action message
934       */       */
# Line 826  class FormContent { Line 939  class FormContent {
939    
940      /**      /**
941       * This method sets the javasript action       * This method sets the javasript action
942       * to be taken when the cancel button       * to be taken when the cancel button
943       * is clicked.  Calling this method       * is clicked.  Calling this method
944       * w/ a non NULL value automatically enables       * w/ a non NULL value automatically enables
945       * the Cancel button.       * the Cancel button.
# Line 837  class FormContent { Line 950  class FormContent {
950          $this->_cancel_action = $action;          $this->_cancel_action = $action;
951      }      }
952    
953        /**
954         * This method sets the flag to have ALL of the
955         * FormElement's labels be appended with a :
956         * character.  Some folks like this for some
957         * completely unexplained reason.
958         */
959        function set_colon_flag($flag=TRUE) {
960            $this->_label_colon_flag = $flag;
961        }
962    
963    
964    
# Line 867  class FormContent { Line 988  class FormContent {
988          $blabel = FORM_ACTION.$this->_action_counter;          $blabel = FORM_ACTION.$this->_action_counter;
989          $this->_action_counter++;          $this->_action_counter++;
990    
991          // The onClick is responsible for:          // The onclick is responsible for:
992          // -disabling the action button to discourage stupid users          // -disabling the action button to discourage stupid users
993          // -setting the action in the _form_action hidden variable          // -setting the action in the _form_action hidden variable
994          // -calling the onSubmit function (if any), since form.submit() will not trigger          // -calling the onSubmit function (if any), since form.submit() will not trigger
# Line 883  class FormContent { Line 1004  class FormContent {
1004          if ($submit_function) {          if ($submit_function) {
1005              $onclick .= $submit_function.";";              $onclick .= $submit_function.";";
1006          }          }
1007          $onclick .= "this.form.submit();";  
1008            if ($disable_on_submit) {
1009                $onclick .= "this.form.submit();";
1010            }
1011    
1012          $attrib = array("style" => $style,          $attrib = array("style" => $style,
1013                          "onClick" => $onclick);                          "onclick" => $onclick);
1014    
1015          if ($disabled) $attrib[] = "disabled";          if ($disabled) $attrib[] = "disabled";
1016          return form_submit($blabel, $label, $attrib);          return form_submit($blabel, $label, $attrib);
# Line 923  class FormContent { Line 1047  class FormContent {
1047    
1048          $style = "padding-left: 5px;padding-right:5px;";          $style = "padding-left: 5px;padding-right:5px;";
1049          $attrib = array("style" => $style,          $attrib = array("style" => $style,
1050                          "onClick" => $onclick);                          "onclick" => $onclick);
1051    
1052          $container->push(form_submit($blabel, $label,          $container->push(form_submit($blabel, $label,
1053                                       array("style" => $style,                                       array("style" => $style,
1054                                             "onClick" => $onclick)));                                             "onclick" => $onclick)));
1055          return $container;          return $container;
1056          }          }
1057    
# Line 957  class FormContent { Line 1081  class FormContent {
1081              $click .= " if(window.global_cntr<2) {";              $click .= " if(window.global_cntr<2) {";
1082              $click .= $onclick."}";              $click .= $onclick."}";
1083          }          }
1084            
1085          $img = html_img($image_name);          $img = html_img($image_name);
1086          $img->set_tag_attribute("border", 0 );          $img->set_tag_attribute("border", 0 );
1087          $img->set_tag_attribute("style", "cursor:hand;");          $img->set_tag_attribute("style", "cursor:hand;");
# Line 969  class FormContent { Line 1093  class FormContent {
1093    
1094    
1095      /**      /**
1096       * build a cancel button with a url       * build a cancel button with a url
1097       * to go to       * to go to
1098       *       *
1099       * @param string - the cancel action       * @param string - the cancel action
1100       * @return form button       * @return form button
1101       */       */
1102      function add_cancel() {      function add_cancel() {
# Line 980  class FormContent { Line 1104  class FormContent {
1104          $cancel_button = form_button("cancel","Cancel",          $cancel_button = form_button("cancel","Cancel",
1105                                       array("type"=>"button",                                       array("type"=>"button",
1106                                             "style"=>"width: 90px;",                                             "style"=>"width: 90px;",
1107                                             "onClick"=>                                             "onclick"=>
1108                                             "javascript:document.location='".$this->_cancel_action."'"));                                             "javascript:document.location='".$this->_cancel_action."'"));
1109          $cancel_button->set_style("vertical-align:middle");          $cancel_button->set_style("vertical-align:middle");
1110          return $cancel_button;          return $cancel_button;
1111      }      }
1112        
1113      /**      /**
1114       * This function is used to set the required field marker       * This function is used to set the required field marker
1115       *       *
1116       * @param string - the marker       * @param string - the marker
1117       */       */
1118       function set_required_marker($marker) {       function set_required_marker($marker) {
1119           $this->_required_field_marker = $marker;               $this->_required_field_marker = $marker;
1120         }
1121    
1122        /**
1123         * This function is used to get the required field marker
1124         *
1125         * @return string - the marker
1126         */
1127         function get_required_marker() {
1128             if (is_object($this->_required_field_marker)) {
1129                 return $this->_required_field_marker->render();
1130             } else {
1131                 return $this->_required_field_marker;
1132             }
1133       }       }
1134        
1135       /**       /**
1136        * This sets the required text        * This sets the required text
1137        *        *
# Line 1002  class FormContent { Line 1139  class FormContent {
1139        */        */
1140        function set_required_text($text) {        function set_required_text($text) {
1141            $this->_required_field_text = $text;            $this->_required_field_text = $text;
1142          
1143        }        }
1144    
1145    
# Line 1024  class FormContent { Line 1161  class FormContent {
1161              if (!isset($this->_elements[$label])) {              if (!isset($this->_elements[$label])) {
1162                  trigger_error("FormContent::".$method."() - '".$label."' element doesn't exist", E_USER_ERROR);                  trigger_error("FormContent::".$method."() - '".$label."' element doesn't exist", E_USER_ERROR);
1163              }              }
1164          }                  }
1165      }      }
1166    
1167        /**
1168         * return the values of all visible form elements as an array
1169         *
1170         * @access public
1171         * @return array
1172         *
1173         */
1174        function get_all_element_values()
1175        {
1176    
1177            foreach ($this->_elements as $element) {
1178                $data[$element->get_element_name()] = $element->get_value();
1179            }
1180    
1181            return $data;
1182    
1183        }
1184    
1185        /**
1186         * This method is used to keep a local reference
1187         * of the FormProcessor's FormValidation object
1188         *
1189         * @param FormValidation object
1190         */
1191        function _set_validation_object(&$FormValidation) {
1192            $this->_FormValidation =& $FormValidation;
1193        }
1194    
1195  }  }
1196  ?>  ?>

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

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