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/QuickForm/input.php");
25 * HTML class for a checkbox type field
27 * @author Adam Daniel <adaniel1@eesus.jnj.com>
28 * @author Bertrand Mansion <bmansion@mamasam.com>
33 class HTML_QuickForm_checkbox
extends HTML_QuickForm_input
38 * Checkbox display text
51 * @param string $elementName (optional)Input field name attribute
52 * @param string $elementLabel (optional)Input field value
53 * @param string $text (optional)Checkbox display text
54 * @param mixed $attributes (optional)Either a typical HTML attribute string
55 * or an associative array
60 function HTML_QuickForm_checkbox($elementName=null, $elementLabel=null, $text='', $attributes=null)
62 HTML_QuickForm_input
::HTML_QuickForm_input($elementName, $elementLabel, $attributes);
63 $this->_persistantFreeze
= true;
65 $this->setType('checkbox');
66 $this->updateAttributes(array('value'=>1));
74 * Sets whether a checkbox is checked
76 * @param bool $checked Whether the field is checked or not
81 function setChecked($checked)
84 $this->removeAttribute('checked');
86 $this->updateAttributes(array('checked'=>'checked'));
88 } //end func setChecked
94 * Returns whether a checkbox is checked
100 function getChecked()
102 return (bool)$this->getAttribute('checked');
103 } //end func getChecked
109 * Returns the checkbox element in HTML
117 if (0 == strlen($this->_text
)) {
119 } elseif ($this->_flagFrozen
) {
120 $label = $this->_text
;
122 $label = '<label for="' . $this->getAttribute('id') . '">' . $this->_text
. '</label>';
124 return HTML_QuickForm_input
::toHtml() . $label;
128 // {{{ getFrozenHtml()
131 * Returns the value of field without HTML tags
137 function getFrozenHtml()
139 if ($this->getChecked()) {
140 return '<tt>[x]</tt>' .
141 $this->_getPersistantData();
143 return '<tt>[ ]</tt>';
145 } //end func getFrozenHtml
151 * Sets the checkbox text
153 * @param string $text
158 function setText($text)
160 $this->_text
= $text;
167 * Returns the checkbox text
182 * Sets the value of the form element
184 * @param string $value Default value of the form element
189 function setValue($value)
191 return $this->setChecked($value);
192 } // end func setValue
198 * Returns the value of the form element
206 return $this->getChecked();
207 } // end func getValue
210 // {{{ onQuickFormEvent()
213 * Called by HTML_QuickForm whenever form event is made on this element
215 * @param string $event Name of event
216 * @param mixed $arg event arguments
217 * @param object $caller calling object
222 function onQuickFormEvent($event, $arg, &$caller)
226 // constant values override both default and submitted ones
227 // default values are overriden by submitted
228 $value = $this->_findValue($caller->_constantValues
);
229 if (null === $value) {
230 // if no boxes were checked, then there is no value in the array
231 // yet we don't want to display default value in this case
232 if ($caller->isSubmitted()) {
233 $value = $this->_findValue($caller->_submitValues
);
235 $value = $this->_findValue($caller->_defaultValues
);
238 if (null !== $value) {
239 $this->setChecked($value);
242 case 'setGroupValue':
243 $this->setChecked($arg);
246 parent
::onQuickFormEvent($event, $arg, $caller);
249 } // end func onQuickFormEvent
255 * Return true if the checkbox is checked, null if it is not checked (getValue() returns false)
257 function exportValue(&$submitValues, $assoc = false)
259 $value = $this->_findValue($submitValues);
260 if (null === $value) {
261 $value = $this->getChecked()?
true: null;
263 return $this->_prepareValue($value, $assoc);
267 } //end class HTML_QuickForm_checkbox