2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
5 * Base class for form elements
9 * LICENSE: This source file is subject to version 3.01 of the PHP license
10 * that is available through the world-wide-web at the following URI:
11 * http://www.php.net/license/3_01.txt If you did not receive a copy of
12 * the PHP License and are unable to obtain it through the web, please
13 * send a note to license@php.net so we can mail you a copy immediately.
16 * @package HTML_QuickForm
17 * @author Adam Daniel <adaniel1@eesus.jnj.com>
18 * @author Bertrand Mansion <bmansion@mamasam.com>
19 * @author Alexey Borzov <avb@php.net>
20 * @copyright 2001-2011 The PHP Group
21 * @license http://www.php.net/license/3_01.txt PHP License 3.01
23 * @link http://pear.php.net/package/HTML_QuickForm
27 * Base class for all HTML classes
29 require_once 'HTML/Common.php';
31 * Static utility methods
33 require_once 'HTML/QuickForm/utils.php';
36 * Base class for form elements
39 * @package HTML_QuickForm
40 * @author Adam Daniel <adaniel1@eesus.jnj.com>
41 * @author Bertrand Mansion <bmansion@mamasam.com>
42 * @author Alexey Borzov <avb@php.net>
43 * @version Release: 3.2.16
47 class HTML_QuickForm_element extends HTML_Common
68 * Flag to tell if element is frozen
73 var $_flagFrozen = false;
76 * Does the element support persistant data when frozen
81 var $_persistantFreeze = false;
89 * @param string Name of the element
90 * @param mixed Label(s) for the element
91 * @param mixed Associative array of tag attributes or HTML attributes name="value" pairs
96 function HTML_QuickForm_element($elementName=null, $elementLabel=null, $attributes=null)
98 HTML_Common::HTML_Common($attributes);
99 if (isset($elementName)) {
100 $this->setName($elementName);
102 if (isset($elementLabel)) {
103 $this->setLabel($elementLabel);
111 * Returns the current API version
117 function apiVersion()
120 } // end func apiVersion
126 * Returns element type
135 } // end func getType
141 * Sets the input field name
143 * @param string $name Input field name attribute
148 function setName($name)
157 * Returns the element name
172 * Sets the value of the form element
174 * @param string $value Default value of the form element
179 function setValue($value)
182 } // end func setValue
188 * Returns the value of the form element
198 } // end func getValue
204 * Freeze the element so that only its value is returned
211 $this->_flagFrozen = true;
218 * Unfreezes the element so that it becomes editable
226 $this->_flagFrozen = false;
230 // {{{ getFrozenHtml()
233 * Returns the value of field without HTML tags
239 function getFrozenHtml()
241 $value = $this->getValue();
242 return (strlen($value)? htmlspecialchars($value): ' ') .
243 $this->_getPersistantData();
244 } //end func getFrozenHtml
247 // {{{ _getPersistantData()
250 * Used by getFrozenHtml() to pass the element's value if _persistantFreeze is on
255 function _getPersistantData()
257 if (!$this->_persistantFreeze) {
260 $id = $this->getAttribute('id');
261 return '<input' . $this->_getAttrString(array(
263 'name' => $this->getName(),
264 'value' => $this->getValue()
265 ) + (isset($id)? array('id' => $id): array())) . ' />';
273 * Returns whether or not the element is frozen
281 return $this->_flagFrozen;
282 } // end func isFrozen
285 // {{{ setPersistantFreeze()
288 * Sets wether an element value should be kept in an hidden field
289 * when the element is frozen or not
291 * @param bool $persistant True if persistant value
296 function setPersistantFreeze($persistant=false)
298 $this->_persistantFreeze = $persistant;
299 } //end func setPersistantFreeze
305 * Sets display text for the element
307 * @param string $label Display text for the element
312 function setLabel($label)
314 $this->_label = $label;
315 } //end func setLabel
321 * Returns display text for the element
329 return $this->_label;
330 } //end func getLabel
336 * Tries to find the element value from the values array
342 function _findValue(&$values)
344 if (empty($values)) {
347 $elementName = $this->getName();
348 if (isset($values[$elementName])) {
349 return $values[$elementName];
350 } elseif (strpos($elementName, '[')) {
353 array('\\', '\'', ']', '['), array('\\\\', '\\\'', '', "']['"),
356 $arrayKeys = explode("']['", $keys);
357 return HTML_QuickForm_utils::recursiveValue($values, $arrayKeys);
362 } //end func _findValue
365 // {{{ onQuickFormEvent()
368 * Called by HTML_QuickForm whenever form event is made on this element
370 * @param string $event Name of event
371 * @param mixed $arg event arguments
372 * @param object &$caller calling object
377 function onQuickFormEvent($event, $arg, &$caller)
380 case 'createElement':
381 $className = get_class($this);
382 $this->$className($arg[0], $arg[1], $arg[2], $arg[3], $arg[4]);
385 $this->onQuickFormEvent('createElement', $arg, $caller);
386 $this->onQuickFormEvent('updateValue', null, $caller);
389 // constant values override both default and submitted ones
390 // default values are overriden by submitted
391 $value = $this->_findValue($caller->_constantValues);
392 if (null === $value) {
393 $value = $this->_findValue($caller->_submitValues);
394 if (null === $value) {
395 $value = $this->_findValue($caller->_defaultValues);
398 if (null !== $value) {
399 $this->setValue($value);
402 case 'setGroupValue':
403 $this->setValue($arg);
406 } // end func onQuickFormEvent
414 * @param HTML_QuickForm_Renderer renderer object
415 * @param bool Whether an element is required
416 * @param string An error message associated with an element
420 function accept(&$renderer, $required=false, $error=null)
422 $renderer->renderElement($this, $required, $error);
429 * Automatically generates and assigns an 'id' attribute for the element.
431 * Currently used to ensure that labels work on radio buttons and
432 * checkboxes. Per idea of Alexander Radivanovich.
437 function _generateId()
441 if (!$this->getAttribute('id')) {
442 $this->updateAttributes(array('id' => 'qf_' . substr(md5(microtime() . $idx++), 0, 6)));
444 } // end func _generateId
450 * Returns a 'safe' element's value
452 * @param array array of submitted values to search
453 * @param bool whether to return the value as associative array
457 function exportValue(&$submitValues, $assoc = false)
459 $value = $this->_findValue($submitValues);
460 if (null === $value) {
461 $value = $this->getValue();
463 return $this->_prepareValue($value, $assoc);
467 // {{{ _prepareValue()
470 * Used by exportValue() to prepare the value for returning
472 * @param mixed the value found in exportValue()
473 * @param bool whether to return the value as associative array
477 function _prepareValue($value, $assoc)
479 if (null === $value) {
484 $name = $this->getName();
485 if (!strpos($name, '[')) {
486 return array($name => $value);
490 array('\\', '\'', ']', '['), array('\\\\', '\\\'', '', "']['"),
493 $keysArray = explode("']['", $keys);
494 return HTML_QuickForm_utils::recursiveBuild($keysArray, $value);
500 } // end class HTML_QuickForm_element