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-2009 The PHP Group
21 * @license http://www.php.net/license/3_01.txt PHP License 3.01
22 * @version CVS: $Id: checkbox.php,v 1.23 2009/04/04 21:34:02 avb Exp $
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.11
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 //hack to add 'id' for checkbox
73 $attributes = array( 'id' => $elementName );
75 // set element id only if its not set
76 if ( !isset( $attributes['id'] ) ) {
77 $attributes['id'] = $elementName;
81 HTML_QuickForm_input
::HTML_QuickForm_input($elementName, $elementLabel, $attributes);
82 $this->_persistantFreeze
= true;
84 $this->setType('checkbox');
85 $this->updateAttributes(array('value'=>1));
93 * Sets whether a checkbox is checked
95 * @param bool $checked Whether the field is checked or not
100 function setChecked($checked)
103 $this->removeAttribute('checked');
105 $this->updateAttributes(array('checked'=>'checked'));
107 } //end func setChecked
113 * Returns whether a checkbox is checked
119 function getChecked()
121 return (bool)$this->getAttribute('checked');
122 } //end func getChecked
128 * Returns the checkbox element in HTML
136 $attributes = $this->getAttributes();
138 if (0 == strlen($this->_text
)) {
140 } elseif ($this->_flagFrozen ||
isset( $attributes['skiplabel']) ) {
141 $label = $this->_text
;
143 $label = '<label for="' . $this->getAttribute('id') . '">' . $this->_text
. '</label>';
146 unset( $attributes['skipLabel'] );
147 return HTML_QuickForm_input
::toHtml() . $label;
151 // {{{ getFrozenHtml()
154 * Returns the value of field without HTML tags
160 function getFrozenHtml()
162 if ($this->getChecked()) {
163 return '<tt>[x]</tt>' .
164 $this->_getPersistantData();
166 return '<tt>[ ]</tt>';
168 } //end func getFrozenHtml
174 * Sets the checkbox text
176 * @param string $text
181 function setText($text)
183 $this->_text
= $text;
190 * Returns the checkbox text
205 * Sets the value of the form element
207 * @param string $value Default value of the form element
212 function setValue($value)
214 return $this->setChecked($value);
215 } // end func setValue
221 * Returns the value of the form element
229 return $this->getChecked();
230 } // end func getValue
233 // {{{ onQuickFormEvent()
236 * Called by HTML_QuickForm whenever form event is made on this element
238 * @param string $event Name of event
239 * @param mixed $arg event arguments
240 * @param object &$caller calling object
245 function onQuickFormEvent($event, $arg, &$caller)
249 // constant values override both default and submitted ones
250 // default values are overriden by submitted
251 $value = $this->_findValue($caller->_constantValues
);
252 if (null === $value) {
253 // if no boxes were checked, then there is no value in the array
254 // yet we don't want to display default value in this case
255 if ($caller->isSubmitted()) {
256 $value = $this->_findValue($caller->_submitValues
);
258 $value = $this->_findValue($caller->_defaultValues
);
261 if (null !== $value ||
$caller->isSubmitted()) {
262 $this->setChecked($value);
265 case 'setGroupValue':
266 $this->setChecked($arg);
269 parent
::onQuickFormEvent($event, $arg, $caller);
272 } // end func onQuickFormEvent
278 * Return true if the checkbox is checked, null if it is not checked (getValue() returns false)
280 function exportValue(&$submitValues, $assoc = false)
282 $value = $this->_findValue($submitValues);
283 if (null === $value) {
284 $value = $this->getChecked()?
true: null;
286 return $this->_prepareValue($value, $assoc);
290 } //end class HTML_QuickForm_checkbox