commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-new / civicrm / packages / HTML / QuickForm / advcheckbox.php
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3
4 /**
5 * HTML class for an advanced checkbox type field
6 *
7 * PHP versions 4 and 5
8 *
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.
14 *
15 * @category HTML
16 * @package HTML_QuickForm
17 * @author Jason Rust <jrust@php.net>
18 * @author Alexey Borzov <avb@php.net>
19 * @copyright 2001-2009 The PHP Group
20 * @license http://www.php.net/license/3_01.txt PHP License 3.01
21 * @version CVS: $Id: advcheckbox.php,v 1.18 2009/04/04 21:34:02 avb Exp $
22 * @link http://pear.php.net/package/HTML_QuickForm
23 */
24
25 /**
26 * HTML class for a checkbox type field
27 */
28 require_once 'HTML/QuickForm/checkbox.php';
29
30 /**
31 * HTML class for an advanced checkbox type field
32 *
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.
39 *
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
43 * its value.
44 *
45 * @category HTML
46 * @package HTML_QuickForm
47 * @author Jason Rust <jrust@php.net>
48 * @author Alexey Borzov <avb@php.net>
49 * @version Release: 3.2.11
50 * @since 2.0
51 */
52 class HTML_QuickForm_advcheckbox extends HTML_QuickForm_checkbox
53 {
54 // {{{ properties
55
56 /**
57 * The values passed by the hidden elment
58 *
59 * @var array
60 * @access private
61 */
62 var $_values = null;
63
64 /**
65 * The default value
66 *
67 * @var boolean
68 * @access private
69 */
70 var $_currentValue = null;
71
72 // }}}
73 // {{{ constructor
74
75 /**
76 * Class constructor
77 *
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
84 *
85 * @since 1.0
86 * @access public
87 * @return void
88 */
89 function HTML_QuickForm_advcheckbox($elementName=null, $elementLabel=null, $text=null, $attributes=null, $values=null)
90 {
91 $this->HTML_QuickForm_checkbox($elementName, $elementLabel, $text, $attributes);
92 $this->setValues($values);
93 } //end constructor
94
95 // }}}
96 // {{{ getPrivateName()
97
98 /**
99 * Gets the private name for the element
100 *
101 * @param string $elementName The element name to make private
102 *
103 * @access public
104 * @return string
105 *
106 * @deprecated Deprecated since 3.2.6, both generated elements have the same name
107 */
108 function getPrivateName($elementName)
109 {
110 return '__'.$elementName;
111 }
112
113 // }}}
114 // {{{ getOnclickJs()
115
116 /**
117 * Create the javascript for the onclick event which will
118 * set the value of the hidden field
119 *
120 * @param string $elementName The element name
121 *
122 * @access public
123 * @return string
124 *
125 * @deprecated Deprecated since 3.2.6, this element no longer uses any javascript
126 */
127 function getOnclickJs($elementName)
128 {
129 $onclickJs = 'if (this.checked) { this.form[\''.$elementName.'\'].value=\''.addcslashes($this->_values[1], '\'').'\'; }';
130 $onclickJs .= 'else { this.form[\''.$elementName.'\'].value=\''.addcslashes($this->_values[0], '\'').'\'; }';
131 return $onclickJs;
132 }
133
134 // }}}
135 // {{{ setValues()
136
137 /**
138 * Sets the values used by the hidden element
139 *
140 * @param mixed $values The values, either a string or an array
141 *
142 * @access public
143 * @return void
144 */
145 function setValues($values)
146 {
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);
154 } else {
155 $this->_values = $values;
156 }
157 $this->updateAttributes(array('value' => $this->_values[1]));
158 $this->setChecked($this->_currentValue == $this->_values[1]);
159 }
160
161 // }}}
162 // {{{ setValue()
163
164 /**
165 * Sets the element's value
166 *
167 * @param mixed Element's value
168 * @access public
169 */
170 function setValue($value)
171 {
172 $this->setChecked(isset($this->_values[1]) && $value == $this->_values[1]);
173 $this->_currentValue = $value;
174 }
175
176 // }}}
177 // {{{ getValue()
178
179 /**
180 * Returns the element's value
181 *
182 * @access public
183 * @return mixed
184 */
185 function getValue()
186 {
187 if (is_array($this->_values)) {
188 return $this->_values[$this->getChecked()? 1: 0];
189 } else {
190 return null;
191 }
192 }
193
194 // }}}
195 // {{{ toHtml()
196
197 /**
198 * Returns the checkbox element in HTML
199 * and the additional hidden element in HTML
200 *
201 * @access public
202 * @return string
203 */
204 function toHtml()
205 {
206 if ($this->_flagFrozen) {
207 return parent::toHtml();
208 } else {
209 return '<input' . $this->_getAttrString(array(
210 'type' => 'hidden',
211 'name' => $this->getName(),
212 'value' => $this->_values[0]
213 )) . ' />' . parent::toHtml();
214
215 }
216 } //end func toHtml
217
218 // }}}
219 // {{{ getFrozenHtml()
220
221 /**
222 * Unlike checkbox, this has to append a hidden input in both
223 * checked and non-checked states
224 */
225 function getFrozenHtml()
226 {
227 return ($this->getChecked()? '<tt>[x]</tt>': '<tt>[ ]</tt>') .
228 $this->_getPersistantData();
229 }
230
231 // }}}
232 // {{{ onQuickFormEvent()
233
234 /**
235 * Called by HTML_QuickForm whenever form event is made on this element
236 *
237 * @param string $event Name of event
238 * @param mixed $arg event arguments
239 * @param object &$caller calling object
240 * @since 1.0
241 * @access public
242 * @return void
243 */
244 function onQuickFormEvent($event, $arg, &$caller)
245 {
246 switch ($event) {
247 case 'updateValue':
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);
255 }
256 }
257 if (null !== $value) {
258 $this->setValue($value);
259 }
260 break;
261 default:
262 parent::onQuickFormEvent($event, $arg, $caller);
263 }
264 return true;
265 } // end func onQuickFormLoad
266
267 // }}}
268 // {{{ exportValue()
269
270 /**
271 * This element has a value even if it is not checked, thus we override
272 * checkbox's behaviour here
273 */
274 function exportValue(&$submitValues, $assoc = false)
275 {
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])) {
280 $value = null;
281 }
282 return $this->_prepareValue($value, $assoc);
283 }
284 // }}}
285 } //end class HTML_QuickForm_advcheckbox
286 ?>