adding some strings
[moodle-linuxchix.git] / lib / pear / HTML / QuickForm / advcheckbox.php
blob0a9089f66e80f54b16c65c97ef9f33f3d102a480
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4: */
3 // +----------------------------------------------------------------------+
4 // | PHP version 4.0 |
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 // +----------------------------------------------------------------------+
20 // $Id$
22 require_once('HTML/QuickForm/checkbox.php');
24 /**
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
37 * its value.
39 * @author Jason Rust <jrust@php.net>
40 * @since 2.0
41 * @access public
43 class HTML_QuickForm_advcheckbox extends HTML_QuickForm_checkbox
45 // {{{ properties
47 /**
48 * The values passed by the hidden elment
50 * @var array
51 * @access private
53 var $_values = null;
55 /**
56 * The default value
58 * @var boolean
59 * @access private
61 var $_currentValue = null;
63 // }}}
64 // {{{ constructor
66 /**
67 * Class constructor
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
76 * @since 1.0
77 * @access public
78 * @return void
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);
84 } //end constructor
86 // }}}
87 // {{{ getPrivateName()
89 /**
90 * Gets the private name for the element
92 * @param string $elementName The element name to make private
94 * @access public
95 * @return string
97 * @deprecated Deprecated since 3.2.6, both generated elements have the same name
99 function getPrivateName($elementName)
101 return '__'.$elementName;
104 // }}}
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
113 * @access public
114 * @return string
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], '\'').'\'; }';
122 return $onclickJs;
125 // }}}
126 // {{{ setValues()
129 * Sets the values used by the hidden element
131 * @param mixed $values The values, either a string or an array
133 * @access public
134 * @return void
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);
145 } else {
146 $this->_values = $values;
148 $this->updateAttributes(array('value' => $this->_values[1]));
149 $this->setChecked($this->_currentValue == $this->_values[1]);
152 // }}}
153 // {{{ setValue()
156 * Sets the element's value
158 * @param mixed Element's value
159 * @access public
161 function setValue($value)
163 $this->setChecked(isset($this->_values[1]) && $value == $this->_values[1]);
164 $this->_currentValue = $value;
167 // }}}
168 // {{{ getValue()
171 * Returns the element's value
173 * @access public
174 * @return mixed
176 function getValue()
178 if (is_array($this->_values)) {
179 return $this->_values[$this->getChecked()? 1: 0];
180 } else {
181 return null;
185 // }}}
186 // {{{ toHtml()
189 * Returns the checkbox element in HTML
190 * and the additional hidden element in HTML
192 * @access public
193 * @return string
195 function toHtml()
197 if ($this->_flagFrozen) {
198 return parent::toHtml();
199 } else {
200 return '<input' . $this->_getAttrString(array(
201 'type' => 'hidden',
202 'name' => $this->getName(),
203 'value' => $this->_values[0]
204 )) . ' />' . parent::toHtml();
207 } //end func toHtml
209 // }}}
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();
222 // }}}
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
231 * @since 1.0
232 * @access public
233 * @return void
235 function onQuickFormEvent($event, $arg, &$caller)
237 switch ($event) {
238 case 'updateValue':
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);
251 break;
252 default:
253 parent::onQuickFormEvent($event, $arg, $caller);
255 return true;
256 } // end func onQuickFormLoad
258 // }}}
259 // {{{ exportValue()
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])) {
271 $value = null;
273 return $this->_prepareValue($value, $assoc);
275 // }}}
276 } //end class HTML_QuickForm_advcheckbox