2 /* vim: set expandtab tabstop=4 shiftwidth=4: */
3 // +----------------------------------------------------------------------+
5 // +----------------------------------------------------------------------+
6 // | Copyright (c) 1997, 1998, 1999, 2000, 2001 The PHP Group |
7 // +----------------------------------------------------------------------+
8 // | This source file is subject to version 2.0 of the PHP license, |
9 // | that is bundled with this package in the file LICENSE, and is |
10 // | available at through the world-wide-web at |
11 // | http://www.php.net/license/2_02.txt. |
12 // | If you did not receive a copy of the PHP license and are unable to |
13 // | obtain it through the world-wide-web, please send a note to |
14 // | license@php.net so we can mail you a copy immediately. |
15 // +----------------------------------------------------------------------+
16 // | Authors: Adam Daniel <adaniel1@eesus.jnj.com> |
17 // | Bertrand Mansion <bmansion@mamasam.com> |
18 // +----------------------------------------------------------------------+
22 require_once('HTML/Common.php');
25 * Base class for form elements
27 * @author Adam Daniel <adaniel1@eesus.jnj.com>
28 * @author Bertrand Mansion <bmansion@mamasam.com>
34 class HTML_QuickForm_element
extends HTML_Common
55 * Flag to tell if element is frozen
60 var $_flagFrozen = false;
63 * Does the element support persistant data when frozen
68 var $_persistantFreeze = false;
76 * @param string Name of the element
77 * @param mixed Label(s) for the element
78 * @param mixed Associative array of tag attributes or HTML attributes name="value" pairs
83 function HTML_QuickForm_element($elementName=null, $elementLabel=null, $attributes=null)
85 HTML_Common
::HTML_Common($attributes);
86 if (isset($elementName)) {
87 $this->setName($elementName);
89 if (isset($elementLabel)) {
90 $this->setLabel($elementLabel);
98 * Returns the current API version
104 function apiVersion()
107 } // end func apiVersion
113 * Returns element type
122 } // end func getType
128 * Sets the input field name
130 * @param string $name Input field name attribute
135 function setName($name)
144 * Returns the element name
159 * Sets the value of the form element
161 * @param string $value Default value of the form element
166 function setValue($value)
169 } // end func setValue
175 * Returns the value of the form element
185 } // end func getValue
191 * Freeze the element so that only its value is returned
198 $this->_flagFrozen
= true;
205 * Unfreezes the element so that it becomes editable
213 $this->_flagFrozen
= false;
217 // {{{ getFrozenHtml()
220 * Returns the value of field without HTML tags
226 function getFrozenHtml()
228 $value = $this->getValue();
229 return ('' != $value?
htmlspecialchars($value): ' ') .
230 $this->_getPersistantData();
231 } //end func getFrozenHtml
234 // {{{ _getPersistantData()
237 * Used by getFrozenHtml() to pass the element's value if _persistantFreeze is on
242 function _getPersistantData()
244 if (!$this->_persistantFreeze
) {
247 $id = $this->getAttribute('id');
248 return '<input' . $this->_getAttrString(array(
250 'name' => $this->getName(),
251 'value' => $this->getValue()
252 ) +
(isset($id)?
array('id' => $id): array())) . ' />';
260 * Returns whether or not the element is frozen
268 return $this->_flagFrozen
;
269 } // end func isFrozen
272 // {{{ setPersistantFreeze()
275 * Sets wether an element value should be kept in an hidden field
276 * when the element is frozen or not
278 * @param bool $persistant True if persistant value
283 function setPersistantFreeze($persistant=false)
285 $this->_persistantFreeze
= $persistant;
286 } //end func setPersistantFreeze
292 * Sets display text for the element
294 * @param string $label Display text for the element
299 function setLabel($label)
301 $this->_label
= $label;
302 } //end func setLabel
308 * Returns display text for the element
316 return $this->_label
;
317 } //end func getLabel
323 * Tries to find the element value from the values array
329 function _findValue(&$values)
331 if (empty($values)) {
334 $elementName = $this->getName();
335 if (isset($values[$elementName])) {
336 return $values[$elementName];
337 } elseif (strpos($elementName, '[')) {
338 $myVar = "['" . str_replace(array(']', '['), array('', "']['"), $elementName) . "']";
339 return eval("return (isset(\$values$myVar)) ? \$values$myVar : null;");
343 } //end func _findValue
346 // {{{ onQuickFormEvent()
349 * Called by HTML_QuickForm whenever form event is made on this element
351 * @param string $event Name of event
352 * @param mixed $arg event arguments
353 * @param object $caller calling object
358 function onQuickFormEvent($event, $arg, &$caller)
361 case 'createElement':
362 $className = get_class($this);
363 $this->$className($arg[0], $arg[1], $arg[2], $arg[3], $arg[4]);
366 $this->onQuickFormEvent('createElement', $arg, $caller);
367 $this->onQuickFormEvent('updateValue', null, $caller);
370 // constant values override both default and submitted ones
371 // default values are overriden by submitted
372 $value = $this->_findValue($caller->_constantValues
);
373 if (null === $value) {
374 $value = $this->_findValue($caller->_submitValues
);
375 if (null === $value) {
376 $value = $this->_findValue($caller->_defaultValues
);
379 if (null !== $value) {
380 $this->setValue($value);
383 case 'setGroupValue':
384 $this->setValue($arg);
387 } // end func onQuickFormEvent
395 * @param object An HTML_QuickForm_Renderer object
396 * @param bool Whether an element is required
397 * @param string An error message associated with an element
401 function accept(&$renderer, $required=false, $error=null)
403 $renderer->renderElement($this, $required, $error);
410 * Automatically generates and assigns an 'id' attribute for the element.
412 * Currently used to ensure that labels work on radio buttons and
413 * checkboxes. Per idea of Alexander Radivanovich.
418 function _generateId()
422 if (!$this->getAttribute('id')) {
423 $this->updateAttributes(array('id' => 'qf_' . substr(md5(microtime() . $idx++
), 0, 6)));
425 } // end func _generateId
431 * Returns a 'safe' element's value
433 * @param array array of submitted values to search
434 * @param bool whether to return the value as associative array
438 function exportValue(&$submitValues, $assoc = false)
440 $value = $this->_findValue($submitValues);
441 if (null === $value) {
442 $value = $this->getValue();
444 return $this->_prepareValue($value, $assoc);
448 // {{{ _prepareValue()
451 * Used by exportValue() to prepare the value for returning
453 * @param mixed the value found in exportValue()
454 * @param bool whether to return the value as associative array
458 function _prepareValue($value, $assoc)
460 if (null === $value) {
465 $name = $this->getName();
466 if (!strpos($name, '[')) {
467 return array($name => $value);
470 $myIndex = "['" . str_replace(array(']', '['), array('', "']['"), $name) . "']";
471 eval("\$valueAry$myIndex = \$value;");
478 } // end class HTML_QuickForm_element