1 |
<?php |
2 |
/** |
3 |
* This file contains the base FormElement class. |
4 |
* |
5 |
* $Id: FormElement.inc,v 1.28 2003/07/18 01:23:43 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 |
/** |
122 |
* The constructor |
123 |
* |
124 |
* @param label string - text label for the element |
125 |
* @param bool required - is this a required element |
126 |
*/ |
127 |
function FormElement($label, $required = TRUE) { |
128 |
|
129 |
$this->set_label_text($label); |
130 |
$this->set_required($required); |
131 |
|
132 |
// create a unique name to be used in the html form |
133 |
$this->create_element_name(); |
134 |
|
135 |
//This sets the initial value of the element |
136 |
$this->set_value( $this->get_init_value() ); |
137 |
} |
138 |
|
139 |
|
140 |
/** |
141 |
* This function will return the |
142 |
* elements label text |
143 |
* |
144 |
* @return string |
145 |
*/ |
146 |
function get_label_text() { |
147 |
return $this->_label_text; |
148 |
} |
149 |
|
150 |
/** |
151 |
* This function will set the |
152 |
* label for the element |
153 |
* |
154 |
* @param label string |
155 |
*/ |
156 |
function set_label_text($label) { |
157 |
$this->_label_text = $label; |
158 |
} |
159 |
|
160 |
/** |
161 |
* This function will return the |
162 |
* elements value |
163 |
* |
164 |
* @return mixed |
165 |
*/ |
166 |
function get_value() { |
167 |
if ($this->_stripslashes) { |
168 |
return stripslashes($this->_value); |
169 |
} else { |
170 |
return $this->_value; |
171 |
} |
172 |
} |
173 |
|
174 |
/** |
175 |
* This function will set the |
176 |
* initial value for the element |
177 |
* |
178 |
* @param value mixed |
179 |
*/ |
180 |
function set_value($value) { |
181 |
$this->_value = $value; |
182 |
} |
183 |
|
184 |
/** |
185 |
* in case anyone in JS land |
186 |
* needs the name of the form that |
187 |
* this element lives in |
188 |
* |
189 |
* @param string - the form name |
190 |
*/ |
191 |
function set_form_name($name) { |
192 |
$this->_form_name = $name; |
193 |
} |
194 |
|
195 |
/** |
196 |
* This provides a method |
197 |
* for the FormContent |
198 |
* to get access to the |
199 |
* text associated with a |
200 |
* field. This is only available |
201 |
* on FormElements that have text |
202 |
* associated with a field. |
203 |
* It is used during Confirmation |
204 |
* |
205 |
* @param mixed - the value to look up |
206 |
* @return string - the text associated |
207 |
*/ |
208 |
function get_value_text($value) { |
209 |
return $this->get_value(); |
210 |
} |
211 |
|
212 |
|
213 |
/** |
214 |
* This function set the elements |
215 |
* required state |
216 |
* |
217 |
* @param bool required |
218 |
*/ |
219 |
function set_required($required) { |
220 |
$this->_is_required = $required; |
221 |
} |
222 |
|
223 |
/** |
224 |
* Returns whether this elements |
225 |
* final value cannot be empty |
226 |
* |
227 |
* @return bool requried |
228 |
*/ |
229 |
function is_required() { |
230 |
return $this->_is_required; |
231 |
} |
232 |
|
233 |
|
234 |
/** |
235 |
* This returns the initial value of |
236 |
* the element |
237 |
* |
238 |
* @return mixed |
239 |
*/ |
240 |
function get_init_value() { |
241 |
return @$_REQUEST[str_replace("[]","",$this->get_element_name())]; |
242 |
} |
243 |
|
244 |
|
245 |
/** |
246 |
* Sets the disabled element flag |
247 |
* |
248 |
* @param bool disabled |
249 |
*/ |
250 |
function set_disabled($flag) { |
251 |
$this->_is_disabled = $flag; |
252 |
} |
253 |
|
254 |
/** |
255 |
* Returns the elements disabled |
256 |
* state |
257 |
* |
258 |
* @return bool disabled |
259 |
*/ |
260 |
function is_disabled() { |
261 |
return $this->_is_disabled; |
262 |
} |
263 |
|
264 |
/** |
265 |
* This sets the stripslashes flag for |
266 |
* this object. |
267 |
* |
268 |
* @param boolean |
269 |
*/ |
270 |
function set_stripslashes( $flag = TRUE ) { |
271 |
$this->_stripslashes = $flag; |
272 |
} |
273 |
|
274 |
|
275 |
/********* Tag attributes methods *********/ |
276 |
|
277 |
|
278 |
/** |
279 |
* add a single attribute (name="value") |
280 |
* |
281 |
* @param string $name attribute name |
282 |
* @param mixed $value the value |
283 |
* |
284 |
*/ |
285 |
function set_attribute($name, $value = NULL) { |
286 |
if ($value == NULL) { |
287 |
$this->_attributes[] = $name; |
288 |
} |
289 |
else { |
290 |
$this->_attributes[$name] = $value; |
291 |
} |
292 |
} |
293 |
|
294 |
/** |
295 |
* return a single attribute |
296 |
* |
297 |
* @param string $name attribute name |
298 |
* @return mixed $value the value |
299 |
* |
300 |
*/ |
301 |
function get_attribute($name) { |
302 |
return $this->_attributes[$name]; |
303 |
} |
304 |
|
305 |
/** |
306 |
* Sets elements title text |
307 |
* |
308 |
* @param string title |
309 |
*/ |
310 |
function set_title($title) { |
311 |
$this->set_attribute("title", $title); |
312 |
} |
313 |
|
314 |
/** |
315 |
* Sets elements css attribute |
316 |
* |
317 |
* @param string $name attribute name |
318 |
* @param mixed $value the value |
319 |
*/ |
320 |
function set_style_attribute($name, $value) { |
321 |
$this->_style_attributes[$name] = $value; |
322 |
} |
323 |
|
324 |
|
325 |
/********* Error handling methods *********/ |
326 |
|
327 |
/** |
328 |
* Defines error message text and |
329 |
* sets the error flag to true |
330 |
* |
331 |
* @param mesage text - error message |
332 |
* @param label text - a label to provide |
333 |
* for the error. This is |
334 |
* only needed for a complex |
335 |
* element that has multiple |
336 |
* 'hidden/magic' fields. |
337 |
*/ |
338 |
function set_error_message($message, $label=NULL) { |
339 |
if ($label == NULL) { |
340 |
$label = $this->get_label_text(); |
341 |
} |
342 |
$this->_errors[] = array("label" => $label, |
343 |
"message" => $message); |
344 |
$this->_has_error = TRUE; |
345 |
} |
346 |
|
347 |
/** |
348 |
* This returns the array of errors |
349 |
* for this element |
350 |
* |
351 |
* @param array of errors |
352 |
*/ |
353 |
function get_errors() { |
354 |
return $this->_errors; |
355 |
} |
356 |
|
357 |
|
358 |
/** |
359 |
* Returns the current error message |
360 |
* if any |
361 |
* |
362 |
* @return mesage text - error message |
363 |
*/ |
364 |
function get_error_message() { |
365 |
return $this->_error_message; |
366 |
} |
367 |
|
368 |
/** |
369 |
* Returns the current error state |
370 |
* |
371 |
* |
372 |
* @return bool error state |
373 |
*/ |
374 |
function has_error($label=NULL) { |
375 |
if ($label==NULL) { |
376 |
return $this->_has_error; |
377 |
} else { |
378 |
//xxx( $label ); |
379 |
//they are looking for a specific error |
380 |
foreach( $this->_errors as $error) { |
381 |
|
382 |
if ($error['label'] == $label) { |
383 |
xxx( $error['label'] ); |
384 |
return TRUE; |
385 |
} |
386 |
} |
387 |
return FALSE; |
388 |
} |
389 |
|
390 |
} |
391 |
|
392 |
|
393 |
/********* Element name handling methods *********/ |
394 |
|
395 |
/** |
396 |
* This function creates element name |
397 |
* used in the form based on the text label |
398 |
* or any other parameters |
399 |
* |
400 |
*/ |
401 |
function create_element_name() { |
402 |
|
403 |
$name = strtolower($this->get_label_text()); |
404 |
$len = strlen($name); |
405 |
|
406 |
for ($i=0; $i<$len;$i++) { |
407 |
if ((ord($name[$i])<97 || ord($name[$i])>122) && (ord($name[$i])<48 || ord($name[$i])>57)) |
408 |
$name[$i] = "_"; |
409 |
} |
410 |
|
411 |
$this->_element_name = $name; |
412 |
} |
413 |
|
414 |
/** |
415 |
* Returns the element name |
416 |
* to be used in the form |
417 |
* |
418 |
* @return string form element name |
419 |
*/ |
420 |
function get_element_name() { |
421 |
return $this->_element_name; |
422 |
} |
423 |
|
424 |
|
425 |
/********* Validation methods *********/ |
426 |
|
427 |
|
428 |
/** |
429 |
* This function performs the actual validation |
430 |
* It is called only if the validation is required by |
431 |
* this element |
432 |
* |
433 |
* This function is responsible for performing complete |
434 |
* validation and setting the appropriate error message |
435 |
* in case of a failed validation |
436 |
* |
437 |
* @param FormValidation object. |
438 |
*/ |
439 |
function validate(&$_FormValidation) { |
440 |
return TRUE; |
441 |
} |
442 |
|
443 |
|
444 |
/** |
445 |
* This function checks if the validation |
446 |
* is nesseccary and calls the validate method |
447 |
* |
448 |
* @param FormValidation object. |
449 |
*/ |
450 |
function _do_validation(&$_FormValidation) { |
451 |
$value = $this->get_value(); |
452 |
$has_value = FALSE; |
453 |
if (is_array($value)) { |
454 |
foreach($value as $entry) { |
455 |
if ($entry != NULL) { |
456 |
$has_value = TRUE; |
457 |
} |
458 |
} |
459 |
} else { |
460 |
if ($value != NULL) { |
461 |
$has_value = TRUE; |
462 |
} |
463 |
} |
464 |
|
465 |
if ($has_value && !$this->is_disabled()) { |
466 |
return $this->validate($_FormValidation); |
467 |
} else if ($this->is_required()) { |
468 |
$this->set_error_message("This field cannot be empty"); |
469 |
return FALSE; |
470 |
} else { |
471 |
return TRUE; |
472 |
} |
473 |
} |
474 |
|
475 |
|
476 |
/********* JavaScript event methods *********/ |
477 |
|
478 |
|
479 |
/** |
480 |
* This method is used for adding any javascript |
481 |
* that is used by this element. This will automatically |
482 |
* get called and added to the page by the FormProcessor |
483 |
* |
484 |
* @return string - raw js |
485 |
*/ |
486 |
function javascript() { |
487 |
return NULL; |
488 |
} |
489 |
|
490 |
/** |
491 |
* This function return the javaScript code for |
492 |
* an onClick event |
493 |
* |
494 |
* @return string - javascript code |
495 |
*/ |
496 |
function onClick() { |
497 |
return NULL; |
498 |
} |
499 |
|
500 |
/** |
501 |
* This function return the javaScript code for |
502 |
* an onFocus event |
503 |
* |
504 |
* @return string - javascript code |
505 |
*/ |
506 |
function onFocus() { |
507 |
return NULL; |
508 |
} |
509 |
|
510 |
/** |
511 |
* This function return the javaScript code for |
512 |
* an onSubmit event |
513 |
* |
514 |
* @return string - javascript code |
515 |
*/ |
516 |
function onSubmit() { |
517 |
return NULL; |
518 |
} |
519 |
|
520 |
/** |
521 |
* This function return the javaScript code for |
522 |
* an onBlur event |
523 |
* |
524 |
* @return string - javascript code |
525 |
*/ |
526 |
function onBlur() { |
527 |
return NULL; |
528 |
} |
529 |
|
530 |
|
531 |
/** |
532 |
* this function retuns the javaScript code for |
533 |
* an onChange event |
534 |
* |
535 |
* @return string - javascript code |
536 |
*/ |
537 |
function onChange() { |
538 |
return NULL; |
539 |
} |
540 |
|
541 |
/** |
542 |
* This function builds the complete javaScript events code |
543 |
* for the element |
544 |
* |
545 |
* @return array - attributes |
546 |
*/ |
547 |
function _build_javascript() { |
548 |
|
549 |
$javascript = array(); |
550 |
|
551 |
if (($js=$this->onClick()) != NULL) { |
552 |
$javascript[] = "onclick=\"" . $js . "\""; |
553 |
} |
554 |
|
555 |
if (($js=$this->onFocus()) != NULL) { |
556 |
$javascript[] = "onfocus=\"" . $js . "\""; |
557 |
} |
558 |
|
559 |
if (($js=$this->onSubmit()) != NULL) { |
560 |
$javascript[] = "onsubmit=\"" . $js . "\""; |
561 |
} |
562 |
|
563 |
if (($js=$this->onBlur()) != NULL) { |
564 |
$javascript[] = "onblur=\"" . $js . "\""; |
565 |
} |
566 |
|
567 |
if (($js=$this->onChange()) != NULL) { |
568 |
$javascript[] = "onchange=\"" . $js . "\""; |
569 |
} |
570 |
|
571 |
if (count($javascript)>0) { |
572 |
return $javascript; |
573 |
} |
574 |
else { |
575 |
return NULL; |
576 |
} |
577 |
} |
578 |
|
579 |
|
580 |
/********* rendering methods *********/ |
581 |
|
582 |
|
583 |
/** |
584 |
* This function return the symbol used to |
585 |
* denote a required field |
586 |
* |
587 |
* @return string - required symbol |
588 |
*/ |
589 |
function get_required_symbol() { |
590 |
return '*'; |
591 |
} |
592 |
|
593 |
/** |
594 |
* This function builds the element form attributes |
595 |
* |
596 |
* @return array attributes |
597 |
*/ |
598 |
function _build_element_attributes() { |
599 |
|
600 |
$attributes = $this->_attributes; |
601 |
|
602 |
if ($this->_style_attributes) { |
603 |
// build the css styles |
604 |
foreach ($this->_style_attributes as $name=>$value) { |
605 |
$style[] = $name . ":" . $value . ";"; |
606 |
} |
607 |
$attributes["style"] = implode("", $style); |
608 |
} |
609 |
|
610 |
// set the element tag name |
611 |
$attributes["name"] = $this->get_element_name(); |
612 |
|
613 |
if (($js = $this->_build_javascript()) != NULL) { |
614 |
// build javaScript attributes |
615 |
$attributes = array_merge($attributes, $js); |
616 |
} |
617 |
|
618 |
//see if its disabled |
619 |
if ($this->is_disabled()) { |
620 |
$attributes[] = "disabled"; |
621 |
} |
622 |
|
623 |
return $attributes; |
624 |
} |
625 |
|
626 |
|
627 |
/** |
628 |
* This function builds and returns a |
629 |
* label object based on the label text |
630 |
* and error conditions |
631 |
* |
632 |
* @return object SPANtag |
633 |
*/ |
634 |
function get_label() { |
635 |
|
636 |
$label = $this->get_label_text(); |
637 |
|
638 |
if ($this->is_required()) $label .= ' ' . $this->get_required_symbol(); |
639 |
|
640 |
$span = html_span("formlabel", $label); |
641 |
|
642 |
if ($this->has_error($label)) { |
643 |
$span->set_tag_attribute("style","color:red;"); |
644 |
} |
645 |
|
646 |
return $span; |
647 |
} |
648 |
|
649 |
|
650 |
/** |
651 |
* This function builds and returns the |
652 |
* form element object |
653 |
* |
654 |
* @return object |
655 |
*/ |
656 |
function get_element() { |
657 |
|
658 |
$span = new SPANtag($this->_build_element_attributes(), $this->get_value()); |
659 |
|
660 |
return $span; |
661 |
} |
662 |
|
663 |
} // FormElement |
664 |
?> |