2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
5 * HTML class for a checkbox type field
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 <input /> form elements
29 require_once 'HTML/QuickForm/input.php';
32 * HTML class for a checkbox type field
35 * @package HTML_QuickForm
36 * @author Adam Daniel <adaniel1@eesus.jnj.com>
37 * @author Bertrand Mansion <bmansion@mamasam.com>
38 * @author Alexey Borzov <avb@php.net>
39 * @version Release: 3.2.16
42 class HTML_QuickForm_checkbox extends HTML_QuickForm_input
47 * Checkbox display text
60 * @param string $elementName (optional)Input field name attribute
61 * @param string $elementLabel (optional)Input field value
62 * @param string $text (optional)Checkbox display text
63 * @param mixed $attributes (optional)Either a typical HTML attribute string
64 * or an associative array
69 function HTML_QuickForm_checkbox($elementName=null, $elementLabel=null, $text='', $attributes=null)
71 HTML_QuickForm_input::HTML_QuickForm_input($elementName, $elementLabel, $attributes);
72 $this->_persistantFreeze = true;
74 $this->setType('checkbox');
75 $this->updateAttributes(array('value'=>1));
83 * Sets whether a checkbox is checked
85 * @param bool $checked Whether the field is checked or not
90 function setChecked($checked)
93 $this->removeAttribute('checked');
95 $this->updateAttributes(array('checked'=>'checked'));
97 } //end func setChecked
103 * Returns whether a checkbox is checked
109 function getChecked()
111 return (bool)$this->getAttribute('checked');
112 } //end func getChecked
118 * Returns the checkbox element in HTML
126 if (0 == strlen($this->_text)) {
128 } elseif ($this->_flagFrozen) {
129 $label = $this->_text;
131 $label = '<label for="' . $this->getAttribute('id') . '">' . $this->_text . '</label>';
133 return HTML_QuickForm_input::toHtml() . $label;
137 // {{{ getFrozenHtml()
140 * Returns the value of field without HTML tags
146 function getFrozenHtml()
148 if ($this->getChecked()) {
149 return '<tt>[x]</tt>' .
150 $this->_getPersistantData();
152 return '<tt>[ ]</tt>';
154 } //end func getFrozenHtml
160 * Sets the checkbox text
162 * @param string $text
167 function setText($text)
169 $this->_text = $text;
176 * Returns the checkbox text
191 * Sets the value of the form element
193 * @param string $value Default value of the form element
198 function setValue($value)
200 return $this->setChecked($value);
201 } // end func setValue
207 * Returns the value of the form element
215 return $this->getChecked();
216 } // end func getValue
219 // {{{ onQuickFormEvent()
222 * Called by HTML_QuickForm whenever form event is made on this element
224 * @param string $event Name of event
225 * @param mixed $arg event arguments
226 * @param object &$caller calling object
231 function onQuickFormEvent($event, $arg, &$caller)
235 // constant values override both default and submitted ones
236 // default values are overriden by submitted
237 $value = $this->_findValue($caller->_constantValues);
238 if (null === $value) {
239 // if no boxes were checked, then there is no value in the array
240 // yet we don't want to display default value in this case
241 if ($caller->isSubmitted()) {
242 $value = $this->_findValue($caller->_submitValues);
244 $value = $this->_findValue($caller->_defaultValues);
247 if (null !== $value || $caller->isSubmitted()) {
248 $this->setChecked($value);
251 case 'setGroupValue':
252 $this->setChecked($arg);
255 parent::onQuickFormEvent($event, $arg, $caller);
258 } // end func onQuickFormEvent
264 * Return true if the checkbox is checked, null if it is not checked (getValue() returns false)
266 function exportValue(&$submitValues, $assoc = false)
268 $value = $this->_findValue($submitValues);
269 if (null === $value) {
270 $value = $this->getChecked()? true: null;
272 return $this->_prepareValue($value, $assoc);
276 } //end class HTML_QuickForm_checkbox