2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
5 * HTML class for an advanced 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 Jason Rust <jrust@php.net>
18 * @author Alexey Borzov <avb@php.net>
19 * @copyright 2001-2011 The PHP Group
20 * @license http://www.php.net/license/3_01.txt PHP License 3.01
22 * @link http://pear.php.net/package/HTML_QuickForm
26 * HTML class for a checkbox type field
28 require_once 'HTML/QuickForm/checkbox.php';
31 * HTML class for an advanced checkbox type field
33 * Basically this fixes a problem that HTML has had
34 * where checkboxes can only pass a single value (the
35 * value of the checkbox when checked). A value for when
36 * the checkbox is not checked cannot be passed, and
37 * furthermore the checkbox variable doesn't even exist if
38 * the checkbox was submitted unchecked.
40 * It works by prepending a hidden field with the same name and
41 * another "unchecked" value to the checbox. If the checkbox is
42 * checked, PHP overwrites the value of the hidden field with
46 * @package HTML_QuickForm
47 * @author Jason Rust <jrust@php.net>
48 * @author Alexey Borzov <avb@php.net>
49 * @version Release: 3.2.16
52 class HTML_QuickForm_advcheckbox extends HTML_QuickForm_checkbox
57 * The values passed by the hidden elment
70 var $_currentValue = null;
78 * @param string $elementName (optional)Input field name attribute
79 * @param string $elementLabel (optional)Input field label
80 * @param string $text (optional)Text to put after the checkbox
81 * @param mixed $attributes (optional)Either a typical HTML attribute string
82 * or an associative array
83 * @param mixed $values (optional)Values to pass if checked or not checked
89 function HTML_QuickForm_advcheckbox($elementName=null, $elementLabel=null, $text=null, $attributes=null, $values=null)
91 $this->HTML_QuickForm_checkbox($elementName, $elementLabel, $text, $attributes);
92 $this->setValues($values);
96 // {{{ getPrivateName()
99 * Gets the private name for the element
101 * @param string $elementName The element name to make private
106 * @deprecated Deprecated since 3.2.6, both generated elements have the same name
108 function getPrivateName($elementName)
110 return '__'.$elementName;
114 // {{{ getOnclickJs()
117 * Create the javascript for the onclick event which will
118 * set the value of the hidden field
120 * @param string $elementName The element name
125 * @deprecated Deprecated since 3.2.6, this element no longer uses any javascript
127 function getOnclickJs($elementName)
129 $onclickJs = 'if (this.checked) { this.form[\''.$elementName.'\'].value=\''.addcslashes($this->_values[1], '\'').'\'; }';
130 $onclickJs .= 'else { this.form[\''.$elementName.'\'].value=\''.addcslashes($this->_values[0], '\'').'\'; }';
138 * Sets the values used by the hidden element
140 * @param mixed $values The values, either a string or an array
145 function setValues($values)
147 if (empty($values)) {
148 // give it default checkbox behavior
149 $this->_values = array('', 1);
150 } elseif (is_scalar($values)) {
151 // if it's string, then assume the value to
152 // be passed is for when the element is checked
153 $this->_values = array('', $values);
155 $this->_values = $values;
157 $this->updateAttributes(array('value' => $this->_values[1]));
158 $this->setChecked($this->_currentValue == $this->_values[1]);
165 * Sets the element's value
167 * @param mixed Element's value
170 function setValue($value)
172 $this->setChecked(isset($this->_values[1]) && $value == $this->_values[1]);
173 $this->_currentValue = $value;
180 * Returns the element's value
187 if (is_array($this->_values)) {
188 return $this->_values[$this->getChecked()? 1: 0];
198 * Returns the checkbox element in HTML
199 * and the additional hidden element in HTML
206 if ($this->_flagFrozen) {
207 return parent::toHtml();
209 return '<input' . $this->_getAttrString(array(
211 'name' => $this->getName(),
212 'value' => $this->_values[0]
213 )) . ' />' . parent::toHtml();
219 // {{{ getFrozenHtml()
222 * Unlike checkbox, this has to append a hidden input in both
223 * checked and non-checked states
225 function getFrozenHtml()
227 return ($this->getChecked()? '<tt>[x]</tt>': '<tt>[ ]</tt>') .
228 $this->_getPersistantData();
232 // {{{ onQuickFormEvent()
235 * Called by HTML_QuickForm whenever form event is made on this element
237 * @param string $event Name of event
238 * @param mixed $arg event arguments
239 * @param object &$caller calling object
244 function onQuickFormEvent($event, $arg, &$caller)
248 // constant values override both default and submitted ones
249 // default values are overriden by submitted
250 $value = $this->_findValue($caller->_constantValues);
251 if (null === $value) {
252 $value = $this->_findValue($caller->_submitValues);
253 if (null === $value) {
254 $value = $this->_findValue($caller->_defaultValues);
257 if (null !== $value) {
258 $this->setValue($value);
262 parent::onQuickFormEvent($event, $arg, $caller);
265 } // end func onQuickFormLoad
271 * This element has a value even if it is not checked, thus we override
272 * checkbox's behaviour here
274 function exportValue(&$submitValues, $assoc = false)
276 $value = $this->_findValue($submitValues);
277 if (null === $value) {
278 $value = $this->getValue();
279 } elseif (is_array($this->_values) && ($value != $this->_values[0]) && ($value != $this->_values[1])) {
282 return $this->_prepareValue($value, $assoc);
285 } //end class HTML_QuickForm_advcheckbox