commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-old / civicrm / packages / HTML / QuickForm / radio.php
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3
4 /**
5 * HTML class for a radio type element
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 * @copyright 2001-2009 The PHP Group
20 * @license http://www.php.net/license/3_01.txt PHP License 3.01
21 * @version CVS: $Id: radio.php,v 1.20 2009/04/04 21:34:04 avb Exp $
22 * @link http://pear.php.net/package/HTML_QuickForm
23 */
24
25 /**
26 * Base class for <input /> form elements
27 */
28 require_once 'HTML/QuickForm/input.php';
29
30 /**
31 * HTML class for a radio type element
32 *
33 * @category HTML
34 * @package HTML_QuickForm
35 * @author Adam Daniel <adaniel1@eesus.jnj.com>
36 * @author Bertrand Mansion <bmansion@mamasam.com>
37 * @version Release: 3.2.11
38 * @since 1.0
39 */
40 class HTML_QuickForm_radio extends HTML_QuickForm_input
41 {
42 // {{{ properties
43
44 /**
45 * Radio display text
46 * @var string
47 * @since 1.1
48 * @access private
49 */
50 var $_text = '';
51
52 // }}}
53 // {{{ constructor
54
55 /**
56 * Class constructor
57 *
58 * @param string Input field name attribute
59 * @param mixed Label(s) for a field
60 * @param string Text to display near the radio
61 * @param string Input field value
62 * @param mixed Either a typical HTML attribute string or an associative array
63 * @since 1.0
64 * @access public
65 * @return void
66 */
67 function HTML_QuickForm_radio($elementName=null, $elementLabel=null, $text=null, $value=null, $attributes=null)
68 {
69
70 $this->HTML_QuickForm_element($elementName, $elementLabel, $attributes);
71 if (isset($value)) {
72 $this->setValue($value);
73 }
74 $this->_persistantFreeze = true;
75 $this->setType('radio');
76 $this->_text = $text;
77 // $this->_generateId();
78 if ( ! $this->getAttribute('id') ) {
79 //hack to add 'id' for radio
80 static $idTextStr = 1;
81 if (CRM_Utils_Array::value('id_suffix', $attributes)) {
82 $idSuffix = $attributes['id_suffix'];
83 $this->removeAttribute('id_suffix');
84 }
85 else {
86 $idSuffix = $idTextStr;
87 $idTextStr++;
88 }
89
90 $this->updateAttributes( array('id' => CRM_Utils_String::munge( "CIVICRM_QFID_{$value}_{$idSuffix}" ) ) );
91 }
92 } //end constructor
93
94 // }}}
95 // {{{ setChecked()
96
97 /**
98 * Sets whether radio button is checked
99 *
100 * @param bool $checked Whether the field is checked or not
101 * @since 1.0
102 * @access public
103 * @return void
104 */
105 function setChecked($checked)
106 {
107 if (!$checked) {
108 $this->removeAttribute('checked');
109 } else {
110 $this->updateAttributes(array('checked'=>'checked'));
111 }
112 } //end func setChecked
113
114 // }}}
115 // {{{ getChecked()
116
117 /**
118 * Returns whether radio button is checked
119 *
120 * @since 1.0
121 * @access public
122 * @return string
123 */
124 function getChecked()
125 {
126 return $this->getAttribute('checked');
127 } //end func getChecked
128
129 // }}}
130 // {{{ toHtml()
131
132 /**
133 * Returns the radio element in HTML
134 *
135 * @since 1.0
136 * @access public
137 * @return string
138 */
139 function toHtml()
140 {
141 if (0 == strlen($this->_text)) {
142 $label = '';
143 } elseif ($this->_flagFrozen) {
144 $label = $this->_text;
145 } else {
146 $label = '<label for="' . $this->getAttribute('id') . '">' . $this->_text . '</label>';
147 }
148 return HTML_QuickForm_input::toHtml() . $label;
149 } //end func toHtml
150
151 // }}}
152 // {{{ getFrozenHtml()
153
154 /**
155 * Returns the value of field without HTML tags
156 *
157 * @since 1.0
158 * @access public
159 * @return string
160 */
161 function getFrozenHtml()
162 {
163 if ($this->getChecked()) {
164 return '<tt>(x)</tt>' .
165 $this->_getPersistantData();
166 } else {
167 return '<tt>( )</tt>';
168 }
169 } //end func getFrozenHtml
170
171 // }}}
172 // {{{ setText()
173
174 /**
175 * Sets the radio text
176 *
177 * @param string $text Text to display near the radio button
178 * @since 1.1
179 * @access public
180 * @return void
181 */
182 function setText($text)
183 {
184 $this->_text = $text;
185 } //end func setText
186
187 // }}}
188 // {{{ getText()
189
190 /**
191 * Returns the radio text
192 *
193 * @since 1.1
194 * @access public
195 * @return string
196 */
197 function getText()
198 {
199 return $this->_text;
200 } //end func getText
201
202 // }}}
203 // {{{ onQuickFormEvent()
204
205 /**
206 * Called by HTML_QuickForm whenever form event is made on this element
207 *
208 * @param string $event Name of event
209 * @param mixed $arg event arguments
210 * @param object &$caller calling object
211 * @since 1.0
212 * @access public
213 * @return void
214 */
215 function onQuickFormEvent($event, $arg, &$caller)
216 {
217 switch ($event) {
218 case 'updateValue':
219 // constant values override both default and submitted ones
220 $value = $this->_findValue($caller->_constantValues);
221 if (null === $value) {
222 // we should retrieve value from submitted values when form is submitted,
223 // else set value from defaults values
224 if ( $caller->isSubmitted( ) ) {
225 $value = $this->_findValue($caller->_submitValues);
226 } else {
227 $value = $this->_findValue($caller->_defaultValues);
228 }
229 }
230 if (!is_null($value) && $value == $this->getValue()) {
231 $this->setChecked(true);
232 } else {
233 $this->setChecked(false);
234 }
235 break;
236 case 'setGroupValue':
237 if ($arg == $this->getValue()) {
238 $this->setChecked(true);
239 } else {
240 $this->setChecked(false);
241 }
242 break;
243 default:
244 parent::onQuickFormEvent($event, $arg, $caller);
245 }
246 return true;
247 } // end func onQuickFormLoad
248
249 // }}}
250 // {{{ exportValue()
251
252 /**
253 * Returns the value attribute if the radio is checked, null if it is not
254 */
255 function exportValue(&$submitValues, $assoc = false)
256 {
257 $value = $this->_findValue($submitValues);
258 if (null === $value) {
259 // fix to return blank value when all radio's are unselected / not selected
260 // always use submitted values rather than defaults
261 //$value = $this->getChecked()? $this->getValue(): null;
262 $value = '';
263 } elseif ($value != $this->getValue()) {
264 $value = null;
265 }
266 return $this->_prepareValue($value, $assoc);
267 }
268
269 // }}}
270 } //end class HTML_QuickForm_radio
271 ?>