adding some strings
[moodle-linuxchix.git] / lib / pear / HTML / QuickForm / checkbox.php
blob55c7574c57e6dea5dd9c83babf49fd455a2a642e
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 checkbox type field
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_checkbox extends HTML_QuickForm_input
35 // {{{ properties
37 /**
38 * Checkbox 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 $elementName (optional)Input field name attribute
52 * @param string $elementLabel (optional)Input field value
53 * @param string $text (optional)Checkbox display text
54 * @param mixed $attributes (optional)Either a typical HTML attribute string
55 * or an associative array
56 * @since 1.0
57 * @access public
58 * @return void
60 function HTML_QuickForm_checkbox($elementName=null, $elementLabel=null, $text='', $attributes=null)
62 HTML_QuickForm_input::HTML_QuickForm_input($elementName, $elementLabel, $attributes);
63 $this->_persistantFreeze = true;
64 $this->_text = $text;
65 $this->setType('checkbox');
66 $this->updateAttributes(array('value'=>1));
67 $this->_generateId();
68 } //end constructor
70 // }}}
71 // {{{ setChecked()
73 /**
74 * Sets whether a checkbox is checked
76 * @param bool $checked Whether the field is checked or not
77 * @since 1.0
78 * @access public
79 * @return void
81 function setChecked($checked)
83 if (!$checked) {
84 $this->removeAttribute('checked');
85 } else {
86 $this->updateAttributes(array('checked'=>'checked'));
88 } //end func setChecked
90 // }}}
91 // {{{ getChecked()
93 /**
94 * Returns whether a checkbox is checked
96 * @since 1.0
97 * @access public
98 * @return bool
100 function getChecked()
102 return (bool)$this->getAttribute('checked');
103 } //end func getChecked
105 // }}}
106 // {{{ toHtml()
109 * Returns the checkbox element in HTML
111 * @since 1.0
112 * @access public
113 * @return string
115 function toHtml()
117 if (0 == strlen($this->_text)) {
118 $label = '';
119 } elseif ($this->_flagFrozen) {
120 $label = $this->_text;
121 } else {
122 $label = '<label for="' . $this->getAttribute('id') . '">' . $this->_text . '</label>';
124 return HTML_QuickForm_input::toHtml() . $label;
125 } //end func toHtml
127 // }}}
128 // {{{ getFrozenHtml()
131 * Returns the value of field without HTML tags
133 * @since 1.0
134 * @access public
135 * @return string
137 function getFrozenHtml()
139 if ($this->getChecked()) {
140 return '<tt>[x]</tt>' .
141 $this->_getPersistantData();
142 } else {
143 return '<tt>[ ]</tt>';
145 } //end func getFrozenHtml
147 // }}}
148 // {{{ setText()
151 * Sets the checkbox text
153 * @param string $text
154 * @since 1.1
155 * @access public
156 * @return void
158 function setText($text)
160 $this->_text = $text;
161 } //end func setText
163 // }}}
164 // {{{ getText()
167 * Returns the checkbox text
169 * @since 1.1
170 * @access public
171 * @return string
173 function getText()
175 return $this->_text;
176 } //end func getText
178 // }}}
179 // {{{ setValue()
182 * Sets the value of the form element
184 * @param string $value Default value of the form element
185 * @since 1.0
186 * @access public
187 * @return void
189 function setValue($value)
191 return $this->setChecked($value);
192 } // end func setValue
194 // }}}
195 // {{{ getValue()
198 * Returns the value of the form element
200 * @since 1.0
201 * @access public
202 * @return bool
204 function getValue()
206 return $this->getChecked();
207 } // end func getValue
209 // }}}
210 // {{{ onQuickFormEvent()
213 * Called by HTML_QuickForm whenever form event is made on this element
215 * @param string $event Name of event
216 * @param mixed $arg event arguments
217 * @param object $caller calling object
218 * @since 1.0
219 * @access public
220 * @return void
222 function onQuickFormEvent($event, $arg, &$caller)
224 switch ($event) {
225 case 'updateValue':
226 // constant values override both default and submitted ones
227 // default values are overriden by submitted
228 $value = $this->_findValue($caller->_constantValues);
229 if (null === $value) {
230 // if no boxes were checked, then there is no value in the array
231 // yet we don't want to display default value in this case
232 if ($caller->isSubmitted()) {
233 $value = $this->_findValue($caller->_submitValues);
234 } else {
235 $value = $this->_findValue($caller->_defaultValues);
238 if (null !== $value) {
239 $this->setChecked($value);
241 break;
242 case 'setGroupValue':
243 $this->setChecked($arg);
244 break;
245 default:
246 parent::onQuickFormEvent($event, $arg, $caller);
248 return true;
249 } // end func onQuickFormEvent
251 // }}}
252 // {{{ exportValue()
255 * Return true if the checkbox is checked, null if it is not checked (getValue() returns false)
257 function exportValue(&$submitValues, $assoc = false)
259 $value = $this->_findValue($submitValues);
260 if (null === $value) {
261 $value = $this->getChecked()? true: null;
263 return $this->_prepareValue($value, $assoc);
266 // }}}
267 } //end class HTML_QuickForm_checkbox