[ 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/checkbox.php'); 23 24 /** 25 * HTML class for an advanced checkbox type field 26 * 27 * Basically this fixes a problem that HTML has had 28 * where checkboxes can only pass a single value (the 29 * value of the checkbox when checked). A value for when 30 * the checkbox is not checked cannot be passed, and 31 * furthermore the checkbox variable doesn't even exist if 32 * the checkbox was submitted unchecked. 33 * 34 * It works by prepending a hidden field with the same name and 35 * another "unchecked" value to the checbox. If the checkbox is 36 * checked, PHP overwrites the value of the hidden field with 37 * its value. 38 * 39 * @author Jason Rust <[email protected]> 40 * @since 2.0 41 * @access public 42 */ 43 class HTML_QuickForm_advcheckbox extends HTML_QuickForm_checkbox 44 { 45 // {{{ properties 46 47 /** 48 * The values passed by the hidden elment 49 * 50 * @var array 51 * @access private 52 */ 53 var $_values = null; 54 55 /** 56 * The default value 57 * 58 * @var boolean 59 * @access private 60 */ 61 var $_currentValue = null; 62 63 // }}} 64 // {{{ constructor 65 66 /** 67 * Class constructor 68 * 69 * @param string $elementName (optional)Input field name attribute 70 * @param string $elementLabel (optional)Input field label 71 * @param string $text (optional)Text to put after the checkbox 72 * @param mixed $attributes (optional)Either a typical HTML attribute string 73 * or an associative array 74 * @param mixed $values (optional)Values to pass if checked or not checked 75 * 76 * @since 1.0 77 * @access public 78 * @return void 79 */ 80 function HTML_QuickForm_advcheckbox($elementName=null, $elementLabel=null, $text=null, $attributes=null, $values=null) 81 { 82 $this->HTML_QuickForm_checkbox($elementName, $elementLabel, $text, $attributes); 83 $this->setValues($values); 84 } //end constructor 85 86 // }}} 87 // {{{ getPrivateName() 88 89 /** 90 * Gets the private name for the element 91 * 92 * @param string $elementName The element name to make private 93 * 94 * @access public 95 * @return string 96 * 97 * @deprecated Deprecated since 3.2.6, both generated elements have the same name 98 */ 99 function getPrivateName($elementName) 100 { 101 return '__'.$elementName; 102 } 103 104 // }}} 105 // {{{ getOnclickJs() 106 107 /** 108 * Create the javascript for the onclick event which will 109 * set the value of the hidden field 110 * 111 * @param string $elementName The element name 112 * 113 * @access public 114 * @return string 115 * 116 * @deprecated Deprecated since 3.2.6, this element no longer uses any javascript 117 */ 118 function getOnclickJs($elementName) 119 { 120 $onclickJs = 'if (this.checked) { this.form[\''.$elementName.'\'].value=\''.addcslashes($this->_values[1], '\'').'\'; }'; 121 $onclickJs .= 'else { this.form[\''.$elementName.'\'].value=\''.addcslashes($this->_values[0], '\'').'\'; }'; 122 return $onclickJs; 123 } 124 125 // }}} 126 // {{{ setValues() 127 128 /** 129 * Sets the values used by the hidden element 130 * 131 * @param mixed $values The values, either a string or an array 132 * 133 * @access public 134 * @return void 135 */ 136 function setValues($values) 137 { 138 if (empty($values)) { 139 // give it default checkbox behavior 140 $this->_values = array('', 1); 141 } elseif (is_scalar($values)) { 142 // if it's string, then assume the value to 143 // be passed is for when the element is checked 144 $this->_values = array('', $values); 145 } else { 146 $this->_values = $values; 147 } 148 $this->updateAttributes(array('value' => $this->_values[1])); 149 $this->setChecked($this->_currentValue == $this->_values[1]); 150 } 151 152 // }}} 153 // {{{ setValue() 154 155 /** 156 * Sets the element's value 157 * 158 * @param mixed Element's value 159 * @access public 160 */ 161 function setValue($value) 162 { 163 $this->setChecked(isset($this->_values[1]) && $value == $this->_values[1]); 164 $this->_currentValue = $value; 165 } 166 167 // }}} 168 // {{{ getValue() 169 170 /** 171 * Returns the element's value 172 * 173 * @access public 174 * @return mixed 175 */ 176 function getValue() 177 { 178 if (is_array($this->_values)) { 179 return $this->_values[$this->getChecked()? 1: 0]; 180 } else { 181 return null; 182 } 183 } 184 185 // }}} 186 // {{{ toHtml() 187 188 /** 189 * Returns the checkbox element in HTML 190 * and the additional hidden element in HTML 191 * 192 * @access public 193 * @return string 194 */ 195 function toHtml() 196 { 197 if ($this->_flagFrozen) { 198 return parent::toHtml(); 199 } else { 200 return '<input' . $this->_getAttrString(array( 201 'type' => 'hidden', 202 'name' => $this->getName(), 203 'value' => $this->_values[0] 204 )) . ' />' . parent::toHtml(); 205 206 } 207 } //end func toHtml 208 209 // }}} 210 // {{{ getFrozenHtml() 211 212 /** 213 * Unlike checkbox, this has to append a hidden input in both 214 * checked and non-checked states 215 */ 216 function getFrozenHtml() 217 { 218 return ($this->getChecked()? '<tt>[x]</tt>': '<tt>[ ]</tt>') . 219 $this->_getPersistantData(); 220 } 221 222 // }}} 223 // {{{ onQuickFormEvent() 224 225 /** 226 * Called by HTML_QuickForm whenever form event is made on this element 227 * 228 * @param string $event Name of event 229 * @param mixed $arg event arguments 230 * @param object $caller calling object 231 * @since 1.0 232 * @access public 233 * @return void 234 */ 235 function onQuickFormEvent($event, $arg, &$caller) 236 { 237 switch ($event) { 238 case 'updateValue': 239 // constant values override both default and submitted ones 240 // default values are overriden by submitted 241 $value = $this->_findValue($caller->_constantValues); 242 if (null === $value) { 243 $value = $this->_findValue($caller->_submitValues); 244 if (null === $value) { 245 $value = $this->_findValue($caller->_defaultValues); 246 } 247 } 248 if (null !== $value) { 249 $this->setValue($value); 250 } 251 break; 252 default: 253 parent::onQuickFormEvent($event, $arg, $caller); 254 } 255 return true; 256 } // end func onQuickFormLoad 257 258 // }}} 259 // {{{ exportValue() 260 261 /** 262 * This element has a value even if it is not checked, thus we override 263 * checkbox's behaviour here 264 */ 265 function exportValue(&$submitValues, $assoc = false) 266 { 267 $value = $this->_findValue($submitValues); 268 if (null === $value) { 269 $value = $this->getValue(); 270 } elseif (is_array($this->_values) && ($value != $this->_values[0]) && ($value != $this->_values[1])) { 271 $value = null; 272 } 273 return $this->_prepareValue($value, $assoc); 274 } 275 // }}} 276 } //end class HTML_QuickForm_advcheckbox 277 ?>
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 |