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/checkbox.php');
25 * HTML class for an advanced checkbox type field
27 * Basically this fixes a problem that HTML has had
28 * where checkboxes can only pass a single value (the
29 * value of the checkbox when checked). A value for when
30 * the checkbox is not checked cannot be passed, and
31 * furthermore the checkbox variable doesn't even exist if
32 * the checkbox was submitted unchecked.
34 * It works by prepending a hidden field with the same name and
35 * another "unchecked" value to the checbox. If the checkbox is
36 * checked, PHP overwrites the value of the hidden field with
39 * @author Jason Rust <jrust@php.net>
43 class HTML_QuickForm_advcheckbox
extends HTML_QuickForm_checkbox
48 * The values passed by the hidden elment
61 var $_currentValue = null;
69 * @param string $elementName (optional)Input field name attribute
70 * @param string $elementLabel (optional)Input field label
71 * @param string $text (optional)Text to put after the checkbox
72 * @param mixed $attributes (optional)Either a typical HTML attribute string
73 * or an associative array
74 * @param mixed $values (optional)Values to pass if checked or not checked
80 function HTML_QuickForm_advcheckbox($elementName=null, $elementLabel=null, $text=null, $attributes=null, $values=null)
82 $this->HTML_QuickForm_checkbox($elementName, $elementLabel, $text, $attributes);
83 $this->setValues($values);
87 // {{{ getPrivateName()
90 * Gets the private name for the element
92 * @param string $elementName The element name to make private
97 * @deprecated Deprecated since 3.2.6, both generated elements have the same name
99 function getPrivateName($elementName)
101 return '__'.$elementName;
105 // {{{ getOnclickJs()
108 * Create the javascript for the onclick event which will
109 * set the value of the hidden field
111 * @param string $elementName The element name
116 * @deprecated Deprecated since 3.2.6, this element no longer uses any javascript
118 function getOnclickJs($elementName)
120 $onclickJs = 'if (this.checked) { this.form[\''.$elementName.'\'].value=\''.addcslashes($this->_values
[1], '\'').'\'; }';
121 $onclickJs .= 'else { this.form[\''.$elementName.'\'].value=\''.addcslashes($this->_values
[0], '\'').'\'; }';
129 * Sets the values used by the hidden element
131 * @param mixed $values The values, either a string or an array
136 function setValues($values)
138 if (empty($values)) {
139 // give it default checkbox behavior
140 $this->_values
= array('', 1);
141 } elseif (is_scalar($values)) {
142 // if it's string, then assume the value to
143 // be passed is for when the element is checked
144 $this->_values
= array('', $values);
146 $this->_values
= $values;
148 $this->updateAttributes(array('value' => $this->_values
[1]));
149 $this->setChecked($this->_currentValue
== $this->_values
[1]);
156 * Sets the element's value
158 * @param mixed Element's value
161 function setValue($value)
163 $this->setChecked(isset($this->_values
[1]) && $value == $this->_values
[1]);
164 $this->_currentValue
= $value;
171 * Returns the element's value
178 if (is_array($this->_values
)) {
179 return $this->_values
[$this->getChecked()?
1: 0];
189 * Returns the checkbox element in HTML
190 * and the additional hidden element in HTML
197 if ($this->_flagFrozen
) {
198 return parent
::toHtml();
200 return '<input' . $this->_getAttrString(array(
202 'name' => $this->getName(),
203 'value' => $this->_values
[0]
204 )) . ' />' . parent
::toHtml();
210 // {{{ getFrozenHtml()
213 * Unlike checkbox, this has to append a hidden input in both
214 * checked and non-checked states
216 function getFrozenHtml()
218 return ($this->getChecked()?
'<tt>[x]</tt>': '<tt>[ ]</tt>') .
219 $this->_getPersistantData();
223 // {{{ onQuickFormEvent()
226 * Called by HTML_QuickForm whenever form event is made on this element
228 * @param string $event Name of event
229 * @param mixed $arg event arguments
230 * @param object $caller calling object
235 function onQuickFormEvent($event, $arg, &$caller)
239 // constant values override both default and submitted ones
240 // default values are overriden by submitted
241 $value = $this->_findValue($caller->_constantValues
);
242 if (null === $value) {
243 $value = $this->_findValue($caller->_submitValues
);
244 if (null === $value) {
245 $value = $this->_findValue($caller->_defaultValues
);
248 if (null !== $value) {
249 $this->setValue($value);
253 parent
::onQuickFormEvent($event, $arg, $caller);
256 } // end func onQuickFormLoad
262 * This element has a value even if it is not checked, thus we override
263 * checkbox's behaviour here
265 function exportValue(&$submitValues, $assoc)
267 $value = $this->_findValue($submitValues);
268 if (null === $value) {
269 $value = $this->getValue();
270 } elseif (is_array($this->_values
) && ($value != $this->_values
[0]) && ($value != $this->_values
[1])) {
273 return $this->_prepareValue($value, $assoc);
276 } //end class HTML_QuickForm_advcheckbox