adding some strings
[moodle-linuxchix.git] / lib / pear / HTML / QuickForm / radio.php
blob963168b06ac14a1c6e49448a40e9b2bccdb3ce97
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4: */
3 // +----------------------------------------------------------------------+
4 // | PHP version 4.0 |
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 // +----------------------------------------------------------------------+
20 // $Id$
22 require_once('HTML/QuickForm/input.php');
24 /**
25 * HTML class for a radio type element
27 * @author Adam Daniel <adaniel1@eesus.jnj.com>
28 * @author Bertrand Mansion <bmansion@mamasam.com>
29 * @version 1.1
30 * @since PHP4.04pl1
31 * @access public
33 class HTML_QuickForm_radio extends HTML_QuickForm_input
35 // {{{ properties
37 /**
38 * Radio display text
39 * @var string
40 * @since 1.1
41 * @access private
43 var $_text = '';
45 // }}}
46 // {{{ constructor
48 /**
49 * Class constructor
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
56 * @since 1.0
57 * @access public
58 * @return void
60 function HTML_QuickForm_radio($elementName=null, $elementLabel=null, $text=null, $value=null, $attributes=null)
62 $this->HTML_QuickForm_element($elementName, $elementLabel, $attributes);
63 if (isset($value)) {
64 $this->setValue($value);
66 $this->_persistantFreeze = true;
67 $this->setType('radio');
68 $this->_text = $text;
69 $this->_generateId();
70 } //end constructor
72 // }}}
73 // {{{ setChecked()
75 /**
76 * Sets whether radio button is checked
78 * @param bool $checked Whether the field is checked or not
79 * @since 1.0
80 * @access public
81 * @return void
83 function setChecked($checked)
85 if (!$checked) {
86 $this->removeAttribute('checked');
87 } else {
88 $this->updateAttributes(array('checked'=>'checked'));
90 } //end func setChecked
92 // }}}
93 // {{{ getChecked()
95 /**
96 * Returns whether radio button is checked
98 * @since 1.0
99 * @access public
100 * @return string
102 function getChecked()
104 return $this->getAttribute('checked');
105 } //end func getChecked
107 // }}}
108 // {{{ toHtml()
111 * Returns the radio element in HTML
113 * @since 1.0
114 * @access public
115 * @return string
117 function toHtml()
119 if (0 == strlen($this->_text)) {
120 $label = '';
121 } elseif ($this->_flagFrozen) {
122 $label = $this->_text;
123 } else {
124 $label = '<label for="' . $this->getAttribute('id') . '">' . $this->_text . '</label>';
126 return HTML_QuickForm_input::toHtml() . $label;
127 } //end func toHtml
129 // }}}
130 // {{{ getFrozenHtml()
133 * Returns the value of field without HTML tags
135 * @since 1.0
136 * @access public
137 * @return string
139 function getFrozenHtml()
141 if ($this->getChecked()) {
142 return '<tt>(x)</tt>' .
143 $this->_getPersistantData();
144 } else {
145 return '<tt>( )</tt>';
147 } //end func getFrozenHtml
149 // }}}
150 // {{{ setText()
153 * Sets the radio text
155 * @param string $text Text to display near the radio button
156 * @since 1.1
157 * @access public
158 * @return void
160 function setText($text)
162 $this->_text = $text;
163 } //end func setText
165 // }}}
166 // {{{ getText()
169 * Returns the radio text
171 * @since 1.1
172 * @access public
173 * @return string
175 function getText()
177 return $this->_text;
178 } //end func getText
180 // }}}
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
189 * @since 1.0
190 * @access public
191 * @return void
193 function onQuickFormEvent($event, $arg, &$caller)
195 switch ($event) {
196 case 'updateValue':
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);
208 } else {
209 $this->setChecked(false);
211 break;
212 case 'setGroupValue':
213 if ($arg == $this->getValue()) {
214 $this->setChecked(true);
215 } else {
216 $this->setChecked(false);
218 break;
219 default:
220 parent::onQuickFormEvent($event, $arg, $caller);
222 return true;
223 } // end func onQuickFormLoad
225 // }}}
226 // {{{ exportValue()
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()) {
237 $value = null;
239 return $this->_prepareValue($value, $assoc);
242 // }}}
243 } //end class HTML_QuickForm_radio