| [ Index ] |
|
Code source de XOOPS 2.0.17.1 |
1 <?php 2 // $Id: form.php 984 2007-08-11 16:31:54Z phppp $ 3 // ------------------------------------------------------------------------ // 4 // XOOPS - PHP Content Management System // 5 // Copyright (c) 2000 XOOPS.org // 6 // <http://www.xoops.org/> // 7 // ------------------------------------------------------------------------ // 8 // This program is free software; you can redistribute it and/or modify // 9 // it under the terms of the GNU General Public License as published by // 10 // the Free Software Foundation; either version 2 of the License, or // 11 // (at your option) any later version. // 12 // // 13 // You may not change or alter any portion of this comment or credits // 14 // of supporting developers from this source code or any supporting // 15 // source code which is considered copyrighted (c) material of the // 16 // original comment or credit authors. // 17 // // 18 // This program is distributed in the hope that it will be useful, // 19 // but WITHOUT ANY WARRANTY; without even the implied warranty of // 20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // 21 // GNU General Public License for more details. // 22 // // 23 // You should have received a copy of the GNU General Public License // 24 // along with this program; if not, write to the Free Software // 25 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // 26 // ------------------------------------------------------------------------ // 27 // Author: Kazumi Ono (AKA onokazu) // 28 // URL: http://www.myweb.ne.jp/, http://www.xoops.org/, http://jp.xoops.org/ // 29 // Project: The XOOPS Project // 30 // ------------------------------------------------------------------------- // 31 // public abstruct 32 /** 33 * 34 * 35 * @package kernel 36 * @subpackage form 37 * 38 * @author Kazumi Ono <onokazu@xoops.org> 39 * @copyright copyright (c) 2000-2003 XOOPS.org 40 */ 41 42 43 /** 44 * Abstract base class for forms 45 * 46 * @author Kazumi Ono <onokazu@xoops.org> 47 * @copyright copyright (c) 2000-2003 XOOPS.org 48 * 49 * @package kernel 50 * @subpackage form 51 */ 52 class XoopsForm { 53 /**#@+ 54 * @access private 55 */ 56 /** 57 * "action" attribute for the html form 58 * @var string 59 */ 60 var $_action; 61 62 /** 63 * "method" attribute for the form. 64 * @var string 65 */ 66 var $_method; 67 68 /** 69 * "name" attribute of the form 70 * @var string 71 */ 72 var $_name; 73 74 /** 75 * title for the form 76 * @var string 77 */ 78 var $_title; 79 80 /** 81 * array of {@link XoopsFormElement} objects 82 * @var array 83 */ 84 var $_elements = array(); 85 86 /** 87 * extra information for the <form> tag 88 * @var string 89 */ 90 var $_extra; 91 92 /** 93 * required elements 94 * @var array 95 */ 96 var $_required = array(); 97 98 /**#@-*/ 99 100 /** 101 * constructor 102 * 103 * @param string $title title of the form 104 * @param string $name "name" attribute for the <form> tag 105 * @param string $action "action" attribute for the <form> tag 106 * @param string $method "method" attribute for the <form> tag 107 * @param bool $addtoken whether to add a security token to the form 108 */ 109 function XoopsForm($title, $name, $action, $method="post", $addtoken=false){ 110 $this->_title = $title; 111 $this->_name = $name; 112 $this->_action = $action; 113 $this->_method = $method; 114 if ($addtoken != false) { 115 $this->addElement(new XoopsFormHiddenToken()); 116 } 117 } 118 119 /** 120 * return the title of the form 121 * 122 * @return string 123 */ 124 function getTitle(){ 125 return $this->_title; 126 } 127 128 /** 129 * get the "name" attribute for the <form> tag 130 * 131 * @return string 132 */ 133 function getName(){ 134 return $this->_name; 135 } 136 137 /** 138 * get the "action" attribute for the <form> tag 139 * 140 * @return string 141 */ 142 function getAction(){ 143 return $this->_action; 144 } 145 146 /** 147 * get the "method" attribute for the <form> tag 148 * 149 * @return string 150 */ 151 function getMethod(){ 152 return $this->_method; 153 } 154 155 /** 156 * Add an element to the form 157 * 158 * @param object &$formElement reference to a {@link XoopsFormElement} 159 * @param bool $required is this a "required" element? 160 */ 161 function addElement(&$formElement, $required = false){ 162 if ( is_string( $formElement ) ) { 163 $this->_elements[] = $formElement; 164 } elseif ( is_subclass_of($formElement, 'xoopsformelement') ) { 165 $this->_elements[] =& $formElement; 166 if (!$formElement->isContainer()) { 167 if ($required) { 168 $formElement->_required = true; 169 $this->_required[] =& $formElement; 170 } 171 } else { 172 $required_elements =& $formElement->getRequired(); 173 $count = count($required_elements); 174 for ($i = 0 ; $i < $count; $i++) { 175 $this->_required[] =& $required_elements[$i]; 176 } 177 } 178 } 179 } 180 181 /** 182 * get an array of forms elements 183 * 184 * @param bool get elements recursively? 185 * @return array array of {@link XoopsFormElement}s 186 */ 187 function &getElements($recurse = false){ 188 if (!$recurse) { 189 return $this->_elements; 190 } else { 191 $ret = array(); 192 $count = count($this->_elements); 193 for ($i = 0; $i < $count; $i++) { 194 if ( is_object( $this->_elements[$i] ) ) { 195 if (!$this->_elements[$i]->isContainer()) { 196 $ret[] =& $this->_elements[$i]; 197 } else { 198 $elements =& $this->_elements[$i]->getElements(true); 199 $count2 = count($elements); 200 for ($j = 0; $j < $count2; $j++) { 201 $ret[] =& $elements[$j]; 202 } 203 unset($elements); 204 } 205 } 206 } 207 return $ret; 208 } 209 } 210 211 /** 212 * get an array of "name" attributes of form elements 213 * 214 * @return array array of form element names 215 */ 216 function getElementNames() 217 { 218 $ret = array(); 219 $elements =& $this->getElements(true); 220 $count = count($elements); 221 for ($i = 0; $i < $count; $i++) { 222 $ret[] = $elements[$i]->getName(); 223 } 224 return $ret; 225 } 226 227 /** 228 * get a reference to a {@link XoopsFormElement} object by its "name" 229 * 230 * @param string $name "name" attribute assigned to a {@link XoopsFormElement} 231 * @return object reference to a {@link XoopsFormElement}, false if not found 232 */ 233 function &getElementByName($name){ 234 $elements = $this->getElements(true); 235 $count = count($elements); 236 for ($i = 0; $i < $count; $i++) { 237 if ($name == $elements[$i]->getName()) { 238 return $elements[$i]; 239 } 240 } 241 $elt = false; 242 return $elt; 243 } 244 245 /** 246 * Sets the "value" attribute of a form element 247 * 248 * @param string $name the "name" attribute of a form element 249 * @param string $value the "value" attribute of a form element 250 */ 251 function setElementValue($name, $value){ 252 $ele =& $this->getElementByName($name); 253 if (is_object($ele) && method_exists($ele, 'setValue')) { 254 $ele->setValue($value); 255 } 256 } 257 258 /** 259 * Sets the "value" attribute of form elements in a batch 260 * 261 * @param array $values array of name/value pairs to be assigned to form elements 262 */ 263 function setElementValues($values){ 264 if (is_array($values) && !empty($values)) { 265 // will not use getElementByName() for performance.. 266 $elements =& $this->getElements(true); 267 $count = count($elements); 268 for ($i = 0; $i < $count; $i++) { 269 $name = $elements[$i]->getName(); 270 if ($name && isset($values[$name]) && method_exists($elements[$i], 'setValue')) { 271 $elements[$i]->setValue($values[$name]); 272 } 273 } 274 } 275 } 276 277 /** 278 * Gets the "value" attribute of a form element 279 * 280 * @param string $name the "name" attribute of a form element 281 * @return string the "value" attribute assigned to a form element, null if not set 282 */ 283 function getElementValue($name){ 284 $ele =& $this->getElementByName($name); 285 if (is_object($ele) && method_exists($ele, 'getValue')) { 286 return $ele->getValue($value); 287 } 288 return; 289 } 290 291 /** 292 * gets the "value" attribute of all form elements 293 * 294 * @return array array of name/value pairs assigned to form elements 295 */ 296 function getElementValues(){ 297 // will not use getElementByName() for performance.. 298 $elements =& $this->getElements(true); 299 $count = count($elements); 300 $values = array(); 301 for ($i = 0; $i < $count; $i++) { 302 $name = $elements[$i]->getName(); 303 if ($name && method_exists($elements[$i], 'getValue')) { 304 $values[$name] =& $elements[$i]->getValue(); 305 } 306 } 307 return $values; 308 } 309 310 /** 311 * set the extra attributes for the <form> tag 312 * 313 * @param string $extra extra attributes for the <form> tag 314 */ 315 function setExtra($extra){ 316 $this->_extra = " ".$extra; 317 } 318 319 /** 320 * get the extra attributes for the <form> tag 321 * 322 * @return string 323 */ 324 function &getExtra(){ 325 if (isset($this->_extra)) { 326 return $this->_extra; 327 } 328 $extra = null; 329 return $extra; 330 } 331 332 /** 333 * make an element "required" 334 * 335 * @param object &$formElement reference to a {@link XoopsFormElement} 336 */ 337 function setRequired(&$formElement){ 338 $this->_required[] =& $formElement; 339 } 340 341 /** 342 * get an array of "required" form elements 343 * 344 * @return array array of {@link XoopsFormElement}s 345 */ 346 function &getRequired(){ 347 return $this->_required; 348 } 349 350 /** 351 * insert a break in the form 352 * 353 * This method is abstract. It must be overwritten in the child classes. 354 * 355 * @param string $extra extra information for the break 356 * @abstract 357 */ 358 function insertBreak($extra = null){ 359 } 360 361 /** 362 * returns renderered form 363 * 364 * This method is abstract. It must be overwritten in the child classes. 365 * 366 * @abstract 367 */ 368 function render(){ 369 } 370 371 /** 372 * displays rendered form 373 */ 374 function display(){ 375 echo $this->render(); 376 } 377 378 /** 379 * Renders the Javascript function needed for client-side for validation 380 * 381 * Form elements that have been declared "required" and not set will prevent the form from being 382 * submitted. Additionally, each element class may provide its own "renderValidationJS" method 383 * that is supposed to return custom validation code for the element. 384 * 385 * The element validation code can assume that the JS "myform" variable points to the form, and must 386 * execute <i>return false</i> if validation fails. 387 * 388 * A basic element validation method may contain something like this: 389 * <code> 390 * function renderValidationJS() { 391 * $name = $this->getName(); 392 * return "if ( myform.{$name}.value != 'valid' ) { " . 393 * "myform.{$name}.focus(); window.alert( '$name is invalid' ); return false;" . 394 * " }"; 395 * } 396 * </code> 397 * 398 * @param boolean $withtags Include the < javascript > tags in the returned string 399 */ 400 function renderValidationJS( $withtags = true ) { 401 $js = ""; 402 if ( $withtags ) { 403 $js .= "\n<!-- Start Form Validation JavaScript //-->\n<script type='text/javascript'>\n<!--//\n"; 404 } 405 $myts =& MyTextSanitizer::getInstance(); 406 $formname = $this->getName(); 407 $js .= "function xoopsFormValidate_{$formname}() { myform = window.document.{$formname}; "; 408 $elements = $this->getElements( true ); 409 foreach ( $elements as $elt ) { 410 if ( method_exists( $elt, 'renderValidationJS' ) ) { 411 $js .= $elt->renderValidationJS(); 412 } 413 } 414 $js .= "return true;\n}\n"; 415 if ( $withtags ) { 416 $js .= "//--></script>\n<!-- End Form Vaidation JavaScript //-->\n"; 417 } 418 return $js; 419 } 420 /** 421 * assign to smarty form template instead of displaying directly 422 * 423 * @param object &$tpl reference to a {@link Smarty} object 424 * @see Smarty 425 */ 426 function assign(&$tpl){ 427 $i = -1; 428 $elements = array(); 429 foreach ( $this->getElements() as $ele ) { 430 ++$i; 431 if(is_string( $ele )) { 432 $elements[$i]['body'] = $ele; 433 continue; 434 } 435 $n = ($ele->getName() != "") ? $ele->getName() : $i; 436 $elements[$n]['name'] = $ele->getName(); 437 $elements[$n]['caption'] = $ele->getCaption(); 438 $elements[$n]['body'] = $ele->render(); 439 $elements[$n]['hidden'] = $ele->isHidden(); 440 $elements[$n]['required'] = $ele->isRequired(); 441 if ($ele->getDescription() != '') { 442 $elements[$n]['description'] = $ele->getDescription(); 443 } 444 } 445 $js = $this->renderValidationJS(); 446 $tpl->assign($this->getName(), array('title' => $this->getTitle(), 'name' => $this->getName(), 'action' => $this->getAction(), 'method' => $this->getMethod(), 'extra' => 'onsubmit="return xoopsFormValidate_'.$this->getName().'();"'.$this->getExtra(), 'javascript' => $js, 'elements' => $elements)); 447 } 448 } 449 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Sun Nov 25 11:44:32 2007 | par Balluche grâce à PHPXref 0.7 |
|