2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
5 * HTML class for a radio type element
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.
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
26 * Base class for <input /> form elements
28 require_once 'HTML/QuickForm/input.php';
31 * HTML class for a radio type element
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
40 class HTML_QuickForm_radio
extends HTML_QuickForm_input
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
67 function HTML_QuickForm_radio($elementName=null, $elementLabel=null, $text=null, $value=null, $attributes=null)
70 $this->HTML_QuickForm_element($elementName, $elementLabel, $attributes);
72 $this->setValue($value);
74 $this->_persistantFreeze
= true;
75 $this->setType('radio');
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');
86 $idSuffix = $idTextStr;
90 $this->updateAttributes( array('id' => CRM_Utils_String
::munge( "CIVICRM_QFID_{$value}_{$idSuffix}" ) ) );
98 * Sets whether radio button is checked
100 * @param bool $checked Whether the field is checked or not
105 function setChecked($checked)
108 $this->removeAttribute('checked');
110 $this->updateAttributes(array('checked'=>'checked'));
112 } //end func setChecked
118 * Returns whether radio button is checked
124 function getChecked()
126 return $this->getAttribute('checked');
127 } //end func getChecked
133 * Returns the radio element in HTML
141 if (0 == strlen($this->_text
)) {
143 } elseif ($this->_flagFrozen
) {
144 $label = $this->_text
;
146 $label = '<label for="' . $this->getAttribute('id') . '">' . $this->_text
. '</label>';
148 return HTML_QuickForm_input
::toHtml() . $label;
152 // {{{ getFrozenHtml()
155 * Returns the value of field without HTML tags
161 function getFrozenHtml()
163 if ($this->getChecked()) {
164 return '<tt>(x)</tt>' .
165 $this->_getPersistantData();
167 return '<tt>( )</tt>';
169 } //end func getFrozenHtml
175 * Sets the radio text
177 * @param string $text Text to display near the radio button
182 function setText($text)
184 $this->_text
= $text;
191 * Returns the radio text
203 // {{{ onQuickFormEvent()
206 * Called by HTML_QuickForm whenever form event is made on this element
208 * @param string $event Name of event
209 * @param mixed $arg event arguments
210 * @param object &$caller calling object
215 function onQuickFormEvent($event, $arg, &$caller)
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
);
227 $value = $this->_findValue($caller->_defaultValues
);
230 if (!is_null($value) && $value == $this->getValue()) {
231 $this->setChecked(true);
233 $this->setChecked(false);
236 case 'setGroupValue':
237 if ($arg == $this->getValue()) {
238 $this->setChecked(true);
240 $this->setChecked(false);
244 parent
::onQuickFormEvent($event, $arg, $caller);
247 } // end func onQuickFormLoad
253 * Returns the value attribute if the radio is checked, null if it is not
255 function exportValue(&$submitValues, $assoc = false)
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;
263 } elseif ($value != $this->getValue()) {
266 return $this->_prepareValue($value, $assoc);
270 } //end class HTML_QuickForm_radio