1 |
<?php |
2 |
/** |
3 |
* This file contains the base FormElement class. |
4 |
* |
5 |
* $Id: FormElement.inc,v 1.39 2004/03/25 21:02:57 hemna 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 base FormElement object. It can be |
18 |
* single form field such as a text input field, |
19 |
* or a complex object. |
20 |
* |
21 |
* Usefull Functions |
22 |
* |
23 |
* get_value() - This gets the current |
24 |
* 'value' of the FormElement. |
25 |
* NOTE: This can be a 'complex' FormElement |
26 |
* in which it may return an array of |
27 |
* values. |
28 |
* |
29 |
* set_value() - Set the current value |
30 |
* for this FormElement. |
31 |
* |
32 |
* get_label() - returns the label for this |
33 |
* FormElement. |
34 |
* |
35 |
* |
36 |
* @package phpHtmlLib |
37 |
* @subpackage FormProcessing |
38 |
*/ |
39 |
class FormElement { |
40 |
|
41 |
/** |
42 |
* Holds the elements label text |
43 |
* |
44 |
*/ |
45 |
var $_label_text = NULL; |
46 |
|
47 |
/** |
48 |
* Holds the elements initial value |
49 |
* |
50 |
*/ |
51 |
var $_value = NULL; |
52 |
|
53 |
/** |
54 |
* Indicates whether this elements |
55 |
* final value is required and cannot |
56 |
* be empty |
57 |
*/ |
58 |
var $_is_required = TRUE; |
59 |
|
60 |
/** |
61 |
* holds the array of errors |
62 |
* for this element. |
63 |
*/ |
64 |
var $_errors = array(); |
65 |
|
66 |
/** |
67 |
* Holds the error message text |
68 |
* for validation errors, if any |
69 |
* |
70 |
*/ |
71 |
var $_error_message = NULL; |
72 |
|
73 |
/** |
74 |
* Holds the state of the last validation |
75 |
* Sets to true in case of a validation error |
76 |
* |
77 |
*/ |
78 |
var $_has_error = FALSE; |
79 |
|
80 |
/** |
81 |
* Holds the name of the element |
82 |
* as it appears in the form html tag |
83 |
* |
84 |
*/ |
85 |
var $_element_name = NULL; |
86 |
|
87 |
/** |
88 |
* Holds additional attributes for |
89 |
* the elements html tag |
90 |
* |
91 |
*/ |
92 |
var $_attributes; |
93 |
|
94 |
/** |
95 |
* Holds additional style attributes for |
96 |
* the elements html tag |
97 |
* |
98 |
*/ |
99 |
var $_style_attributes; |
100 |
|
101 |
/** |
102 |
* Indicates a disabled element |
103 |
* |
104 |
*/ |
105 |
var $_is_disabled = FALSE; |
106 |
|
107 |
/** |
108 |
* automatically strip slashes from |
109 |
* form values? |
110 |
*/ |
111 |
var $_stripslashes = FALSE; |
112 |
|
113 |
|
114 |
/** |
115 |
* This holds the name of the form |
116 |
* for js that needs it |
117 |
*/ |
118 |
var $_form_name; |
119 |
|
120 |
/** |
121 |
* This Form Element needs to |
122 |
* propogate some js to the |
123 |
* Form tag's onsubmit attribute |
124 |
* |
125 |
* @var string |
126 |
*/ |
127 |
var $_has_form_on_submit = false; |
128 |
|
129 |
/** |
130 |
* This holds an array of FormElement objects |
131 |
* that are slaves of this element |
132 |
*/ |
133 |
var $_slave_elements; |
134 |
|
135 |
/** |
136 |
* The required field string |
137 |
* |
138 |
*/ |
139 |
var $_required_field_marker = "*"; |
140 |
|
141 |
|
142 |
/** |
143 |
* The constructor |
144 |
* |
145 |
* @param label string - text label for the element |
146 |
* @param bool required - is this a required element |
147 |
*/ |
148 |
function FormElement($label, $required = TRUE) { |
149 |
|
150 |
$this->set_label_text($label); |
151 |
$this->set_required($required); |
152 |
|
153 |
// create a unique name to be used in the html form |
154 |
$this->create_element_name(); |
155 |
|
156 |
//This sets the initial value of the element |
157 |
$this->set_value( $this->get_init_value() ); |
158 |
} |
159 |
|
160 |
/** |
161 |
* This function will return the |
162 |
* slaves of this element |
163 |
* |
164 |
* @return array of slave elements |
165 |
*/ |
166 |
function &get_slave_elements() { |
167 |
return $this->_slave_elements; |
168 |
} |
169 |
|
170 |
/** |
171 |
* This function will set the |
172 |
* slaves of this element |
173 |
* |
174 |
* @param array of slave elements (element references &) |
175 |
*/ |
176 |
function set_slave_elements($slaveElements) { |
177 |
$this->_slave_elements = $slaveElements; |
178 |
$this->set_data_all_slaves( $this->get_value() ); |
179 |
} |
180 |
|
181 |
|
182 |
/** |
183 |
* This function will set the |
184 |
* data the parent wants to set for the slave |
185 |
* |
186 |
* @param the slave |
187 |
* @param the value being set for the parent |
188 |
*/ |
189 |
function set_slave_data(&$slave, $parentValue) { |
190 |
; //override to set the data for the slave |
191 |
} |
192 |
|
193 |
/** |
194 |
* This function will return the |
195 |
* elements label text |
196 |
* |
197 |
* @return string |
198 |
*/ |
199 |
function get_label_text() { |
200 |
return $this->_label_text; |
201 |
} |
202 |
|
203 |
/** |
204 |
* This function will set the |
205 |
* label for the element |
206 |
* |
207 |
* @param label string |
208 |
*/ |
209 |
function set_label_text($label) { |
210 |
$this->_label_text = $label; |
211 |
} |
212 |
|
213 |
/** |
214 |
* This function will return the |
215 |
* elements value |
216 |
* |
217 |
* @return mixed |
218 |
*/ |
219 |
function get_value() { |
220 |
if ($this->_stripslashes) { |
221 |
return stripslashes($this->_value); |
222 |
} else { |
223 |
return $this->_value; |
224 |
} |
225 |
} |
226 |
|
227 |
|
228 |
/** |
229 |
* This function will set the |
230 |
* initial value for the element |
231 |
* |
232 |
* @param value mixed |
233 |
*/ |
234 |
function set_value($value) { |
235 |
$this->_value = $value; |
236 |
$this->set_data_all_slaves($value); |
237 |
} |
238 |
|
239 |
/** |
240 |
* This function will call set_slave_data for all slaves |
241 |
* should be called when value of parent changes |
242 |
* |
243 |
* @param string - the current value of parent |
244 |
*/ |
245 |
function set_data_all_slaves($parentValue){ |
246 |
$slaves = &$this->get_slave_elements(); |
247 |
$num_slaves = count($slaves); |
248 |
if ($num_slaves) { |
249 |
for($i=0; $i < $num_slaves; $i++){ |
250 |
$this->set_slave_data($slaves[$i], $parentValue); |
251 |
} |
252 |
|
253 |
} |
254 |
} |
255 |
|
256 |
/** |
257 |
* in case anyone in JS land |
258 |
* needs the name of the form that |
259 |
* this element lives in |
260 |
* |
261 |
* @param string - the form name |
262 |
*/ |
263 |
function set_form_name($name) { |
264 |
$this->_form_name = $name; |
265 |
} |
266 |
|
267 |
/** |
268 |
* This provides a method |
269 |
* for the FormContent |
270 |
* to get access to the |
271 |
* text associated with a |
272 |
* field. This is only available |
273 |
* on FormElements that have text |
274 |
* associated with a field. |
275 |
* It is used during Confirmation |
276 |
* |
277 |
* @param mixed the value to look up |
278 |
* @return string - the text associated |
279 |
*/ |
280 |
function get_value_text() { |
281 |
return $this->get_value(); |
282 |
} |
283 |
|
284 |
|
285 |
/** |
286 |
* This function set the elements |
287 |
* required state |
288 |
* |
289 |
* @param bool required |
290 |
*/ |
291 |
function set_required($required) { |
292 |
$this->_is_required = $required; |
293 |
} |
294 |
|
295 |
/** |
296 |
* Returns whether this elements |
297 |
* final value cannot be empty |
298 |
* |
299 |
* @return bool requried |
300 |
*/ |
301 |
function is_required() { |
302 |
return $this->_is_required; |
303 |
} |
304 |
|
305 |
|
306 |
/** |
307 |
* This returns the initial value of |
308 |
* the element |
309 |
* |
310 |
* @return mixed |
311 |
*/ |
312 |
function get_init_value() { |
313 |
return @$_REQUEST[str_replace("[]","",$this->get_element_name())]; |
314 |
} |
315 |
|
316 |
|
317 |
/** |
318 |
* Sets the disabled element flag |
319 |
* |
320 |
* @param bool disabled |
321 |
*/ |
322 |
function set_disabled($flag) { |
323 |
$this->_is_disabled = $flag; |
324 |
} |
325 |
|
326 |
/** |
327 |
* Returns the elements disabled |
328 |
* state |
329 |
* |
330 |
* @return bool disabled |
331 |
*/ |
332 |
function is_disabled() { |
333 |
return $this->_is_disabled; |
334 |
} |
335 |
|
336 |
/** |
337 |
* This sets the stripslashes flag for |
338 |
* this object. |
339 |
* |
340 |
* @param boolean |
341 |
*/ |
342 |
function set_stripslashes( $flag = TRUE ) { |
343 |
$this->_stripslashes = $flag; |
344 |
} |
345 |
|
346 |
|
347 |
/********* Tag attributes methods *********/ |
348 |
|
349 |
|
350 |
/** |
351 |
* add a single attribute (name="value") |
352 |
* |
353 |
* @param string $name attribute name |
354 |
* @param mixed $value the value |
355 |
* |
356 |
*/ |
357 |
function set_attribute($name, $value = NULL) { |
358 |
if ($value == NULL) { |
359 |
$this->_attributes[] = $name; |
360 |
} |
361 |
else { |
362 |
$this->_attributes[$name] = $value; |
363 |
} |
364 |
} |
365 |
|
366 |
/** |
367 |
* return a single attribute |
368 |
* |
369 |
* @param string $name attribute name |
370 |
* @return mixed $value the value |
371 |
* |
372 |
*/ |
373 |
function get_attribute($name) { |
374 |
return $this->_attributes[$name]; |
375 |
} |
376 |
|
377 |
/** |
378 |
* Sets elements title text |
379 |
* |
380 |
* @param string title |
381 |
*/ |
382 |
function set_title($title) { |
383 |
$this->set_attribute("title", $title); |
384 |
} |
385 |
|
386 |
/** |
387 |
* Sets elements css attribute |
388 |
* |
389 |
* @param string $name attribute name |
390 |
* @param mixed $value the value |
391 |
*/ |
392 |
function set_style_attribute($name, $value) { |
393 |
$this->_style_attributes[$name] = $value; |
394 |
} |
395 |
|
396 |
/** |
397 |
* Sets the element's tab index |
398 |
* |
399 |
* @param int the # for the tabindex |
400 |
*/ |
401 |
function set_tabindex($index) { |
402 |
$this->set_attribute('tabindex', $index); |
403 |
} |
404 |
|
405 |
/** |
406 |
* Gets the current tab index if any |
407 |
* |
408 |
* @return int returns 0 if there is none set. |
409 |
*/ |
410 |
function get_tabindex() { |
411 |
if (isset($this->_attributes['tabindex'])) { |
412 |
return $this->_attributes['tabindex']; |
413 |
} else { |
414 |
return 0; |
415 |
} |
416 |
} |
417 |
|
418 |
|
419 |
/********* Error handling methods *********/ |
420 |
|
421 |
/** |
422 |
* Defines error message text and |
423 |
* sets the error flag to true |
424 |
* |
425 |
* @param mesage text - error message |
426 |
* @param label text - a label to provide |
427 |
* for the error. This is |
428 |
* only needed for a complex |
429 |
* element that has multiple |
430 |
* 'hidden/magic' fields. |
431 |
*/ |
432 |
function set_error_message($message, $label=NULL) { |
433 |
if ($label == NULL) { |
434 |
$label = $this->get_label_text(); |
435 |
} |
436 |
$this->_errors[] = array("label" => $label, |
437 |
"message" => $message); |
438 |
$this->_has_error = TRUE; |
439 |
} |
440 |
|
441 |
/** |
442 |
* This returns the array of errors |
443 |
* for this element |
444 |
* |
445 |
* @param array of errors |
446 |
*/ |
447 |
function get_errors() { |
448 |
return $this->_errors; |
449 |
} |
450 |
|
451 |
|
452 |
/** |
453 |
* Returns the current error message |
454 |
* if any |
455 |
* |
456 |
* @return mesage text - error message |
457 |
*/ |
458 |
function get_error_message() { |
459 |
return $this->_error_message; |
460 |
} |
461 |
|
462 |
/** |
463 |
* Returns the current error state |
464 |
* |
465 |
* |
466 |
* @return bool error state |
467 |
*/ |
468 |
function has_error($label=NULL) { |
469 |
if ($label==NULL) { |
470 |
return $this->_has_error; |
471 |
} else { |
472 |
//they are looking for a specific error |
473 |
foreach( $this->_errors as $error) { |
474 |
|
475 |
if ($error['label'] == $label) { |
476 |
return TRUE; |
477 |
} |
478 |
} |
479 |
return FALSE; |
480 |
} |
481 |
} |
482 |
|
483 |
|
484 |
/********* Element name handling methods *********/ |
485 |
|
486 |
/** |
487 |
* This function creates element name |
488 |
* used in the form based on the text label |
489 |
* or any other parameters |
490 |
* |
491 |
*/ |
492 |
function create_element_name() { |
493 |
|
494 |
$name = strtolower($this->get_label_text()); |
495 |
$len = strlen($name); |
496 |
|
497 |
for ($i=0; $i<$len;$i++) { |
498 |
if ((ord($name[$i])<97 || ord($name[$i])>122) && (ord($name[$i])<48 || ord($name[$i])>57)) |
499 |
$name[$i] = "_"; |
500 |
} |
501 |
|
502 |
$this->set_element_name($name); |
503 |
} |
504 |
|
505 |
/** |
506 |
* This allows you to force the element |
507 |
* name to whatever you like, so you |
508 |
* don't have to use the default name, which is |
509 |
* generated based on the label. |
510 |
* |
511 |
* @param string the form element's name |
512 |
* @return none |
513 |
*/ |
514 |
function set_element_name($name) { |
515 |
$this->_element_name = $name; |
516 |
} |
517 |
|
518 |
/** |
519 |
* Returns the element name |
520 |
* to be used in the form |
521 |
* |
522 |
* @return string form element name |
523 |
*/ |
524 |
function get_element_name() { |
525 |
return $this->_element_name; |
526 |
} |
527 |
|
528 |
|
529 |
/********* Validation methods *********/ |
530 |
|
531 |
|
532 |
/** |
533 |
* This function performs the actual validation |
534 |
* It is called only if the validation is required by |
535 |
* this element |
536 |
* |
537 |
* This function is responsible for performing complete |
538 |
* validation and setting the appropriate error message |
539 |
* in case of a failed validation |
540 |
* |
541 |
* @param FormValidation object. |
542 |
*/ |
543 |
function validate(&$_FormValidation) { |
544 |
return TRUE; |
545 |
} |
546 |
|
547 |
|
548 |
/** |
549 |
* This function checks if the validation |
550 |
* is nesseccary and calls the validate method |
551 |
* |
552 |
* @param FormValidation object. |
553 |
*/ |
554 |
function _do_validation(&$_FormValidation) { |
555 |
$value = $this->get_value(); |
556 |
$has_value = FALSE; |
557 |
if (is_array($value)) { |
558 |
foreach($value as $entry) { |
559 |
if ($entry != NULL) { |
560 |
$has_value = TRUE; |
561 |
} |
562 |
} |
563 |
} else { |
564 |
if ($value != NULL) { |
565 |
$has_value = TRUE; |
566 |
} |
567 |
} |
568 |
|
569 |
if ($has_value && !$this->is_disabled()) { |
570 |
return $this->validate($_FormValidation); |
571 |
} else if ($this->is_required()) { |
572 |
$this->set_error_message("This field cannot be empty"); |
573 |
return FALSE; |
574 |
} else { |
575 |
return TRUE; |
576 |
} |
577 |
} |
578 |
|
579 |
|
580 |
/********* JavaScript event methods *********/ |
581 |
|
582 |
|
583 |
/** |
584 |
* This method is used for adding any javascript |
585 |
* that is used by this element. This will automatically |
586 |
* get called and added to the page by the FormProcessor |
587 |
* |
588 |
* @return string - raw js |
589 |
*/ |
590 |
function javascript() { |
591 |
return NULL; |
592 |
} |
593 |
|
594 |
/** |
595 |
* This function return the javaScript code for |
596 |
* an onClick event |
597 |
* |
598 |
* @return string - javascript code |
599 |
*/ |
600 |
function onClick() { |
601 |
return NULL; |
602 |
} |
603 |
|
604 |
/** |
605 |
* This function return the javaScript code for |
606 |
* an onFocus event |
607 |
* |
608 |
* @return string - javascript code |
609 |
*/ |
610 |
function onFocus() { |
611 |
return NULL; |
612 |
} |
613 |
|
614 |
/** |
615 |
* This function return the javaScript code for |
616 |
* an onSubmit event |
617 |
* |
618 |
* @return string - javascript code |
619 |
*/ |
620 |
function onSubmit() { |
621 |
return NULL; |
622 |
} |
623 |
|
624 |
/** |
625 |
* This function return the javaScript code for |
626 |
* an onBlur event |
627 |
* |
628 |
* @return string - javascript code |
629 |
*/ |
630 |
function onBlur() { |
631 |
return NULL; |
632 |
} |
633 |
|
634 |
|
635 |
/** |
636 |
* this function retuns the javaScript code for |
637 |
* an onChange event |
638 |
* |
639 |
* @return string - javascript code |
640 |
*/ |
641 |
function onChange() { |
642 |
return NULL; |
643 |
} |
644 |
|
645 |
/** |
646 |
* This function builds the complete javaScript events code |
647 |
* for the element |
648 |
* |
649 |
* @return array - attributes |
650 |
*/ |
651 |
function _build_javascript() { |
652 |
|
653 |
$javascript = array(); |
654 |
|
655 |
if (($js=$this->onClick()) != NULL) { |
656 |
$javascript[] = "onclick=\"" . $js . "\""; |
657 |
} |
658 |
|
659 |
if (($js=$this->onFocus()) != NULL) { |
660 |
$javascript[] = "onfocus=\"" . $js . "\""; |
661 |
} |
662 |
|
663 |
if (($js=$this->onSubmit()) != NULL) { |
664 |
$javascript[] = "onsubmit=\"" . $js . "\""; |
665 |
} |
666 |
|
667 |
if (($js=$this->onBlur()) != NULL) { |
668 |
$javascript[] = "onblur=\"" . $js . "\""; |
669 |
} |
670 |
|
671 |
if (($js=$this->onChange()) != NULL) { |
672 |
$javascript[] = "onchange=\"" . $js . "\""; |
673 |
} |
674 |
|
675 |
if (count($javascript)>0) { |
676 |
return $javascript; |
677 |
} |
678 |
else { |
679 |
return NULL; |
680 |
} |
681 |
} |
682 |
|
683 |
|
684 |
/** |
685 |
* This is a method for getting the JS needed |
686 |
* for the form tag's onsubmit attribute. |
687 |
* |
688 |
* @return string |
689 |
*/ |
690 |
function form_tag_onsubmit() { |
691 |
return ''; |
692 |
} |
693 |
|
694 |
|
695 |
/********* rendering methods *********/ |
696 |
|
697 |
|
698 |
/** |
699 |
* This function return the symbol used to |
700 |
* denote a required field |
701 |
* |
702 |
* @return string - required symbol |
703 |
*/ |
704 |
function get_required_symbol() { |
705 |
return $this->_required_field_marker; |
706 |
} |
707 |
|
708 |
/** |
709 |
* This allows you to customize the |
710 |
* require string marker |
711 |
* |
712 |
* @param string |
713 |
*/ |
714 |
function set_required_symbol($symbol) { |
715 |
$this->_required_field_marker; |
716 |
} |
717 |
|
718 |
/** |
719 |
* This function builds the element form attributes |
720 |
* |
721 |
* @return array attributes |
722 |
*/ |
723 |
function _build_element_attributes() { |
724 |
|
725 |
$attributes = $this->_attributes; |
726 |
|
727 |
if ($this->_style_attributes) { |
728 |
// build the css styles |
729 |
foreach ($this->_style_attributes as $name=>$value) { |
730 |
$style[] = $name . ":" . $value . ";"; |
731 |
} |
732 |
$attributes["style"] = implode("", $style); |
733 |
} |
734 |
|
735 |
// set the element tag name |
736 |
$attributes["name"] = $this->get_element_name(); |
737 |
|
738 |
if (($js = $this->_build_javascript()) != NULL) { |
739 |
// build javaScript attributes |
740 |
$attributes = array_merge($attributes, $js); |
741 |
} |
742 |
|
743 |
//see if its disabled |
744 |
if ($this->is_disabled()) { |
745 |
$attributes[] = "disabled"; |
746 |
} |
747 |
|
748 |
return $attributes; |
749 |
} |
750 |
|
751 |
|
752 |
/** |
753 |
* This function builds and returns a |
754 |
* label object based on the label text |
755 |
* and error conditions |
756 |
* |
757 |
* @param FormContent object that holds the |
758 |
* required field marker |
759 |
* @return object SPANtag |
760 |
*/ |
761 |
function get_label($form_content=NULL) { |
762 |
|
763 |
$label = $this->get_label_text(); |
764 |
$text = $label; |
765 |
|
766 |
if ($this->is_required()) { |
767 |
$text .= ' ' . ($form_content ? $form_content->_required_field_marker : |
768 |
$this->get_required_symbol()); |
769 |
} |
770 |
|
771 |
$span = html_span("formlabel", $text); |
772 |
|
773 |
if ($this->has_error()) { |
774 |
$span->set_tag_attribute("style","color:red;"); |
775 |
} |
776 |
|
777 |
return $span; |
778 |
} |
779 |
|
780 |
|
781 |
/** |
782 |
* This function builds and returns the |
783 |
* form element object |
784 |
* |
785 |
* @return object |
786 |
*/ |
787 |
function get_element() { |
788 |
|
789 |
$span = new SPANtag($this->_build_element_attributes(), $this->get_value()); |
790 |
|
791 |
return $span; |
792 |
} |
793 |
|
794 |
|
795 |
/** |
796 |
* This method returns the hidden version of this |
797 |
* element for a confirmation page. |
798 |
* |
799 |
* NOTE: This is called by the FormProcessor only. |
800 |
* It shouldn't be called manually. |
801 |
* |
802 |
* @return INPUTtag of type hidden |
803 |
*/ |
804 |
function get_confirm_element() { |
805 |
$values = $this->get_value(); |
806 |
$name = $this->get_element_name(); |
807 |
|
808 |
if (is_array($values)) { |
809 |
$c = container(); |
810 |
foreach ( $values as $value ) { |
811 |
$c->add( form_hidden($name, $value ) ); |
812 |
} |
813 |
return $c; |
814 |
} else { |
815 |
return form_hidden($name, $this->get_value() ); |
816 |
} |
817 |
} |
818 |
|
819 |
} // FormElement |
820 |
?> |