commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-old / civicrm / packages / HTML / QuickForm / checkbox.php
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3
4 /**
5 * HTML class for a 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 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
24 */
25
26 /**
27 * Base class for <input /> form elements
28 */
29 require_once 'HTML/QuickForm/input.php';
30
31 /**
32 * HTML class for a checkbox type field
33 *
34 * @category HTML
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
40 * @since 1.0
41 */
42 class HTML_QuickForm_checkbox extends HTML_QuickForm_input
43 {
44 // {{{ properties
45
46 /**
47 * Checkbox display text
48 * @var string
49 * @since 1.1
50 * @access private
51 */
52 var $_text = '';
53
54 // }}}
55 // {{{ constructor
56
57 /**
58 * Class constructor
59 *
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
65 * @since 1.0
66 * @access public
67 * @return void
68 */
69 function HTML_QuickForm_checkbox($elementName=null, $elementLabel=null, $text='', $attributes=null)
70 {
71 //hack to add 'id' for checkbox
72 if ( !$attributes ) {
73 $attributes = array( 'id' => $elementName );
74 } else {
75 // set element id only if its not set
76 if ( !isset( $attributes['id'] ) ) {
77 $attributes['id'] = $elementName;
78 }
79 }
80
81 HTML_QuickForm_input::HTML_QuickForm_input($elementName, $elementLabel, $attributes);
82 $this->_persistantFreeze = true;
83 $this->_text = $text;
84 $this->setType('checkbox');
85 $this->updateAttributes(array('value'=>1));
86 $this->_generateId();
87 } //end constructor
88
89 // }}}
90 // {{{ setChecked()
91
92 /**
93 * Sets whether a checkbox is checked
94 *
95 * @param bool $checked Whether the field is checked or not
96 * @since 1.0
97 * @access public
98 * @return void
99 */
100 function setChecked($checked)
101 {
102 if (!$checked) {
103 $this->removeAttribute('checked');
104 } else {
105 $this->updateAttributes(array('checked'=>'checked'));
106 }
107 } //end func setChecked
108
109 // }}}
110 // {{{ getChecked()
111
112 /**
113 * Returns whether a checkbox is checked
114 *
115 * @since 1.0
116 * @access public
117 * @return bool
118 */
119 function getChecked()
120 {
121 return (bool)$this->getAttribute('checked');
122 } //end func getChecked
123
124 // }}}
125 // {{{ toHtml()
126
127 /**
128 * Returns the checkbox element in HTML
129 *
130 * @since 1.0
131 * @access public
132 * @return string
133 */
134 function toHtml()
135 {
136 $attributes = $this->getAttributes();
137
138 if (0 == strlen($this->_text)) {
139 $label = '';
140 } elseif ($this->_flagFrozen || isset( $attributes['skiplabel']) ) {
141 $label = $this->_text;
142 } else {
143 $label = '<label for="' . $this->getAttribute('id') . '">' . $this->_text . '</label>';
144 }
145
146 unset( $attributes['skipLabel'] );
147 return HTML_QuickForm_input::toHtml() . $label;
148 } //end func toHtml
149
150 // }}}
151 // {{{ getFrozenHtml()
152
153 /**
154 * Returns the value of field without HTML tags
155 *
156 * @since 1.0
157 * @access public
158 * @return string
159 */
160 function getFrozenHtml()
161 {
162 if ($this->getChecked()) {
163 return '<tt>[x]</tt>' .
164 $this->_getPersistantData();
165 } else {
166 return '<tt>[ ]</tt>';
167 }
168 } //end func getFrozenHtml
169
170 // }}}
171 // {{{ setText()
172
173 /**
174 * Sets the checkbox text
175 *
176 * @param string $text
177 * @since 1.1
178 * @access public
179 * @return void
180 */
181 function setText($text)
182 {
183 $this->_text = $text;
184 } //end func setText
185
186 // }}}
187 // {{{ getText()
188
189 /**
190 * Returns the checkbox text
191 *
192 * @since 1.1
193 * @access public
194 * @return string
195 */
196 function getText()
197 {
198 return $this->_text;
199 } //end func getText
200
201 // }}}
202 // {{{ setValue()
203
204 /**
205 * Sets the value of the form element
206 *
207 * @param string $value Default value of the form element
208 * @since 1.0
209 * @access public
210 * @return void
211 */
212 function setValue($value)
213 {
214 return $this->setChecked($value);
215 } // end func setValue
216
217 // }}}
218 // {{{ getValue()
219
220 /**
221 * Returns the value of the form element
222 *
223 * @since 1.0
224 * @access public
225 * @return bool
226 */
227 function getValue()
228 {
229 return $this->getChecked();
230 } // end func getValue
231
232 // }}}
233 // {{{ onQuickFormEvent()
234
235 /**
236 * Called by HTML_QuickForm whenever form event is made on this element
237 *
238 * @param string $event Name of event
239 * @param mixed $arg event arguments
240 * @param object &$caller calling object
241 * @since 1.0
242 * @access public
243 * @return void
244 */
245 function onQuickFormEvent($event, $arg, &$caller)
246 {
247 switch ($event) {
248 case 'updateValue':
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);
257 } else {
258 $value = $this->_findValue($caller->_defaultValues);
259 }
260 }
261 if (null !== $value || $caller->isSubmitted()) {
262 $this->setChecked($value);
263 }
264 break;
265 case 'setGroupValue':
266 $this->setChecked($arg);
267 break;
268 default:
269 parent::onQuickFormEvent($event, $arg, $caller);
270 }
271 return true;
272 } // end func onQuickFormEvent
273
274 // }}}
275 // {{{ exportValue()
276
277 /**
278 * Return true if the checkbox is checked, null if it is not checked (getValue() returns false)
279 */
280 function exportValue(&$submitValues, $assoc = false)
281 {
282 $value = $this->_findValue($submitValues);
283 if (null === $value) {
284 $value = $this->getChecked()? true: null;
285 }
286 return $this->_prepareValue($value, $assoc);
287 }
288
289 // }}}
290 } //end class HTML_QuickForm_checkbox
291 ?>