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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (hide annotations)
Sat Feb 22 21:07:40 2003 UTC (21 years, 4 months ago) by jonen
Branch: MAIN
+ updated whole lib to version 2.2.1 (new FormProcessing since 2.2.0!)

1 jonen 1.1 <?php
2     /**
3     * This file contains the FormContent class.
4     *
5     * $Id: FormContent.inc,v 1.29 2003/02/20 23:58:47 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     /**
17     * Make sure we include the Processor
18     */
19     require_once($phphtmllib."/form/FormProcessor.inc");
20    
21     /**
22     * This class is used to build and render the form.
23     * It builds a form by creating FormElement objects
24     * which have automatic validation. It leaves the
25     * layout of the form up to the child class. It has
26     * a mechanism in place to automagically show a
27     * confirmation 'page' after the data has been submitted
28     * and validated. It also provides a hook for any
29     * 'back end' validation of data. Finally, it provides
30     * a function for handling the action of the form, which
31     * only gets call after ALL validation has passed.
32     *
33     * Functions:
34     *
35     * form_init_elements() - This function is used to
36     * build the FormElement objects that will be used
37     * by the form. This function is called EVERY time
38     * the FormContent class is instantiated. After you
39     * create the FormElement, you call the
40     * FormContent::add_element() method, to add the
41     * FormElement object to the form. You will then
42     * call the 2 methods FormContent::element_label() and
43     * FormContent::element_form() to get access to the
44     * FormElement's label and form field respectively.
45     *
46     * form_init_data() - This is called only the first time
47     * the form is encountered. It Allows you to populate
48     * the FormElements with data from a DB for example.
49     * You would use FormContent::set_element_value() or
50     * FormContent::set_hidden_element_value() inside here.
51     *
52     * form() - This is the method that gets called to build
53     * the layout for your form. Typically you use a table
54     * and add the label in the first <td> and the form field
55     * itself in the 2nd <td>. So there are 2 methods in
56     * the FormContent object that allow u to get access to
57     * the FormElements label, and form field.
58     * FormContent::element_label() and
59     * FormContent::element_form().
60     *
61     *
62     * form_backend_validation() - This method enables you to
63     * do any "back end" validation of data. Such as, check
64     * for a duplicate in the DB on a create/new form. This
65     * is called after the FormElement's validation methods have
66     * passed.
67     *
68     * form_action() - This method is called after ALL validation
69     * was successfull, including each FormElement object's
70     * validation methods, as well as the FormContent::form_backend_validation()
71     *
72     *
73     *
74     * @package phpHtmlLib
75     * @subpackage FormProcessing
76     */
77     class FormContent {
78    
79     /**
80     * This holds the name of the form
81     * for js that needs it
82     */
83     var $_form_name;
84    
85    
86     /**
87     * This holds the array of
88     * hidden form elements
89     * used in this form
90     */
91     var $_hidden_elements = array();
92    
93     /**
94     * This holds the array of
95     * non-hidden elements
96     */
97     var $_elements = array();
98    
99    
100     /**
101     * This holds the default
102     * css class for form field
103     * label text.
104     *
105     */
106     var $_default_label_css = "formlabel";
107    
108     /**
109     * This is the css class used
110     * for fields that have an error
111     *
112     */
113     var $_error_label_css = "formlabelerror";
114    
115     /**
116     * flag to let the FormProcessor
117     * object know this form has a
118     * confirmation page that is required
119     */
120     var $_has_confirm = FALSE;
121    
122    
123     /**
124     * Holds the width to be used for the
125     * error table
126     * DEFAULT: 95%
127     *
128     */
129     var $_width = "600";
130    
131    
132     /**
133     * The message that is set
134     * during the form_action
135     */
136     var $_action_message = "";
137    
138    
139     /**
140     * The action to take opon clicking
141     * the "Cancel" button
142     */
143     var $_cancel_action = NULL;
144    
145    
146     /**
147     * Text to show denoted required
148     * fields for the form.
149     *
150     */
151     var $_required_field_text = " - required field";
152    
153     /**
154     * marker for the required field
155     *
156     */
157     var $_required_field_marker = "*";
158    
159    
160    
161     function FormContent($width="100%", $cancel_action=NULL) {
162     $this->set_form_width( $width );
163     $this->set_cancel_action( $cancel_action );
164     }
165    
166     /**
167     * This method is what is called to
168     * build the list of FormElements that
169     * will be used by this form.
170     *
171     */
172     function form_init_elements() {
173     user_error("FormContent::form_init_elements() - Child class must override");
174     }
175    
176    
177     /**
178     * This method is called by the
179     * Form Processor to allow this
180     * class to do any fetching of
181     * data to prepopulate the
182     * form field values.
183     * You typically use this to
184     * read data from a DB.
185     *
186     * This method is only called once
187     * when the form is first hit.
188     *
189     * NOTE: you should build the data
190     * and save it in $this->_init_data
191     */
192     function form_init_data() {
193     $this->_init_data = array();
194     }
195    
196    
197     /**
198     * This method builds the html form.
199     * It is up to the child class to define
200     * the layout of the form and return it
201     * in a phpHtmllib container.
202     *
203     * @return Container
204     */
205     function form() {
206     user_error("FormContent::form() - Child class must override");
207     return NULL;
208     }
209    
210     /**
211     * This method allows this class to do any
212     * data munging prior to the form_confirm
213     * method being called @ render time.
214     *
215     * @return TRUE = success
216     * FALSE if u want to trigger an error
217     */
218     function pre_confirm() {
219     return TRUE;
220     }
221    
222    
223    
224     /**
225     * This function is used to show an intermediary
226     * confirmation page. Use this function to
227     * show a confirmation of the data that was
228     * submitted by the user.
229     * This will get called after all of the
230     * form data was successfully validated.
231     * All of the form data will automatically
232     * be created as hidden form fields. All you
233     * have to do is show the data, and a confirm
234     * submit button.
235     *
236     * @return mixed - either raw html, or some
237     * container HTMLTag object.
238     */
239     function form_confirm( ) {
240     $table = new InfoTable("Form Confirmation", $this->_width);
241    
242     $this->build_confirm_table( $table );
243    
244     //now add the confirmation button
245     $td = new TDtag(array("colspan" => 2,
246     "class" => "contentnovertical",
247     "align" => "center"),
248     $this->add_action("Confirm"));
249    
250     if ($this->_cancel_action) {
251     $td->add(_HTML_SPACE, $this->add_cancel());
252     }
253    
254     $table->add_row( $td );
255    
256     return $table;
257     }
258    
259    
260     /**
261     * This method allows the child to ovveride the
262     * default confirm data. By default the form_confirm()
263     * will show ALL FormElements. This is prolly not good
264     * in case of a form where a password exists.
265     *
266     * @param InfoTable object
267     */
268     function build_confirm_table( &$table ) {
269     foreach( $this->_elements as $label => $element) {
270     $c = container(_HTML_SPACE);
271     $c->set_collapse();
272     $value = $element->get_value();
273     if (is_array($value)) {
274     $str = '';
275     foreach($value as $item) {
276     $str .= $element->get_value_text($item).",";
277     }
278     $str = substr($str,0,strlen($str)-1);
279     $c->add( _HTML_SPACE, $str );
280     } else {
281     if (is_a($element, "FEPassword")) {
282     $value = str_repeat('*', strlen($value));
283     $c->add(_HTML_SPACE, $value);
284     } else {
285     $c->add(_HTML_SPACE, $element->get_value_text($value));
286     }
287     }
288     $div = html_div("", $element->get_label() );
289     $div->set_style("white-space:nowrap;");
290     $table->add_row( $div, $c);
291     }
292     }
293    
294     /**
295     * This method is called after the FormElements
296     * have all been validated, and the form_confirm
297     * has been confirmed. It enables the form to
298     * validate any data against a DB or other backend
299     * processing. This will always get called
300     * before the form_action method is called to ensure
301     * that all form data is valid before form_action()
302     * is called.
303     *
304     *
305     * @return boolean TRUE if successfull
306     * FALSE if errors were detected.
307     */
308     function form_backend_validation() {
309     return TRUE;
310     }
311    
312    
313     /**
314     * This method handles the
315     * action (submit button) that
316     * was submitted. This method
317     * only gets called AFTER all
318     * data has been validated.
319     * This includes the backend
320     * validation. If the form
321     * has the confirmation on,
322     * then this method will be
323     * called after the confirmation
324     * has been accepted.
325     *
326     * NOTE : return TRUE if the action
327     * was succesfully handled.
328     * If FALSE is returned, the
329     * form will be displayed again
330     * with the error message.
331     *
332     * @param array - array of errors if any
333     * @return boolean TRUE = success
334     * FALSE = failed.
335     */
336     function form_action() {
337     user_error("FormContent::form_action() - Child class ".
338     "must override");
339     return FALSE;
340     }
341    
342    
343     /**
344     * This method is called when the form_action()
345     * was successfull, and the form wants to render
346     * some kind of message
347     *
348     * @return mixed
349     */
350     function form_success() {
351     return container($this->_action_message, html_br(2));
352     }
353    
354     /**
355     * This function is used to render the error
356     * table for the form. The error data comes
357     * from the FormProcessor or the FormValidation.
358     * NOTE: You can override this method to
359     * show a customized error message(s)
360     *
361     * @return TABLEtag object.
362     */
363     function form_errors() {
364     $table = new InfoTable("Form Errors", $this->_width);
365     $errors = FALSE;
366    
367     //walk each visible form element and see if there is an error in it
368     foreach( $this->_elements as $label => $element ) {
369     if ($element->has_error()) {
370     $span = new SPANtag( array("style" => "padding-right:10px;white-space:nowrap;"),
371     $label );
372     $table->add_row($span, $element->get_error_message());
373     $errors = TRUE;
374     }
375     }
376     if ($errors) {
377     return $table;
378     } else {
379     return NULL;
380     }
381     }
382    
383    
384     /*****************************/
385     /* form element methods */
386     /*****************************/
387    
388     /**
389     * This method is used to add a
390     * form element
391     *
392     * @param FormElement object
393     */
394     function add_element( &$element ) {
395     $this->_elements[$element->get_label_text()] = &$element;
396     }
397    
398     /**
399     * This method is used to add a
400     * hidden form field
401     *
402     * @param FormElement object
403     */
404     function add_hidden_element( $label, $value=NULL ) {
405     $element = new FEHidden( $label, $value );
406     $this->_hidden_elements[$label] = &$element;
407     }
408    
409     /**
410     * This method returns the label object
411     * for a visible form element.
412     *
413     * @param string - the element's label
414     * @return Object
415     */
416     function element_label($label) {
417     return $this->_elements[$label]->get_label();
418     }
419    
420     /**
421     * This method returns the actual form
422     * object that renders the form field.
423     * Such as an INPUTtag object.
424     *
425     * @param string - the element's label
426     * @return Object
427     */
428     function element_form($label) {
429     return $this->_elements[$label]->get_element();
430     }
431    
432     /**
433     * This method returns the FormElement
434     * based on the label.
435     *
436     * @param string - the element's label
437     * @return Object
438     */
439     function &get_element($label) {
440     return $this->_elements[$label];
441     }
442    
443     /**
444     * This method is used to set
445     * the value for a non hidden
446     * element
447     *
448     * @param string - the form label
449     * @param value - the new value
450     */
451     function set_element_value($label, $value) {
452     $this->_elements[$label]->set_value($value);
453     }
454    
455     /**
456     * This method is used to get
457     * the value for a non hidden
458     * element
459     *
460     * @param string - the form label
461     * @return value - the new value
462     */
463     function get_element_value($label) {
464     return $this->_elements[$label]->get_value();
465     }
466    
467     /**
468     * This method is used to set
469     * the value for a hidden element
470     *
471     * @param string - the form label
472     * @param value - the new value
473     */
474     function set_hidden_element_value($label, $value) {
475     $this->_hidden_elements[$label]->set_value( $value );
476     }
477    
478     /**
479     * This method is used to get
480     * the value for a hidden element
481     *
482     * @param string - the form label
483     * @return value - the new value
484     */
485     function get_hidden_element_value($label) {
486     return $this->_hidden_elements[$label]->get_value();
487     }
488    
489    
490     /**
491     * This method is used to create a new error element
492     * during the call to form_action(). This enables us
493     * to do error handling during a transaction into
494     * a DB.
495     *
496     * @param string - the label
497     * @param string - the error message
498     */
499     function add_error( $label, $message ) {
500     if (isset($this->_elements[$label])) {
501     $this->_elements[$label]->set_error_message($message);
502     } else {
503     $this->add_element( new FEError($label, $message) );
504     }
505     }
506    
507    
508     /*****************************/
509     /* Util methods */
510     /*****************************/
511    
512     function set_form_name($name) {
513     $this->_form_name = $name;
514     }
515    
516    
517     /**
518     * Save the action for the form
519     *
520     * @param string - the action from the post.
521     */
522     function set_action($action) {
523     $this->action = $action;
524     }
525    
526     /**
527     * Get the current status of the
528     * action.
529     *
530     * @return string
531     */
532     function get_action() {
533     return $this->action;
534     }
535    
536     /**
537     * this method sets the form name
538     *
539     * @param string - the form name
540     */
541     function set_form_width($width) {
542     $this->_form_width = $width;
543     }
544    
545    
546     /**
547     * This function is used to set the
548     * default CSS class used on
549     * form field text when there is no
550     * error on that field
551     *
552     * @param string - the css class to use
553     */
554     function set_default_css( $css ) {
555     $this->_default_label_css = $css;
556     }
557    
558     /**
559     * This function returns the
560     * default css class used for
561     * NON error text.
562     *
563     * @return string - the current default css
564     */
565     function get_default_css() {
566     return $this->_default_label_css;
567     }
568    
569    
570     /**
571     * This function is used to set the
572     * css class that is used on text
573     * when an error on that field is
574     * detected.
575     *
576     * @param string - the css class to use.
577     */
578     function set_error_css( $css ) {
579     $this->_err_label_css = $css;
580     }
581    
582    
583     /**
584     * This sets the $this->_has_confirmation
585     * flag. to let the object know it has
586     * a required confirmation page, which
587     * should get called after the validation
588     * is successfull, and before the action is
589     * handled on the back-end.
590     *
591     * @param boolean - the flag value TRUE/FALSE
592     */
593     function set_confirm( $flag = TRUE ) {
594     $this->_has_confirm = $flag;
595     }
596    
597     /**
598     * This gets the value of the confirmation flag.
599     * which tells the caller that this object
600     * has/doesn't have a required confirmation
601     * page.
602     *
603     * @return boolean
604     */
605     function has_confirm() {
606     return $this->_has_confirm;
607     }
608    
609    
610     /**
611     * This sets the stripslashes flag for
612     * this object.
613     *
614     * @param boolean
615     */
616     function set_stripslashes( $flag = TRUE ) {
617     $this->_stripslashes = $flag;
618     }
619    
620     /**
621     * This sets the action message.
622     * This is called from withint the
623     * form_action() method
624     *
625     * @param string - the action message
626     */
627     function set_action_message($message) {
628     $this->_action_message = $message;
629     }
630    
631    
632     /**
633     * This method sets the javasript action
634     * to be taken when the cancel button
635     * is clicked. Calling this method
636     * w/ a non NULL value automatically enables
637     * the Cancel button.
638     *
639     * @param string - the javascript cancel action
640     */
641     function set_cancel_action($action) {
642     $this->_cancel_action = $action;
643     }
644    
645    
646    
647    
648    
649     /**
650     * This function adds a form submit button
651     * with the appropriate action.
652     *
653     * @param string - the labe/action for the submit
654     * button.
655     * @param bool disable the button
656     * @param string - the onSubmit function (if any)
657     * @param boolean - disable opon submit?
658     * @return INPUTtag object
659     */
660     function add_action( $label, $disabled = false, $submit_function = false,
661     $disable_on_submit = TRUE ) {
662     $style="vertical-align:middle;";
663     // here we mantain a fixed button size if a label length is reasonably small
664     // i.e. OK or Cancel buttons or use padding otherwise
665     if ( strlen($label)<8 ) $style .= "width: 90px;";
666     else $style .= "padding-left: 5px;padding-right:5px;";
667    
668     // Unique button label
669     if (!$this->_action_counter) {
670     $this->_action_counter = 1;
671     }
672     $blabel = FORM_ACTION.$this->_action_counter;
673     $this->_action_counter++;
674    
675     // The onClick is responsible for:
676     // -disabling the action button to discourage stupid users
677     // -setting the action in the _form_action hidden variable
678     // -calling the onSubmit function (if any), since form.submit() will not trigger
679     // the registered onSubmit function
680     // -call the form.submit() function
681    
682     //build the action
683     $onclick = "";
684     if ($disable_on_submit) {
685     $onclick .= "this.form.".$blabel.".disabled=true;";
686     }
687     $onclick .= "this.form.".FORM_ACTION.".value='".$label."';";
688     if ($submit_function) {
689     $onclick .= $submit_function.";";
690     }
691     $onclick .= "this.form.submit();";
692    
693     $attrib = array("style" => $style,
694     "onClick" => $onclick);
695    
696     if ($disabled) $attrib[] = "disabled";
697     return form_submit($blabel, $label, $attrib);
698     }
699    
700    
701     /**
702     * This function adds a submit button that
703     * can have any label. It just makes the
704     * _form_action a hidden field.
705     * NOTE: you can only do this ONCE per form.
706     *
707     * @param string - the label of the button.
708     * @param string - the action value.
709     * @param boolean - disable opon submit?
710     * @return ContainerWidget object
711     */
712     function add_hidden_action( $label, $action, $disable_on_submit=TRUE) {
713     $container = new ContainerWidget;
714     // Unique button label
715     if (!$this->_action_counter) {
716     $this->_action_counter = 1;
717     }
718     $blabel = FORM_ACTION.$this->_action_counter;
719     $this->_action_counter++;
720    
721     //build the action
722     $onclick = "";
723     if ($disable_on_submit) {
724     $onclick .= "this.form.".$blabel.".disabled=true;";
725     }
726     $onclick .= "this.form.".FORM_ACTION.".value='".$action."';";
727     $onclick .= "this.form.submit();";
728    
729     $style = "padding-left: 5px;padding-right:5px;";
730     $attrib = array("style" => $style,
731     "onClick" => $onclick);
732    
733     $container->push(form_submit($blabel, $label,
734     array("style" => $style,
735     "onClick" => $onclick)));
736     return $container;
737     }
738    
739     /**
740     * This function adds an action as an image to
741     * submit the form.
742     *
743     * @param string - the image name path
744     * @param string - the action
745     * @param boolean - disable opon submit?
746     *
747     * @return
748     */
749     function add_image_action( $image_name, $action, $disable_on_submit=TRUE ) {
750     $container = container();
751    
752     $container = new ContainerWidget;
753     // Unique button label
754     if (!$this->_action_counter) {
755     $this->_action_counter = 1;
756     }
757     $blabel = FORM_ACTION.$this->_action_counter;
758     $this->_action_counter++;
759    
760     $img = form_image($blabel, $action, $image_name);
761     //NS 4 hack
762     $img->set_tag_attribute("border", 0 );
763     $img->set_tag_attribute("style", "cursor:hand;");
764     //build the action
765     $onclick = "";
766     if ($disable_on_submit) {
767     $onclick .= "this.form.".$blabel.".disabled=true;";
768     }
769     $onclick .= "this.form.".FORM_ACTION.".value='".$action."';";
770     $onclick .= "this.form.submit();";
771    
772     $img->set_tag_attribute("onClick", $onclick);
773     //IE HACK
774     $container->push($img);
775    
776     return $container;
777     }
778    
779    
780     /**
781     * build a cancel button with a url
782     * to go to
783     *
784     * @param string - the cancel action
785     * @return form button
786     */
787     function add_cancel() {
788    
789     $cancel_button = form_button("cancel","Cancel",
790     array("type"=>"button",
791     "style"=>"width: 90px;",
792     "onClick"=>
793     "javascript:document.location='".$this->_cancel_action."'"));
794     $cancel_button->set_style("vertical-align:middle");
795     return $cancel_button;
796     }
797    
798    
799     /**
800     * This returns the required field text
801     * if there are any. otherwise it returns NULL
802     *
803     */
804     function get_required_fields_text() {
805     if ( count($this->required_fields) ) {
806    
807     $span = new SPANtag( array( "class" => "formlabel"),
808     $this->_required_field_marker.
809     $this->required_field_text, FALSE );
810    
811     return $span;
812     } else {
813     return "&nbsp;";
814     }
815     }
816    
817    
818     }
819     ?>

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