2 /* vim: set expandtab tabstop=4 shiftwidth=4: */
3 // +----------------------------------------------------------------------+
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 // +----------------------------------------------------------------------+
22 require_once('HTML/QuickForm/input.php');
25 * HTML class for a radio type element
27 * @author Adam Daniel <adaniel1@eesus.jnj.com>
28 * @author Bertrand Mansion <bmansion@mamasam.com>
33 class HTML_QuickForm_radio
extends HTML_QuickForm_input
51 * @param string Input field name attribute
52 * @param mixed Label(s) for a field
53 * @param string Text to display near the radio
54 * @param string Input field value
55 * @param mixed Either a typical HTML attribute string or an associative array
60 function HTML_QuickForm_radio($elementName=null, $elementLabel=null, $text=null, $value=null, $attributes=null)
62 $this->HTML_QuickForm_element($elementName, $elementLabel, $attributes);
64 $this->setValue($value);
66 $this->_persistantFreeze
= true;
67 $this->setType('radio');
76 * Sets whether radio button is checked
78 * @param bool $checked Whether the field is checked or not
83 function setChecked($checked)
86 $this->removeAttribute('checked');
88 $this->updateAttributes(array('checked'=>'checked'));
90 } //end func setChecked
96 * Returns whether radio button is checked
102 function getChecked()
104 return $this->getAttribute('checked');
105 } //end func getChecked
111 * Returns the radio element in HTML
119 if (0 == strlen($this->_text
)) {
121 } elseif ($this->_flagFrozen
) {
122 $label = $this->_text
;
124 $label = '<label for="' . $this->getAttribute('id') . '">' . $this->_text
. '</label>';
126 return HTML_QuickForm_input
::toHtml() . $label;
130 // {{{ getFrozenHtml()
133 * Returns the value of field without HTML tags
139 function getFrozenHtml()
141 if ($this->getChecked()) {
142 return '<tt>(x)</tt>' .
143 $this->_getPersistantData();
145 return '<tt>( )</tt>';
147 } //end func getFrozenHtml
153 * Sets the radio text
155 * @param string $text Text to display near the radio button
160 function setText($text)
162 $this->_text
= $text;
169 * Returns the radio text
181 // {{{ onQuickFormEvent()
184 * Called by HTML_QuickForm whenever form event is made on this element
186 * @param string $event Name of event
187 * @param mixed $arg event arguments
188 * @param object $caller calling object
193 function onQuickFormEvent($event, $arg, &$caller)
197 // constant values override both default and submitted ones
198 // default values are overriden by submitted
199 $value = $this->_findValue($caller->_constantValues
);
200 if (null === $value) {
201 $value = $this->_findValue($caller->_submitValues
);
202 if (null === $value) {
203 $value = $this->_findValue($caller->_defaultValues
);
206 if ($value == $this->getValue()) {
207 $this->setChecked(true);
209 $this->setChecked(false);
212 case 'setGroupValue':
213 if ($arg == $this->getValue()) {
214 $this->setChecked(true);
216 $this->setChecked(false);
220 parent
::onQuickFormEvent($event, $arg, $caller);
223 } // end func onQuickFormLoad
229 * Returns the value attribute if the radio is checked, null if it is not
231 function exportValue(&$submitValues, $assoc = false)
233 $value = $this->_findValue($submitValues);
234 if (null === $value) {
235 $value = $this->getChecked()?
$this->getValue(): null;
236 } elseif ($value != $this->getValue()) {
239 return $this->_prepareValue($value, $assoc);
243 } //end class HTML_QuickForm_radio