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

Contents of /nfo/php/libs/com.newsblob.phphtmllib/form/form_elements/FEListBox.inc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (show annotations)
Thu May 6 16:27:27 2004 UTC (20 years, 4 months ago) by jonen
Branch: MAIN
Changes since 1.2: +1046 -3 lines
 updated all to v2.4.1 - Apr 01, 2004

1 <?php
2 /**
3 * This file contains the FEListBox, FEMultiListBox
4 *
5 * $Id: FEListBox.inc,v 1.19 2004/03/28 19:46:20 culley Exp $
6 *
7 * @author Walter A. Boring IV <waboring@buildabetterweb.com>
8 * @author Suren Markosyan <suren@bcsweb.com>
9 * @package phpHtmlLib
10 * @subpackage FormProcessing
11 *
12 * @copyright LGPL - See LICENCE
13 *
14 */
15
16 /**
17 * This is the ListBox FormElement which builds a
18 * select field with all of its options.
19 * It has no validation method.
20 *
21 *
22 * @author Walter A. Boring IV <waboring@buildabetterweb.com>
23 * @author Suren Markossian <suren@bcsweb.com>
24 * @package phpHtmlLib
25 * @subpackage FormProcessing
26 *
27 * @copyright LGPL - See LICENCE
28 */
29 class FEListBox extends FEDataList {
30
31 /**
32 * This function builds and returns the
33 * form element object
34 *
35 * @return object
36 */
37 function get_element() {
38
39 $attributes = $this->_build_element_attributes();
40
41 if ($this->_height) {
42 $attributes["size"] = 5;
43 }
44
45 $selected_value = $this->get_value();
46
47 $tag = new SELECTtag($attributes);
48
49 foreach ($this->_data_list as $name=>$value) {
50
51 $attributes = array("value"=>$value);
52 if ($value == $selected_value) {
53 $attributes[] = "selected";
54 }
55 if (isset($this->_disabled_items[$name])) {
56 $attributes[] = "disabled";
57 }
58
59 $tag->add(new OPTIONtag($attributes, $name));
60 }
61 return $tag;
62 }
63 }
64
65 /**
66 * Build a Yes/No Select box
67 *
68 * @author Walter A. Boring IV <waboring@buildabetterweb.com>
69 * @author Suren Markossian <suren@bcsweb.com>
70 * @package phpHtmlLib
71 * @subpackage FormProcessing
72 *
73 * @copyright LGPL - See LICENCE
74 */
75 class FEYesNoListBox extends FEListBox {
76
77 /**
78 * The Constructor
79 *
80 * @param label string - text label for the element
81 * @param bool required - is this a required element
82 * @param array data_list - list of data elements (name=>value)
83 * @param int required - element width in characters, pixels (px), percentage (%) or elements (em)
84 * @param string - the value to use for the 'yes' radio
85 * NOTE: default is 'yes'
86 * @param string - the value to use for the 'no' radio
87 * NOTE: default is 'no'
88 */
89 function FEYesNoListBox($label, $required = TRUE, $width = NULL, $height = NULL,
90 $yes_value="yes", $no_value="no") {
91 $options = array("Yes" => $yes_value, "No" => $no_value);
92 $this->FEListBox($label, $required, $width, $height, $options);
93 }
94 }
95
96
97 /**
98 * This class builds a nested select box that
99 * is used to select an entry from nested levels.
100 *
101 * NOTE: The data array must be in the format
102 * array("test" => "testvalue",
103 * "nestedtest" => array("value" => "nestedtestvalue",
104 * "items" => array( "foo" => "foovalue",
105 * "bar" => "barvalue"));
106 *
107 * Example
108 * $data = array("Test" => 1,
109 * "Foo" => array("value" => 2,
110 * "items" => array("Blah" => 3,
111 * "php" => 4)),
112 * "Bar" => array("value" => 5,
113 * "items" => array("testing" => array("value" => 6,
114 * "items" => array("ugh" => 7)),
115 * "again" => 8)));
116 *
117 * would result in
118 *
119 * <select >
120 * <option value="1">Test</option>
121 * <option value="2">Foo</option>
122 * <option value="3">&nbsp;&nbsp;Blah</option>
123 * <option value="4">&nbsp;&nbsp;php</option>
124 * <option value="5">Bar</option>
125 * <option value="6">&nbsp;&nbsp;testing</option>
126 * <option value="7">&nbsp;&nbsp;&nbsp;&nbsp;ugh</option>
127 * <option value="8">&nbsp;&nbsp;again</option>
128 * </select>
129 *
130 *
131 * @author Walter A. Boring IV <waboring@buildabetterweb.com>
132 * @package phpHtmlLib
133 * @subpackage FormProcessing
134 *
135 * @copyright LGPL - See LICENCE
136 */
137 class FENestedListBox extends FEDataList {
138
139 /**
140 * This function builds and returns the
141 * form element object
142 *
143 * @return object
144 */
145 function get_element() {
146
147 $attributes = $this->_build_element_attributes();
148
149 if ($this->_height) {
150 $attributes["size"] = 5;
151 }
152
153 $tag = new SELECTtag($attributes);
154
155 $this->_add_array($this->_data_list, 0, $tag);
156 return $tag;
157 }
158
159
160 /**
161 *
162 * This function is responsible for performing complete
163 * validation and setting the appropriate error message
164 * in case of a failed validation
165 *
166 * @param FormValidation object
167 */
168 function validate(&$_FormValidation) {
169 // we make sure that the value is in the data list
170 $value = $this->get_value();
171
172 if (!$this->_array_search_r($value, $this->_data_list, $temp)) {
173 $this->set_error_message("Invalid value");
174 return FALSE;
175 }
176 return TRUE;
177 }
178
179
180 function _array_search_r($needle, $haystack, &$item){
181 //xxx($needle);
182 $match = FALSE;
183
184 foreach($haystack as $name=>$value){
185 if (is_array($value)) {
186 if ($value["value"] == $needle) {
187 $item = $name;
188 $match = TRUE;
189 } else {
190 $match = $this->_array_search_r($needle, $value["items"], $item);
191 $item = $name."::".$item;
192 }
193 } else if ($value==$needle) {
194 $item = $name;
195 $match = TRUE;
196 }
197
198 if ($match)
199 return TRUE;
200 }
201 return $match;
202 }
203
204
205 /**
206 * This provides a method
207 * for the FormContent
208 * to get access to the
209 * text associated with a
210 * field. This is only available
211 * on FormElements that have text
212 * associated with a field.
213 * It is used during Confirmation
214 *
215 * @return string - the text associated
216 */
217 function get_value_text() {
218 $ret = $this->_array_search_r($this->get_value(),
219 $this->_data_list, $item);
220 return $item;
221 }
222
223
224 /**
225 * This is a recursive function used to add
226 * an array of layers to the select box.
227 *
228 * @param array - the next level of name=>value pairs
229 * @param int - the level
230 * @param SELECTtag - the SELECTtag object to add the options
231 * @return none
232 */
233 function _add_array($layer_arr, $level, &$tag) {
234 if (!is_array($layer_arr)) {
235 return;
236 }
237
238 foreach( $layer_arr as $name=>$value) {
239 if (is_array($value)) {
240 $tag->add( $this->_build_option($name, $value["value"], $level));
241 $this->_add_array($value["items"], $level+1, $tag);
242 } else {
243
244 $tag->add($this->_build_option($name, $value, $level));
245 }
246 }
247 }
248
249 /**
250 * This method builds the actual OPTIONtag object
251 *
252 * @param string the name
253 * @param string the value
254 * @param int the level
255 * @return OPTIONtag object
256 */
257 function _build_option($name, $value, $level) {
258 $selected_value = $this->get_value();
259
260 $attributes = array("value"=>$value);
261 if ($value == $selected_value) {
262 $attributes[] = "selected";
263 }
264
265 if (isset($this->_disabled_items[$name])) {
266 $attributes[] = "disabled";
267 }
268
269 return new OPTIONtag($attributes, $this->_layer_name($name, $level));
270 }
271
272 /**
273 * This builds a layer's name
274 *
275 * @param string - original name
276 * @param int the layer level
277 * @return string the new name
278 */
279 function _layer_name($name, $level) {
280 $newname = '';
281
282 if ($level == 0) {
283 return $name;
284 } else {
285 $newname .= str_repeat(_HTML_SPACE, $level*2).$name;
286 }
287
288 return $newname;
289 }
290 }
291
292
293 /**
294 * This is the MultiListBox FormElement which builds a
295 * select field with all of its options. It enables
296 * the ability to have multiple selections.
297 * It has no validation method.
298 *
299 *
300 * @author Walter A. Boring IV <waboring@buildabetterweb.com>
301 * @author Suren Markossian <suren@bcsweb.com>
302 * @package phpHtmlLib
303 * @subpackage FormProcessing
304 *
305 * @copyright LGPL - See LICENCE
306 */
307 class FEMultiListBox extends FEListBox {
308
309
310 /**
311 * This function creates element name
312 * used in the form based on the text label
313 * or any other parameters
314 *
315 * Overriding things function to make sure
316 * an array is created instead of a single variable
317 */
318 function create_element_name() {
319
320 parent::create_element_name();
321
322 $this->_element_name .= "[]";
323 }
324
325 /**
326 * build the string for the confirmation page
327 *
328 * @return string
329 */
330 function get_value_text() {
331 $value = $this->get_value();
332 if (empty($value)) {
333 return '';
334 }
335
336 $data_flip = array_flip( $this->_data_list );
337 foreach( $value as $val ) {
338 $ret[] = $data_flip[$val];
339 }
340 return implode(", ", $ret);
341 }
342
343 /**
344 * This function builds and returns the
345 * form element object
346 *
347 * @return object
348 */
349 function get_element() {
350
351 $attributes = $this->_build_element_attributes();
352
353 $attributes[] = "multiple";
354
355 if ($this->_height) {
356 $attributes["size"] = 5;
357 }
358
359 $selected_values = $this->get_value();
360
361 $tag = new SELECTtag($attributes);
362
363 foreach ($this->_data_list as $name=>$value) {
364
365 $attributes = array("value"=>$value);
366 if (is_array($selected_values) && in_array($value, $selected_values)) {
367 $attributes[] = "selected";
368 }
369 if (isset($this->_disabled_items[$name])) {
370 $attributes[] = "disabled";
371 }
372
373 $tag->add(new OPTIONtag($attributes, $name));
374 }
375 return $tag;
376 }
377 }
378
379 /**
380 * This class builds a FEDataList that shows
381 * a select box for States of the U.S.A.
382 *
383 * @author Walter A. Boring IV
384 */
385 class FEUnitedStates extends FEListBox {
386
387 /**
388 * The states array
389 * @var array
390 */
391 var $_states = array("Select State" => "",
392 "Alabama" => "AL",
393 "Alaska" => "AK",
394 "Arizona" => "AZ",
395 "Arkansas" => "AR",
396 "California" => "CA",
397 "Colorado" => "CO",
398 "Connecticut" => "CT",
399 "Deleware" => "DE",
400 "District of Columbia" => "DC",
401 "Florida" => "FL",
402 "Georgia" => "GA",
403 "Hawaii" => "HI",
404 "Idaho" => "ID",
405 "Illinois" => "IL",
406 "Indiana" => "IN",
407 "Iowa" => "IA",
408 "Kansas" => "KS",
409 "Kentucky" => "KY",
410 "Louisiana" => "LA",
411 "Maine" => "ME",
412 "Maryland" => "MD",
413 "Massachusetts" => "MA",
414 "Michigan" => "MI",
415 "Minnesota" => "MN",
416 "Mississippi" => "MS",
417 "Missouri" => "MO",
418 "Montana" => "MT",
419 "Nebraska" => "NE",
420 "Nevada" => "NV",
421 "New Hampshire" => "NH",
422 "New Jersey" => "NJ",
423 "New Mexico" => "NM",
424 "New York" => "NY",
425 "North Carolina" => "NC",
426 "North Dakota" => "ND",
427 "Ohio" => "OH",
428 "Oklahoma" => "OK",
429 "Oregon" => "OR",
430 "Pennsylvania" => "PA",
431 "Rhode Island" => "RI",
432 "South Carolina" => "SC",
433 "South Dakota" => "SD",
434 "Tennessee" => "TN",
435 "Texas" => "TX",
436 "Utah" => "UT",
437 "Vermont" => "VT",
438 "Virginia" => "VA",
439 "Washington" => "WA",
440 "West Virginia" => "WV",
441 "Wisconsin" => "WI",
442 "Wyoming" => "WY",
443 "Puerto Rico" => "PR",
444 "Virgin Island" => "VI",
445 "Northern Mariana Islands" => "MP",
446 "Guam" => "GU",
447 "American Samoa" => "AS",
448 "Palau" => "PW",
449 );
450
451 /**
452 * The constructor
453 *
454 * @param string text label for the element
455 * @param boolean is this a required element?
456 * @param int element width in characters, pixels (px), percentage (%) or elements (em)
457 * @param int element height in px
458 * @param array data_list - list of data elements (name=>value)
459 */
460 function FEUnitedStates($label, $required = TRUE, $width = NULL, $height = NULL) {
461 $this->FEListBox($label, $required, $width, $height, $this->_states);
462 }
463 }
464
465
466 /**
467 * This builds a complex dual select box
468 * with buttons to move entries from one
469 * select box to another.
470 *
471 * From Actions To
472 * -------- Add >> --------
473 * |------| Add All |------|
474 * |------| |------|
475 * |------| |------|
476 * |------| << Remove |------|
477 * |------| Remove All |------|
478 * |------| |------|
479 * -------- --------
480 *
481 * @author Walter A. Boring IV
482 */
483 class FEComboListBox extends FEDataList {
484
485
486 /**
487 * Holds the list of available
488 * data elements for the 'to'
489 * box.
490 *
491 */
492 var $_data_list_to = array();
493
494 /**
495 * The from field's label
496 *
497 * @var string
498 */
499 var $_from_label = 'Available';
500
501 /**
502 * The to field's label
503 *
504 * @var string
505 */
506 var $_to_label = 'Selected';
507
508 /**
509 * This Form Element needs to
510 * propogate some js to the
511 * Form tag's onsubmit attribute
512 *
513 * @var string
514 */
515 var $_has_form_on_submit = true;
516
517
518 /**
519 * The constructor
520 *
521 * @param string text label for the element
522 * @param boolean is this a required element?
523 * @param int element width in characters, pixels (px), percentage (%) or elements (em)
524 * @param int element height in px
525 * @param array list of 'from' field data elements (name=>value)
526 * @param array list of 'to' field data elements (name=>value)
527 */
528 function FEComboListBox($label, $required = TRUE, $width="200px", $height="100px",
529 $from_data_list = array(), $to_data_list = array()) {
530 $this->set_to_list_data($to_data_list);
531 $this->FEDataList($label, $required, $width, $height, $from_data_list);
532 }
533
534 /**
535 * This function sets the array of data
536 * to be used in the data list
537 *
538 * @param array data_list - list of data elements (name=>value)
539 */
540 function set_to_list_data( $data_list = array() ) {
541 $this->_data_list_to = $data_list;
542 }
543
544 /**
545 * Use this method to set the label for the
546 * 'from' field
547 *
548 * @param string
549 * @return none
550 */
551 function set_from_label($label) {
552 $this->_from_label = $label;
553 }
554
555 /**
556 * Use this method to set the label for the
557 * 'to' field
558 *
559 * @param string
560 * @return none
561 */
562 function set_to_label($label) {
563 $this->_to_label = $label;
564 }
565
566
567 /**
568 * This function will set the
569 * initial value for the element
570 *
571 * @param value mixed
572 */
573 function set_value($value) {
574 //we need to see if we need to modify the
575 //the 2 data sets so the values of each are
576 //correctly displayed.
577
578 if (isset($_REQUEST[FORM_VISITED])) {
579 $all_list = array_merge( $this->_data_list, $this->_data_list_to);
580
581 if (isset($_REQUEST[$this->_element_name])) {
582 $diff = array_diff( $all_list, $_REQUEST[$this->_element_name]);
583 $this->set_list_data( $diff );
584 $to_list = array();
585
586 if (is_array($_REQUEST[$this->_element_name])) {
587 foreach( $_REQUEST[$this->_element_name] as $value ) {
588 $key = array_search($value, $all_list);
589 if ($key) {
590 $to_list[$key] = $value;
591 }
592 }
593 } else {
594 if (!empty($_REQUEST[$this->_element_name])) {
595 $to_list[array_search($_REQUEST[$this->_element_name], $all_list)] = $_REQUEST[$this->_element_name];
596 }
597 }
598 $this->set_to_list_data( $to_list );
599 } else {
600 //the assigned list is empty
601 $this->set_list_data( $all_list );
602 $this->set_to_list_data(array());
603 }
604 } else {
605 $this->_value = $value;
606 }
607 }
608
609
610
611 /**
612 * This function builds and returns the
613 * form element object
614 *
615 * @return object
616 */
617 function get_element() {
618 $table = html_table();
619 $table->add_row($this->_from_label, _HTML_SPACE,
620 $this->_to_label);
621
622 $from_select = form_select($this->_element_name.'_available',
623 $this->_data_list,'', TRUE);
624 $style = '';
625 if ($this->_height) {
626 $style .= "height: ".$this->_height.";";
627 }
628 if ($this->_width) {
629 $style .= "width: ".$this->_width.";";
630 }
631
632 //build the buttons
633 $button_style = 'width: 90px;';
634 $f_name = $this->_element_name."_move_around";
635 $add = form_button($this->_element_name.'_add', 'Add >>', array('style' => $button_style,
636 'onclick' => $f_name."('right',false);"));
637 $add_all = form_button($this->_element_name.'_add_all', 'Add All', array('style' => $button_style,
638 "onclick" => $f_name."('right', true);"));
639
640 $remove = form_button($this->_element_name.'_remove', '<< Remove', array('style' => $button_style,
641 'onclick' => $f_name."('left', false);"));
642 $remove_all = form_button($this->_element_name.'_remove_all', 'Remove All', array('style' => $button_style,
643 'onclick' => $f_name."('left', true);"));
644
645 $to_select = form_select($this->_element_name.'[]',
646 $this->_data_list_to, '', TRUE);
647
648 if (strlen($style) > 0) {
649 $from_select->set_style($style);
650 $to_select->set_style($style);
651 }
652
653 $table->add_row( $from_select,
654 new TDtag(array('align' => 'left'),
655 $add, html_br(), $add_all, html_br(2),
656 $remove, html_br(), $remove_all),
657 $to_select );
658
659 return $table;
660 }
661
662 /**
663 *
664 * This function is responsible for performing complete
665 * validation and setting the appropriate error message
666 * in case of a failed validation
667 *
668 * NOTE: This makes sure that the data submitted for both
669 * fields is in the original list.
670 *
671 * @param FormValidation object
672 */
673 function validate(&$_FormValidation) {
674 //need to make sure we only allow
675 //elements that the class has.
676 $combined = array_flip(array_merge( $this->_data_list, $this->_data_list_to));
677
678 if (isset($_REQUEST[$this->_element_name])) {
679 if (is_array($_REQUEST[$this->_element_name])) {
680 foreach( $_REQUEST[$this->_element_name] as $value ) {
681 if (!isset($combined[$value])) {
682 return FALSE;
683 }
684 }
685 return TRUE;
686 } else {
687 if (!isset($combined[$value])) {
688 return FALSE;
689 } else {
690 return TRUE;
691 }
692 }
693 } else {
694 //empty value
695 return TRUE;
696 }
697
698 return TRUE;
699 }
700
701 /**
702 * This provides a method
703 * for the FormContent
704 * to get access to the
705 * text associated with a
706 * field. This is only available
707 * on FormElements that have text
708 * associated with a field.
709 * It is used during Confirmation
710 *
711 * @param mixed - the value to look up
712 * @return string - the text associated
713 */
714 function get_value_text() {
715 return implode( ", ", array_keys( $this->get_value() ) );
716 }
717
718 /**
719 * This is a method for getting the JS needed
720 * for the form tag's onsubmit attribute.
721 *
722 * @return string
723 */
724 function form_tag_onsubmit() {
725 return $this->_element_name.'_check_submit();';
726 }
727
728
729 /**
730 * This method builds the Javascript needed for this
731 * element.
732 *
733 * @return string The javascript.
734 */
735 function javascript() {
736
737 $js = "function ".$this->_element_name."_move_around(direction, all) {
738 if (direction==\"right\") {
739 box1 = \"".$this->_element_name."_available\";
740 box2 = \"".$this->_element_name."[]\";
741 } else {
742 box1 = \"".$this->_element_name."[]\";
743 box2 = \"".$this->_element_name."_available\" + \"\";
744 }
745
746 for (var i=0;i<document.forms[0].elements[box1].length;i++) {
747 if (document.forms[0].elements[box1][i].selected || all) {
748 // add to the other list box
749 document.forms[0].elements[box2].options[document.forms[0].elements[box2].length] =
750 new Option(document.forms[0].elements[box1].options[i].text, document.forms[0].elements[box1][i].value);
751 // remove from the current listbox
752 document.forms[0].elements[box1][i] = null;
753 i--;
754 }
755 }
756 }";
757
758
759 $js .= "\nfunction ".$this->_element_name."_check_submit() {
760 // select all items in the added ip list
761 // in order to include in the post data
762 box = \"".$this->_element_name."[]\";
763 if (document.forms[0].elements[box]) {
764 for (var i=0;i<document.forms[0].elements[box].length;i++) {
765 document.forms[0].elements[box][i].selected = true;
766 }
767 }
768
769 // disable the buttons
770 //document.forms[0]._form_action1.disabled = true;
771 //if (document.forms[0]._form_action2)
772 // document.forms[0]._form_action2.disabled = true;
773 //document.forms[0].cancel.disabled = true;
774 return true;
775 }";
776
777 return trim($js);
778 }
779
780 /**
781 * This function will return the
782 * elements value
783 *
784 * @return mixed
785 */
786 function get_value() {
787 return $this->_data_list_to;
788 }
789
790
791 /**
792 * This method returns the hidden version of this
793 * element for a confirmation page.
794 *
795 * NOTE: This is called by the FormProcessor only.
796 * It shouldn't be called manually.
797 *
798 * @return INPUTtag of type hidden
799 */
800 function get_confirm_element() {
801 $name = $this->get_element_name();
802
803 $c = container();
804 if (is_array( $_REQUEST[$name]) ) {
805 foreach( $_REQUEST[$name] as $value ) {
806 $c->add(form_hidden( $name."[]", $value));
807 }
808 } else {
809 $c->add(form_hidden($name."[]", $_REQUEST[$name] ));
810 }
811
812 return $c;
813 }
814 }
815
816 /**
817 * This class builds a FEDataList that shows
818 * a select box for Months of the year
819 *
820 * @author Culley Harrelson <culley@fastmail.fm>
821 *
822 */
823
824 class FEMonths extends FEListBox {
825
826 /**
827 * The constructor
828 *
829 * @param string text label for the element
830 * @param boolean is this a required element?
831 * @param int element width in characters, pixels (px), percentage (%) or elements (em)
832 * @param int element height in px
833 * @param string format should be F m or M: Full month name, digit, abbreviated month name
834 */
835 function FEMonths($label, $required = TRUE, $width = NULL, $height = NULL, $format = 'F') {
836
837 // $format should be M m or F. Default to F if the user passes in garbage.
838 switch ($format) {
839 case 'M':
840 $format = '%b';
841 break;
842 case 'm':
843 $format = '%m';
844 break;
845 default:
846 $format = '%B';
847 break;
848 }
849
850 for ($i = 1; $i < 13; $i++) {
851 $months[$i] = strftime($format, strtotime("$i/12/2004"));
852 }
853
854 $this->FEListBox($label, $required, $width, $height, array_flip($months));
855
856 }
857 }
858
859 /**
860 * This class builds a FEDataList that shows
861 * a select box listing a range of years
862 *
863 * @author Culley Harrelson <culley@fastmail.fm>
864 *
865 */
866
867 class FEYears extends FEListBox {
868
869 /**
870 * The constructor
871 *
872 * @param string text label for the element
873 * @param boolean is this a required element?
874 * @param int element width in characters, pixels (px), percentage (%) or elements (em)
875 * @param int element height in px
876 * @param int min_year
877 * @param int max_year
878 */
879 function FEYears($label, $required = TRUE, $width = NULL, $height = NULL, $min_year = 2000, $max_year = 2010) {
880 // this will be cleaner in php5 with array_combine()
881 $list = range($min_year, $max_year);
882 foreach ($list as $year) {
883 $years[$year] = $year;
884 }
885 $this->FEListBox($label, $required, $width, $height, $years);
886 }
887 }
888
889 /**
890 * This class builds a FEDataList that shows
891 * a select box listing the days of the month
892 *
893 * @author Culley Harrelson <culley@fastmail.fm>
894 *
895 */
896
897 class FEDays extends FEListBox {
898
899 /**
900 * The constructor
901 *
902 * @param string text label for the element
903 * @param boolean is this a required element?
904 * @param int element width in characters, pixels (px), percentage (%) or elements (em)
905 * @param int element height in px
906 */
907
908 function FEDays($label, $required = TRUE, $width = NULL, $height = NULL) {
909
910 // this will be cleaner in php5 with array_combine()
911 $list = range(1, 31);
912 foreach ($list as $day) {
913 // pad the single digit days with zeros
914 $day = sprintf('%02d', $day);
915 $days[$day] = $day;
916 }
917 $this->FEListBox($label, $required, $width, $height, $days);
918
919 }
920 }
921
922 /**
923 * This class builds a FEDataList that shows a group of select boxes
924 * (FEYears, FEMonths, FEDays) representing a date.
925 *
926 * @author Culley Harrelson <culley@fastmail.fm>
927 * @author Suren Markosian <suren@emicron.net>
928 *
929 */
930
931 class FEDate extends FEBoxElement {
932
933 /**
934 * The earliest year shown in the year list.
935 * @var int
936 */
937 var $_min_year;
938
939 /**
940 * The latest year shown in the year list.
941 * @var string
942 */
943 var $_max_year;
944
945 /**
946 * The order in which to show the elements. This variable must be 3
947 * characters long and contain only one m only one d and only one y.
948 *
949 * @var string
950 */
951 var $_format = 'mdy';
952
953 /**
954 * A printf style format string used to add punctuation to the confirmation
955 * display. Defaults to space separated. The placeholders are filled
956 * according to the order set in $_format
957 *
958 * @var string
959 */
960 var $_text_format = '%s %s %s';
961
962 /**
963 * The year form element
964 *
965 * @var FEYears
966 */
967 var $_year;
968
969 /**
970 * The month form element
971 *
972 * @var FEMonths
973 */
974 var $_month;
975
976 /**
977 * The day form element
978 *
979 * @var FEDays
980 */
981 var $_day;
982
983 /**
984 * The constructor
985 *
986 * @param string text label for the element
987 * @param boolean is this a required element?
988 * @param int element width in characters, pixels (px), percentage (%) or elements (em)
989 * @param int element height in px
990 * @param string date format string. M m F Y y d D are valid. 3 characters max.
991 * @param int min value for year drop down list
992 * @param int max value for year drop down list
993 *
994 */
995
996 function FEDate($label, $required = TRUE, $width = NULL, $height = NULL, $format = 'mdy', $min_year = 2000, $max_year = 2010) {
997
998 $this->_set_format($format);
999 $this->_min_year = $min_year;
1000 $this->_max_year = $max_year;
1001
1002 $this->_year = new FEYears($this->_element_name . '_years', $required, null, null, $min_year, $max_year);
1003 $this->_month = new FEMonths($this->_element_name . '_months', $required, null, null, preg_replace('/[dy]/i', '', $this->_format));
1004 $this->_day = new FEDays($this->_element_name . '_days', $required, null, null);
1005 $this->FEBoxElement($label, $required, $width, $height);
1006 }
1007
1008 /**
1009 * This function builds and returns the
1010 * form element object
1011 *
1012 * @return object
1013 */
1014 function get_element() {
1015
1016 $container = new Container();
1017
1018 // add the elements in the order specified.
1019 $chars = preg_split('//', $this->_format, -1, PREG_SPLIT_NO_EMPTY);
1020 foreach ($chars as $char) {
1021 switch ($char) {
1022 case 'y':
1023 $container->add($this->_year->get_element());
1024 break;
1025 case 'm':
1026 case 'F':
1027 $container->add($this->_month->get_element());
1028 break;
1029 case 'd':
1030 $container->add($this->_day->get_element());
1031 break;
1032 }
1033 }
1034
1035 return $container;
1036 }
1037
1038 /**
1039 * This function will return the elements value as an array or month, day
1040 * and year
1041 *
1042 * @return array
1043 */
1044 function get_value() {
1045
1046 return $this->_value;
1047
1048 }
1049
1050 /**
1051 * This function sets the default values for the date element The
1052 * parameter should be a string representation of the date in ISO 8601
1053 * format.
1054 *
1055 * @param string
1056 */
1057 function set_value($value) {
1058
1059 $date_parts = explode('-', $value);
1060 $this->_year->set_value($date_parts[0]);
1061 $this->_month->set_value($date_parts[1]);
1062 $this->_day->set_value($date_parts[2]);
1063
1064 }
1065
1066 /**
1067 * This returns a formatted string used for the confirmation display (and
1068 * possibly elsewhere)
1069 *
1070 * @return string
1071 */
1072 function get_value_text() {
1073
1074 // loop through the characters in $_format to properly set the placeholders
1075 // determined in $_text_format
1076 $chars = preg_split('//', $this->_format, -1, PREG_SPLIT_NO_EMPTY);
1077 $i = 1;
1078 foreach ($chars as $char) {
1079
1080 switch ($char) {
1081 case 'y':
1082 $value = $this->_year->get_value_text();
1083 break;
1084 case 'm':
1085 case 'F':
1086 $value = $this->_month->get_value_text();
1087 break;
1088 case 'd':
1089 $value = $this->_day->get_value_text();
1090 break;
1091 }
1092
1093 switch ($i) {
1094 case 1:
1095 $one = $value;
1096 break;
1097 case 2:
1098 $two = $value;
1099 break;
1100 case 3:
1101 $three = $value;
1102 break;
1103 }
1104
1105 $i++;
1106 }
1107
1108 return sprintf($this->_text_format, $one, $two, $three);
1109 }
1110
1111 /**
1112 *
1113 * This function is responsible for performing complete
1114 * validation and setting the appropriate error message
1115 * in case of a failed validation
1116 *
1117 * @param FormValidation object
1118 */
1119 function validate(&$_FormValidation) {
1120 $value = $this->get_value();
1121
1122 // we make sure that the date is valid
1123 if (!checkdate($value["month"], $value["day"], $value["year"])) {
1124 $this->set_error_message("Invalid date");
1125 return FALSE;
1126 }
1127 return TRUE;
1128 }
1129
1130 /**
1131 * this method sets the display order for the elements in the widget
1132 *
1133 * @param string
1134 * @return bool success or failure
1135 */
1136 function _set_format($format) {
1137
1138 // must be 3 characters
1139 if (strlen($format) != 3) {
1140 return FALSE;
1141 }
1142
1143 // month can be represented by F m or M
1144 if (strstr($format, 'F')) {
1145 $month = 'f';
1146 } else {
1147 $month = 'm';
1148 }
1149
1150 // compare the characters sent with the characters needed. only set
1151 // the property if one of each is present
1152 $search_for = array ('y', $month, 'd');
1153 $chars = preg_split('//', strtolower($format), -1, PREG_SPLIT_NO_EMPTY);
1154
1155 if (count(array_diff($search_for, $chars)) > 0) {
1156 return FALSE;
1157 }
1158
1159 $this->_format = $format;
1160 return TRUE;
1161
1162 }
1163
1164 /**
1165 * this method sets the format string used in get_value_text(). Use this
1166 * method to set special punctuation for the confirmation display.
1167 *
1168 * @param string
1169 *
1170 */
1171 function set_text_format($format) {
1172
1173 $this->_text_format = $format;
1174 }
1175
1176 /**
1177 * This method returns the hidden version of this
1178 * element for a confirmation page.
1179 *
1180 * NOTE: This is called by the FormProcessor only.
1181 * It shouldn't be called manually.
1182 *
1183 * @return container
1184 */
1185 function get_confirm_element() {
1186 $element_name = $this->get_element_name();
1187
1188 $c = container();
1189 $c->add(form_hidden("{$element_name}[year]", $this->_year->get_value()));
1190 $c->add(form_hidden("{$element_name}[month]", $this->_month->get_value()));
1191 $c->add(form_hidden("{$element_name}[day]", $this->_day->get_value()));
1192 return $c;
1193 }
1194
1195 }
1196
1197 ?>

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