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 Markosyan <suren@bcsweb.com> |
* @author Suren Markosyan <suren@bcsweb.com> |
9 |
* @package phpHtmlLib |
* @package phpHtmlLib |
10 |
* @subpackage FormProcessing |
* @subpackage FormProcessing |
104 |
*/ |
*/ |
105 |
var $_is_disabled = FALSE; |
var $_is_disabled = FALSE; |
106 |
|
|
107 |
|
/** |
108 |
|
* Indicates this element is |
109 |
|
* read only. The get_element() |
110 |
|
* will return the get_value_text() |
111 |
|
* instead of the form. |
112 |
|
*/ |
113 |
|
var $_is_readonly = FALSE; |
114 |
|
|
115 |
|
/** |
116 |
|
* Flag to tell us to enable |
117 |
|
* validation or not |
118 |
|
*/ |
119 |
|
var $_validation_enabled = TRUE; |
120 |
|
|
121 |
/** |
/** |
122 |
* automatically strip slashes from |
* automatically strip slashes from |
123 |
* form values? |
* form values? |
141 |
var $_has_form_on_submit = false; |
var $_has_form_on_submit = false; |
142 |
|
|
143 |
/** |
/** |
144 |
* This holds an array of FormElement objects |
* This holds an array of FormElement objects |
145 |
* that are slaves of this element |
* that are slaves of this element |
146 |
*/ |
*/ |
147 |
var $_slave_elements; |
var $_slave_elements; |
148 |
|
|
149 |
/** |
/** |
150 |
* The required field string |
* The required field string |
151 |
* |
* |
152 |
*/ |
*/ |
153 |
var $_required_field_marker = "*"; |
var $_required_field_marker = "*"; |
154 |
|
|
155 |
|
/** |
156 |
|
* The form Element's flag to add the : |
157 |
|
* character at the end of the label. |
158 |
|
*/ |
159 |
|
var $_label_colon_flag = FALSE; |
160 |
|
|
161 |
|
/** |
162 |
|
* variable to store JS onclick |
163 |
|
*/ |
164 |
|
var $onClickJS = NULL; |
165 |
|
|
166 |
|
/** |
167 |
|
* variable to store JS onclick |
168 |
|
*/ |
169 |
|
var $onFocusJS = NULL; |
170 |
|
|
171 |
|
/** |
172 |
|
* variable to store JS onclick |
173 |
|
*/ |
174 |
|
var $onChangeJS = NULL; |
175 |
|
|
176 |
|
/** |
177 |
|
* variable to store JS onclick |
178 |
|
*/ |
179 |
|
var $onSubmitJS = NULL; |
180 |
|
|
181 |
|
/** |
182 |
|
* variable to store JS onclick |
183 |
|
*/ |
184 |
|
var $onBlurJS = NULL; |
185 |
|
|
186 |
/** |
/** |
187 |
* The constructor |
* The constructor |
210 |
function &get_slave_elements() { |
function &get_slave_elements() { |
211 |
return $this->_slave_elements; |
return $this->_slave_elements; |
212 |
} |
} |
213 |
|
|
214 |
/** |
/** |
215 |
* This function will set the |
* This function will set the |
216 |
* slaves of this element |
* slaves of this element |
222 |
$this->set_data_all_slaves( $this->get_value() ); |
$this->set_data_all_slaves( $this->get_value() ); |
223 |
} |
} |
224 |
|
|
225 |
|
|
226 |
/** |
/** |
227 |
* This function will set the |
* This function will set the |
228 |
* data the parent wants to set for the slave |
* data the parent wants to set for the slave |
255 |
} |
} |
256 |
|
|
257 |
/** |
/** |
258 |
|
* This method sets the flag to have ALL of the |
259 |
|
* FormElement's labels be appended with a : |
260 |
|
* character. Some folks like this for some |
261 |
|
* completely unexplained reason. |
262 |
|
*/ |
263 |
|
function set_colon_flag($flag=TRUE) { |
264 |
|
$this->_label_colon_flag = $flag; |
265 |
|
} |
266 |
|
|
267 |
|
/** |
268 |
* This function will return the |
* This function will return the |
269 |
* elements value |
* elements value |
270 |
* |
* |
271 |
* @return mixed |
* @return mixed |
272 |
*/ |
*/ |
273 |
function get_value() { |
function get_value() { |
274 |
|
|
275 |
if ($this->_stripslashes) { |
if ($this->_stripslashes) { |
276 |
return stripslashes($this->_value); |
if (is_array($this->_value)) { |
277 |
|
return array_map('stripslashes', array_map('trim', $this->_value)); |
278 |
|
} else { |
279 |
|
return stripslashes(trim($this->_value)); |
280 |
|
} |
281 |
} else { |
} else { |
282 |
return $this->_value; |
if (is_array($this->_value)) { |
283 |
|
if (is_string($this->_value)) { |
284 |
|
return array_map('trim', $this->_value); |
285 |
|
} else { |
286 |
|
return $this->_value; |
287 |
|
} |
288 |
|
} else { |
289 |
|
if (is_string($this->_value)) { |
290 |
|
return trim($this->_value); |
291 |
|
} else { |
292 |
|
return $this->_value; |
293 |
|
} |
294 |
|
} |
295 |
} |
} |
296 |
} |
} |
297 |
|
|
306 |
$this->_value = $value; |
$this->_value = $value; |
307 |
$this->set_data_all_slaves($value); |
$this->set_data_all_slaves($value); |
308 |
} |
} |
309 |
|
|
310 |
/** |
/** |
311 |
* This function will call set_slave_data for all slaves |
* This function will call set_slave_data for all slaves |
312 |
* should be called when value of parent changes |
* should be called when value of parent changes |
313 |
* |
* |
314 |
* @param string - the current value of parent |
* @param string - the current value of parent |
315 |
*/ |
*/ |
316 |
function set_data_all_slaves($parentValue){ |
function set_data_all_slaves($parentValue){ |
392 |
*/ |
*/ |
393 |
function set_disabled($flag) { |
function set_disabled($flag) { |
394 |
$this->_is_disabled = $flag; |
$this->_is_disabled = $flag; |
395 |
|
if ($flag) { |
396 |
|
//we should make sure there was no |
397 |
|
//value posted in the request, or |
398 |
|
//someone tried to hack us. |
399 |
|
if (isset($_REQUEST[str_replace("[]","",$this->get_element_name())])) { |
400 |
|
//HACK ATTEMPT! |
401 |
|
$this->_has_error = TRUE; |
402 |
|
$this->_errors[] = array('label' => $this->get_label_text(), |
403 |
|
'message' => 'Hack attempt discovered. Disabled Element has been submitted'); |
404 |
|
} |
405 |
|
} |
406 |
} |
} |
407 |
|
|
408 |
/** |
/** |
415 |
return $this->_is_disabled; |
return $this->_is_disabled; |
416 |
} |
} |
417 |
|
|
418 |
|
|
419 |
|
/** |
420 |
|
* This sets the readonly flag. |
421 |
|
* When this flag is set, the FormContent |
422 |
|
* gets back the get_value_text() instead |
423 |
|
* of get_element(). |
424 |
|
* |
425 |
|
* @param bool TRUE = readonly |
426 |
|
*/ |
427 |
|
function set_readonly($flag=TRUE) { |
428 |
|
$this->_is_readonly = $flag; |
429 |
|
} |
430 |
|
|
431 |
|
/** |
432 |
|
* Is this element in read only mode? |
433 |
|
* |
434 |
|
* @return bool |
435 |
|
*/ |
436 |
|
function is_readonly() { |
437 |
|
return $this->_is_readonly; |
438 |
|
} |
439 |
|
|
440 |
|
|
441 |
/** |
/** |
442 |
* This sets the stripslashes flag for |
* This sets the stripslashes flag for |
443 |
* this object. |
* this object. |
595 |
* |
* |
596 |
*/ |
*/ |
597 |
function create_element_name() { |
function create_element_name() { |
598 |
|
$this->set_element_name($this->_sanitize_string($this->get_label_text())); |
599 |
|
} |
600 |
|
|
|
$name = strtolower($this->get_label_text()); |
|
|
$len = strlen($name); |
|
601 |
|
|
602 |
|
/** |
603 |
|
* This private method sanitizes a string for |
604 |
|
* putting in a form element attribute value |
605 |
|
* |
606 |
|
* @param string the string to sanitize |
607 |
|
* @return string |
608 |
|
*/ |
609 |
|
function _sanitize_string($name) { |
610 |
|
$len = strlen($name); |
611 |
for ($i=0; $i<$len;$i++) { |
for ($i=0; $i<$len;$i++) { |
612 |
if ((ord($name[$i])<97 || ord($name[$i])>122) && (ord($name[$i])<48 || ord($name[$i])>57)) |
if ((ord($name[$i])<97 || ord($name[$i])>122) && |
613 |
|
(ord($name[$i])<48 || ord($name[$i])>57) && |
614 |
|
(ord($name[$i])<65 || ord($name[$i])>90)) |
615 |
$name[$i] = "_"; |
$name[$i] = "_"; |
616 |
} |
} |
617 |
|
|
618 |
$this->set_element_name($name); |
return $name; |
619 |
} |
} |
620 |
|
|
621 |
/** |
/** |
644 |
|
|
645 |
/********* Validation methods *********/ |
/********* Validation methods *********/ |
646 |
|
|
647 |
|
/** |
648 |
|
* This method sets a flag to enable/disable |
649 |
|
* validation for the Element. |
650 |
|
* |
651 |
|
* @param boolean TRUE = enable validation |
652 |
|
*/ |
653 |
|
function enable_validation($flag=TRUE) { |
654 |
|
$this->_validation_enabled = $flag; |
655 |
|
} |
656 |
|
|
657 |
|
|
658 |
/** |
/** |
659 |
* This function performs the actual validation |
* This function performs the actual validation |
692 |
} |
} |
693 |
} |
} |
694 |
|
|
695 |
if ($has_value && !$this->is_disabled()) { |
if ($has_value && !$this->is_disabled() && $this->_validation_enabled) { |
696 |
return $this->validate($_FormValidation); |
return $this->validate($_FormValidation); |
697 |
} else if ($this->is_required()) { |
} else if ($this->is_required()) { |
698 |
$this->set_error_message("This field cannot be empty"); |
$this->set_error_message("This field cannot be empty"); |
705 |
|
|
706 |
/********* JavaScript event methods *********/ |
/********* JavaScript event methods *********/ |
707 |
|
|
|
|
|
708 |
/** |
/** |
709 |
* This method is used for adding any javascript |
* This method is used for adding any javascript |
710 |
* that is used by this element. This will automatically |
* that is used by this element. This will automatically |
716 |
return NULL; |
return NULL; |
717 |
} |
} |
718 |
|
|
719 |
|
/** |
720 |
|
* This method sets the onclick javascript |
721 |
|
* @param string - js code |
722 |
|
*/ |
723 |
|
function set_onClick($js){ |
724 |
|
$this->onClickJS = $js; |
725 |
|
} |
726 |
|
|
727 |
|
|
728 |
/** |
/** |
729 |
* This function return the javaScript code for |
* This function return the javaScript code for |
730 |
* an onClick event |
* an onClick event |
731 |
* |
* Note: if you override this function be sure to use |
732 |
|
* the appropriate $this->onClickJS and append your |
733 |
|
* JS to it before returning |
734 |
* @return string - javascript code |
* @return string - javascript code |
735 |
*/ |
*/ |
736 |
function onClick() { |
function onClick() { |
737 |
return NULL; |
return $this->onClickJS; |
738 |
} |
} |
739 |
|
|
740 |
|
/** |
741 |
|
* This method sets the onfocus javascript |
742 |
|
* @param string - js code |
743 |
|
*/ |
744 |
|
function set_onFocus($js){ |
745 |
|
$this->onFocusJS = $js; |
746 |
|
} |
747 |
|
|
748 |
/** |
/** |
749 |
* This function return the javaScript code for |
* This function return the javaScript code for |
750 |
* an onFocus event |
* an onFocus event |
751 |
* |
* Note: if you override this function be sure to use |
752 |
|
* the appropriate $this->onClickJS and append your |
753 |
|
* JS to it before returning |
754 |
* @return string - javascript code |
* @return string - javascript code |
755 |
*/ |
*/ |
756 |
function onFocus() { |
function onFocus() { |
757 |
return NULL; |
return $this->onFocusJS; |
758 |
} |
} |
759 |
|
|
760 |
|
/** |
761 |
|
* This method sets the onSubmit javascript |
762 |
|
* @param string - js code |
763 |
|
*/ |
764 |
|
function set_onSubmit($js){ |
765 |
|
$this->onSubmitJS = $js; |
766 |
|
} |
767 |
|
|
768 |
/** |
/** |
769 |
* This function return the javaScript code for |
* This function return the javaScript code for |
770 |
* an onSubmit event |
* an onSubmit event |
771 |
* |
* Note: if you override this function be sure to use |
772 |
|
* the appropriate $this->onClickJS and append your |
773 |
|
* JS to it before returning |
774 |
* @return string - javascript code |
* @return string - javascript code |
775 |
*/ |
*/ |
776 |
function onSubmit() { |
function onSubmit() { |
777 |
return NULL; |
return $this->onSubmitJS; |
778 |
} |
} |
779 |
|
|
780 |
|
/** |
781 |
|
* This method sets the onblur javascript |
782 |
|
* @param string - js code |
783 |
|
*/ |
784 |
|
function set_onBlur($js){ |
785 |
|
$this->onBlurJS = $js; |
786 |
|
} |
787 |
|
|
788 |
/** |
/** |
789 |
* This function return the javaScript code for |
* This function return the javaScript code for |
790 |
* an onBlur event |
* an onBlur event |
791 |
* |
* Note: if you override this function be sure to use |
792 |
|
* the appropriate $this->onClickJS and append your |
793 |
|
* JS to it before returning |
794 |
* @return string - javascript code |
* @return string - javascript code |
795 |
*/ |
*/ |
796 |
function onBlur() { |
function onBlur() { |
797 |
return NULL; |
return $this->onBlurJS; |
798 |
} |
} |
799 |
|
|
800 |
|
/** |
801 |
|
* This method sets the onChange javascript |
802 |
|
* @param string - js code |
803 |
|
*/ |
804 |
|
function set_onChange($js){ |
805 |
|
$this->onChangeJS = $js; |
806 |
|
} |
807 |
|
|
808 |
/** |
/** |
809 |
* this function retuns the javaScript code for |
* this function retuns the javaScript code for |
810 |
* an onChange event |
* an onChange event |
811 |
* |
* Note: if you override this function be sure to use |
812 |
|
* the appropriate $this->onClickJS and append your |
813 |
|
* JS to it before returning |
814 |
* @return string - javascript code |
* @return string - javascript code |
815 |
*/ |
*/ |
816 |
function onChange() { |
function onChange() { |
817 |
return NULL; |
return $this->onChangeJS; |
818 |
} |
} |
819 |
|
|
820 |
/** |
/** |
877 |
* @return string - required symbol |
* @return string - required symbol |
878 |
*/ |
*/ |
879 |
function get_required_symbol() { |
function get_required_symbol() { |
880 |
return $this->_required_field_marker; |
if (is_object($this->_required_field_marker)) { |
881 |
|
return $this->_required_field_marker->render(); |
882 |
|
} else { |
883 |
|
return $this->_required_field_marker; |
884 |
|
} |
885 |
} |
} |
886 |
|
|
887 |
/** |
/** |
888 |
* This allows you to customize the |
* This allows you to customize the |
889 |
* require string marker |
* require string marker |
890 |
* |
* |
891 |
* @param string |
* @param string |
892 |
*/ |
*/ |
893 |
function set_required_symbol($symbol) { |
function set_required_symbol($symbol) { |
924 |
$attributes[] = "disabled"; |
$attributes[] = "disabled"; |
925 |
} |
} |
926 |
|
|
927 |
|
//build the ID |
928 |
|
$attributes['id'] = $this->build_id_name(); |
929 |
|
|
930 |
return $attributes; |
return $attributes; |
931 |
} |
} |
932 |
|
|
933 |
|
|
934 |
|
|
935 |
|
/** |
936 |
|
* This private method is used to |
937 |
|
* build the id attribute value string |
938 |
|
* |
939 |
|
* @return string |
940 |
|
*/ |
941 |
|
function build_id_name() { |
942 |
|
return $this->_form_name.'_'.$this->get_element_name(); |
943 |
|
} |
944 |
|
|
945 |
|
|
946 |
/** |
/** |
947 |
* This function builds and returns a |
* This function builds and returns a |
948 |
* label object based on the label text |
* label object based on the label text |
949 |
* and error conditions |
* and error conditions |
950 |
* |
* |
951 |
* @param FormContent object that holds the |
* @param FormContent object that holds the |
952 |
* required field marker |
* required field marker |
953 |
|
* @param string this string allows us to use this |
954 |
|
* method and wrap any string as a FormElement |
955 |
|
* label. |
956 |
* @return object SPANtag |
* @return object SPANtag |
957 |
*/ |
*/ |
958 |
function get_label($form_content=NULL) { |
function get_label($form_content=NULL, $label='', $indent_flag=TRUE) { |
959 |
|
|
960 |
$label = $this->get_label_text(); |
if ($label == '') { |
961 |
$text = $label; |
$label = $this->get_label_text(); |
962 |
|
} |
963 |
|
|
964 |
if ($this->is_required()) { |
//check to see if the form content |
965 |
$text .= ' ' . ($form_content ? $form_content->_required_field_marker : |
//is read only. |
966 |
$this->get_required_symbol()); |
if (!is_null( $form_content )) { |
967 |
|
if ($form_content->is_readonly()) { |
968 |
|
|
969 |
|
$this->set_readonly(TRUE); |
970 |
|
} |
971 |
|
} |
972 |
|
|
973 |
|
if ($this->is_required() && !$this->is_readonly()) { |
974 |
|
$text = ($form_content ? $form_content->get_required_marker() : |
975 |
|
$this->get_required_symbol()). ' '.$label; |
976 |
|
} else { |
977 |
|
if ($indent_flag) { |
978 |
|
$text = ' '.$label; |
979 |
|
} else { |
980 |
|
$text = $label; |
981 |
|
} |
982 |
|
} |
983 |
|
|
984 |
|
if ($this->_label_colon_flag) { |
985 |
|
$text .= ':'; |
986 |
} |
} |
987 |
|
|
988 |
$span = html_span("formlabel", $text); |
$span = html_span("formlabel", $text); |
989 |
|
|
990 |
if ($this->has_error()) { |
if ($this->has_error($label)) { |
991 |
$span->set_tag_attribute("style","color:red;"); |
$span->set_tag_attribute("style","color:red;"); |
992 |
} |
} |
993 |
|
|
994 |
return $span; |
return $span; |
995 |
} |
} |
996 |
|
|
997 |
|
/** |
998 |
|
* This method checks to see if |
999 |
|
* this element is readonly and returns |
1000 |
|
* the get_value_text() or returns get_element() |
1001 |
|
* if it isn't readonly |
1002 |
|
* |
1003 |
|
* @param bool force readonly? |
1004 |
|
* @return mixed |
1005 |
|
*/ |
1006 |
|
function &get_form_element($force_readonly=FALSE) { |
1007 |
|
if ($force_readonly) { |
1008 |
|
$this->set_readonly(TRUE); |
1009 |
|
} |
1010 |
|
|
1011 |
|
if ($this->is_readonly()) { |
1012 |
|
return container($this->get_value_text(),$this->get_confirm_element()); |
1013 |
|
} else { |
1014 |
|
return $this->get_element(); |
1015 |
|
} |
1016 |
|
} |
1017 |
|
|
1018 |
|
|
1019 |
/** |
/** |
1020 |
* This function builds and returns the |
* This function builds and returns the |
1021 |
* form element object |
* form element object. This method |
1022 |
|
* ignores the readonly flag. |
1023 |
* |
* |
1024 |
* @return object |
* @return object |
1025 |
*/ |
*/ |