[ Index ] |
PHP Cross Reference of moodle-2.8 |
[Summary view] [Print] [Text view]
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 // | [email protected] so we can mail you a copy immediately. | 15 // +----------------------------------------------------------------------+ 16 // | Authors: Adam Daniel <[email protected]> | 17 // | Bertrand Mansion <[email protected]> | 18 // +----------------------------------------------------------------------+ 19 // 20 // $Id$ 21 22 require_once("HTML/QuickForm/input.php"); 23 24 /** 25 * HTML class for a checkbox type field 26 * 27 * @author Adam Daniel <[email protected]> 28 * @author Bertrand Mansion <[email protected]> 29 * @version 1.1 30 * @since PHP4.04pl1 31 * @access public 32 */ 33 class HTML_QuickForm_checkbox extends HTML_QuickForm_input 34 { 35 // {{{ properties 36 37 /** 38 * Checkbox display text 39 * @var string 40 * @since 1.1 41 * @access private 42 */ 43 var $_text = ''; 44 45 // }}} 46 // {{{ constructor 47 48 /** 49 * Class constructor 50 * 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 59 */ 60 function HTML_QuickForm_checkbox($elementName=null, $elementLabel=null, $text='', $attributes=null) 61 { 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 } //end constructor 68 69 // }}} 70 // {{{ setChecked() 71 72 /** 73 * Sets whether a checkbox is checked 74 * 75 * @param bool $checked Whether the field is checked or not 76 * @since 1.0 77 * @access public 78 * @return void 79 */ 80 function setChecked($checked) 81 { 82 if (!$checked) { 83 $this->removeAttribute('checked'); 84 } else { 85 $this->updateAttributes(array('checked'=>'checked')); 86 } 87 } //end func setChecked 88 89 // }}} 90 // {{{ getChecked() 91 92 /** 93 * Returns whether a checkbox is checked 94 * 95 * @since 1.0 96 * @access public 97 * @return bool 98 */ 99 function getChecked() 100 { 101 return (bool)$this->getAttribute('checked'); 102 } //end func getChecked 103 104 // }}} 105 // {{{ toHtml() 106 107 /** 108 * Returns the checkbox element in HTML 109 * 110 * @since 1.0 111 * @access public 112 * @return string 113 */ 114 function toHtml() 115 { 116 $this->_generateId(); // Seems to be necessary when this is used in a group. 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>'; 123 } 124 return HTML_QuickForm_input::toHtml() . $label; 125 } //end func toHtml 126 127 // }}} 128 // {{{ getFrozenHtml() 129 130 /** 131 * Returns the value of field without HTML tags 132 * 133 * @since 1.0 134 * @access public 135 * @return string 136 */ 137 function getFrozenHtml() 138 { 139 if ($this->getChecked()) { 140 return '<tt>[x]</tt>' . 141 $this->_getPersistantData(); 142 } else { 143 return '<tt>[ ]</tt>'; 144 } 145 } //end func getFrozenHtml 146 147 // }}} 148 // {{{ setText() 149 150 /** 151 * Sets the checkbox text 152 * 153 * @param string $text 154 * @since 1.1 155 * @access public 156 * @return void 157 */ 158 function setText($text) 159 { 160 $this->_text = $text; 161 } //end func setText 162 163 // }}} 164 // {{{ getText() 165 166 /** 167 * Returns the checkbox text 168 * 169 * @since 1.1 170 * @access public 171 * @return string 172 */ 173 function getText() 174 { 175 return $this->_text; 176 } //end func getText 177 178 // }}} 179 // {{{ setValue() 180 181 /** 182 * Sets the value of the form element 183 * 184 * @param string $value Default value of the form element 185 * @since 1.0 186 * @access public 187 * @return void 188 */ 189 function setValue($value) 190 { 191 return $this->setChecked($value); 192 } // end func setValue 193 194 // }}} 195 // {{{ getValue() 196 197 /** 198 * Returns the value of the form element 199 * 200 * @since 1.0 201 * @access public 202 * @return bool 203 */ 204 function getValue() 205 { 206 return $this->getChecked(); 207 } // end func getValue 208 209 // }}} 210 // {{{ onQuickFormEvent() 211 212 /** 213 * Called by HTML_QuickForm whenever form event is made on this element 214 * 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 221 */ 222 function onQuickFormEvent($event, $arg, &$caller) 223 { 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); 236 } 237 } 238 if (null !== $value) { 239 $this->setChecked($value); 240 } 241 break; 242 case 'setGroupValue': 243 $this->setChecked($arg); 244 break; 245 default: 246 parent::onQuickFormEvent($event, $arg, $caller); 247 } 248 return true; 249 } // end func onQuickFormEvent 250 251 // }}} 252 // {{{ exportValue() 253 254 /** 255 * Return true if the checkbox is checked, null if it is not checked (getValue() returns false) 256 */ 257 function exportValue(&$submitValues, $assoc = false) 258 { 259 $value = $this->_findValue($submitValues); 260 if (null === $value) { 261 $value = $this->getChecked()? true: null; 262 } 263 return $this->_prepareValue($value, $assoc); 264 } 265 266 // }}} 267 } //end class HTML_QuickForm_checkbox 268 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:29:05 2014 | Cross-referenced by PHPXref 0.7.1 |