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 // +----------------------------------------------------------------------+ 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 <adaniel1@eesus.jnj.com> 28 * @author Bertrand Mansion <bmansion@mamasam.com> 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 public function __construct($elementName=null, $elementLabel=null, $text='', $attributes=null) { 61 parent::__construct($elementName, $elementLabel, $attributes); 62 $this->_persistantFreeze = true; 63 $this->_text = $text; 64 $this->setType('checkbox'); 65 $this->updateAttributes(array('value'=>1)); 66 } //end constructor 67 68 /** 69 * Old syntax of class constructor. Deprecated in PHP7. 70 * 71 * @deprecated since Moodle 3.1 72 */ 73 public function HTML_QuickForm_checkbox($elementName=null, $elementLabel=null, $text='', $attributes=null) { 74 debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER); 75 self::__construct($elementName, $elementLabel, $text, $attributes); 76 } 77 78 // }}} 79 // {{{ setChecked() 80 81 /** 82 * Sets whether a checkbox is checked 83 * 84 * @param bool $checked Whether the field is checked or not 85 * @since 1.0 86 * @access public 87 * @return void 88 */ 89 function setChecked($checked) 90 { 91 if (!$checked) { 92 $this->removeAttribute('checked'); 93 } else { 94 $this->updateAttributes(array('checked'=>'checked')); 95 } 96 } //end func setChecked 97 98 // }}} 99 // {{{ getChecked() 100 101 /** 102 * Returns whether a checkbox is checked 103 * 104 * @since 1.0 105 * @access public 106 * @return bool 107 */ 108 function getChecked() 109 { 110 return (bool)$this->getAttribute('checked'); 111 } //end func getChecked 112 113 // }}} 114 // {{{ toHtml() 115 116 /** 117 * Returns the checkbox element in HTML 118 * 119 * @since 1.0 120 * @access public 121 * @return string 122 */ 123 function toHtml() 124 { 125 $this->_generateId(); // Seems to be necessary when this is used in a group. 126 if (0 == strlen($this->_text)) { 127 $label = ''; 128 } elseif ($this->_flagFrozen) { 129 $label = $this->_text; 130 } else { 131 $label = '<label for="' . $this->getAttribute('id') . '">' . $this->_text . '</label>'; 132 } 133 return HTML_QuickForm_input::toHtml() . $label; 134 } //end func toHtml 135 136 // }}} 137 // {{{ getFrozenHtml() 138 139 /** 140 * Returns the value of field without HTML tags 141 * 142 * @since 1.0 143 * @access public 144 * @return string 145 */ 146 function getFrozenHtml() 147 { 148 if ($this->getChecked()) { 149 return '<tt>[x]</tt>' . 150 $this->_getPersistantData(); 151 } else { 152 return '<tt>[ ]</tt>'; 153 } 154 } //end func getFrozenHtml 155 156 // }}} 157 // {{{ setText() 158 159 /** 160 * Sets the checkbox text 161 * 162 * @param string $text 163 * @since 1.1 164 * @access public 165 * @return void 166 */ 167 function setText($text) 168 { 169 $this->_text = $text; 170 } //end func setText 171 172 // }}} 173 // {{{ getText() 174 175 /** 176 * Returns the checkbox text 177 * 178 * @since 1.1 179 * @access public 180 * @return string 181 */ 182 function getText() 183 { 184 return $this->_text; 185 } //end func getText 186 187 // }}} 188 // {{{ setValue() 189 190 /** 191 * Sets the value of the form element 192 * 193 * @param string $value Default value of the form element 194 * @since 1.0 195 * @access public 196 * @return void 197 */ 198 function setValue($value) 199 { 200 return $this->setChecked($value); 201 } // end func setValue 202 203 // }}} 204 // {{{ getValue() 205 206 /** 207 * Returns the value of the form element 208 * 209 * @since 1.0 210 * @access public 211 * @return bool 212 */ 213 function getValue() 214 { 215 return $this->getChecked(); 216 } // end func getValue 217 218 // }}} 219 // {{{ onQuickFormEvent() 220 221 /** 222 * Called by HTML_QuickForm whenever form event is made on this element 223 * 224 * @param string $event Name of event 225 * @param mixed $arg event arguments 226 * @param object $caller calling object 227 * @since 1.0 228 * @access public 229 * @return void 230 */ 231 function onQuickFormEvent($event, $arg, &$caller) 232 { 233 switch ($event) { 234 case 'updateValue': 235 // constant values override both default and submitted ones 236 // default values are overriden by submitted 237 $value = $this->_findValue($caller->_constantValues); 238 if (null === $value) { 239 // if no boxes were checked, then there is no value in the array 240 // yet we don't want to display default value in this case 241 if ($caller->isSubmitted()) { 242 $value = $this->_findValue($caller->_submitValues); 243 } else { 244 $value = $this->_findValue($caller->_defaultValues); 245 } 246 } 247 if (null !== $value) { 248 $this->setChecked($value); 249 } 250 break; 251 case 'setGroupValue': 252 $this->setChecked($arg); 253 break; 254 default: 255 parent::onQuickFormEvent($event, $arg, $caller); 256 } 257 return true; 258 } // end func onQuickFormEvent 259 260 // }}} 261 // {{{ exportValue() 262 263 /** 264 * Return true if the checkbox is checked, null if it is not checked (getValue() returns false) 265 */ 266 function exportValue(&$submitValues, $assoc = false) 267 { 268 $value = $this->_findValue($submitValues); 269 if (null === $value) { 270 $value = $this->getChecked()? true: null; 271 } 272 return $this->_prepareValue($value, $assoc); 273 } 274 275 // }}} 276 } //end class HTML_QuickForm_checkbox 277 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body