| 1 | <?php |
|---|
| 2 | // $Id: form.php,v 1.5 2006/05/01 02:37:26 onokazu Exp $ |
|---|
| 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 <[email protected]> |
|---|
| 39 | * @copyright copyright (c) 2000-2003 XOOPS.org |
|---|
| 40 | */ |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | /** |
|---|
| 44 | * Abstract base class for forms |
|---|
| 45 | * |
|---|
| 46 | * @author Kazumi Ono <[email protected]> |
|---|
| 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 ($required) { |
|---|
| 167 | if (!$formElement->isContainer()) { |
|---|
| 168 | $this->_required[] =& $formElement; |
|---|
| 169 | } else { |
|---|
| 170 | $required_elements =& $formElement->getRequired(); |
|---|
| 171 | $count = count($required_elements); |
|---|
| 172 | for ($i = 0 ; $i < $count; $i++) { |
|---|
| 173 | $this->_required[] =& $required_elements[$i]; |
|---|
| 174 | } |
|---|
| 175 | } |
|---|
| 176 | } |
|---|
| 177 | } |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | /** |
|---|
| 181 | * get an array of forms elements |
|---|
| 182 | * |
|---|
| 183 | * @param bool get elements recursively? |
|---|
| 184 | * @return array array of {@link XoopsFormElement}s |
|---|
| 185 | */ |
|---|
| 186 | function &getElements($recurse = false){ |
|---|
| 187 | if (!$recurse) { |
|---|
| 188 | return $this->_elements; |
|---|
| 189 | } else { |
|---|
| 190 | $ret = array(); |
|---|
| 191 | $count = count($this->_elements); |
|---|
| 192 | for ($i = 0; $i < $count; $i++) { |
|---|
| 193 | if (!$this->_elements[$i]->isContainer()) { |
|---|
| 194 | $ret[] =& $this->_elements[$i]; |
|---|
| 195 | } else { |
|---|
| 196 | $elements =& $this->_elements[$i]->getElements(true); |
|---|
| 197 | $count2 = count($elements); |
|---|
| 198 | for ($j = 0; $j < $count2; $j++) { |
|---|
| 199 | $ret[] =& $elements[$j]; |
|---|
| 200 | } |
|---|
| 201 | unset($elements); |
|---|
| 202 | } |
|---|
| 203 | } |
|---|
| 204 | return $ret; |
|---|
| 205 | } |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | /** |
|---|
| 209 | * get an array of "name" attributes of form elements |
|---|
| 210 | * |
|---|
| 211 | * @return array array of form element names |
|---|
| 212 | */ |
|---|
| 213 | function getElementNames() |
|---|
| 214 | { |
|---|
| 215 | $ret = array(); |
|---|
| 216 | $elements =& $this->getElements(true); |
|---|
| 217 | $count = count($elements); |
|---|
| 218 | for ($i = 0; $i < $count; $i++) { |
|---|
| 219 | $ret[] = $elements[$i]->getName(); |
|---|
| 220 | } |
|---|
| 221 | return $ret; |
|---|
| 222 | } |
|---|
| 223 | |
|---|
| 224 | /** |
|---|
| 225 | * get a reference to a {@link XoopsFormElement} object by its "name" |
|---|
| 226 | * |
|---|
| 227 | * @param string $name "name" attribute assigned to a {@link XoopsFormElement} |
|---|
| 228 | * @return object reference to a {@link XoopsFormElement}, false if not found |
|---|
| 229 | */ |
|---|
| 230 | function &getElementByName($name){ |
|---|
| 231 | $elements =& $this->getElements(true); |
|---|
| 232 | $count = count($elements); |
|---|
| 233 | for ($i = 0; $i < $count; $i++) { |
|---|
| 234 | if ($name == $elements[$i]->getName()) { |
|---|
| 235 | return $elements[$i]; |
|---|
| 236 | } |
|---|
| 237 | } |
|---|
| 238 | return false; |
|---|
| 239 | } |
|---|
| 240 | |
|---|
| 241 | /** |
|---|
| 242 | * Sets the "value" attribute of a form element |
|---|
| 243 | * |
|---|
| 244 | * @param string $name the "name" attribute of a form element |
|---|
| 245 | * @param string $value the "value" attribute of a form element |
|---|
| 246 | */ |
|---|
| 247 | function setElementValue($name, $value){ |
|---|
| 248 | $ele =& $this->getElementByName($name); |
|---|
| 249 | if (is_object($ele) && method_exists($ele, 'setValue')) { |
|---|
| 250 | $ele->setValue($value); |
|---|
| 251 | } |
|---|
| 252 | } |
|---|
| 253 | |
|---|
| 254 | /** |
|---|
| 255 | * Sets the "value" attribute of form elements in a batch |
|---|
| 256 | * |
|---|
| 257 | * @param array $values array of name/value pairs to be assigned to form elements |
|---|
| 258 | */ |
|---|
| 259 | function setElementValues($values){ |
|---|
| 260 | if (is_array($values) && !empty($values)) { |
|---|
| 261 | // will not use getElementByName() for performance.. |
|---|
| 262 | $elements =& $this->getElements(true); |
|---|
| 263 | $count = count($elements); |
|---|
| 264 | for ($i = 0; $i < $count; $i++) { |
|---|
| 265 | $name = $elements[$i]->getName(); |
|---|
| 266 | if ($name && isset($values[$name]) && method_exists($elements[$i], 'setValue')) { |
|---|
| 267 | $elements[$i]->setValue($values[$name]); |
|---|
| 268 | } |
|---|
| 269 | } |
|---|
| 270 | } |
|---|
| 271 | } |
|---|
| 272 | |
|---|
| 273 | /** |
|---|
| 274 | * Gets the "value" attribute of a form element |
|---|
| 275 | * |
|---|
| 276 | * @param string $name the "name" attribute of a form element |
|---|
| 277 | * @return string the "value" attribute assigned to a form element, null if not set |
|---|
| 278 | */ |
|---|
| 279 | function &getElementValue($name){ |
|---|
| 280 | $ele =& $this->getElementByName($name); |
|---|
| 281 | if (is_object($ele) && method_exists($ele, 'getValue')) { |
|---|
| 282 | return $ele->getValue($value); |
|---|
| 283 | } |
|---|
| 284 | return; |
|---|
| 285 | } |
|---|
| 286 | |
|---|
| 287 | /** |
|---|
| 288 | * gets the "value" attribute of all form elements |
|---|
| 289 | * |
|---|
| 290 | * @return array array of name/value pairs assigned to form elements |
|---|
| 291 | */ |
|---|
| 292 | function &getElementValues(){ |
|---|
| 293 | // will not use getElementByName() for performance.. |
|---|
| 294 | $elements =& $this->getElements(true); |
|---|
| 295 | $count = count($elements); |
|---|
| 296 | $values = array(); |
|---|
| 297 | for ($i = 0; $i < $count; $i++) { |
|---|
| 298 | $name = $elements[$i]->getName(); |
|---|
| 299 | if ($name && method_exists($elements[$i], 'getValue')) { |
|---|
| 300 | $values[$name] =& $elements[$i]->getValue(); |
|---|
| 301 | } |
|---|
| 302 | } |
|---|
| 303 | return $values; |
|---|
| 304 | } |
|---|
| 305 | |
|---|
| 306 | /** |
|---|
| 307 | * set the extra attributes for the <form> tag |
|---|
| 308 | * |
|---|
| 309 | * @param string $extra extra attributes for the <form> tag |
|---|
| 310 | */ |
|---|
| 311 | function setExtra($extra){ |
|---|
| 312 | $this->_extra = " ".$extra; |
|---|
| 313 | } |
|---|
| 314 | |
|---|
| 315 | /** |
|---|
| 316 | * get the extra attributes for the <form> tag |
|---|
| 317 | * |
|---|
| 318 | * @return string |
|---|
| 319 | */ |
|---|
| 320 | function &getExtra(){ |
|---|
| 321 | $ret = null; |
|---|
| 322 | if (isset($this->_extra)) { |
|---|
| 323 | $ret = $this->_extra; |
|---|
| 324 | } |
|---|
| 325 | return $ret; |
|---|
| 326 | } |
|---|
| 327 | |
|---|
| 328 | /** |
|---|
| 329 | * make an element "required" |
|---|
| 330 | * |
|---|
| 331 | * @param object &$formElement reference to a {@link XoopsFormElement} |
|---|
| 332 | */ |
|---|
| 333 | function setRequired(&$formElement){ |
|---|
| 334 | $this->_required[] =& $formElement; |
|---|
| 335 | } |
|---|
| 336 | |
|---|
| 337 | /** |
|---|
| 338 | * get an array of "required" form elements |
|---|
| 339 | * |
|---|
| 340 | * @return array array of {@link XoopsFormElement}s |
|---|
| 341 | */ |
|---|
| 342 | function &getRequired(){ |
|---|
| 343 | return $this->_required; |
|---|
| 344 | } |
|---|
| 345 | |
|---|
| 346 | /** |
|---|
| 347 | * insert a break in the form |
|---|
| 348 | * |
|---|
| 349 | * This method is abstract. It must be overwritten in the child classes. |
|---|
| 350 | * |
|---|
| 351 | * @param string $extra extra information for the break |
|---|
| 352 | * @abstract |
|---|
| 353 | */ |
|---|
| 354 | function insertBreak($extra = null){ |
|---|
| 355 | } |
|---|
| 356 | |
|---|
| 357 | /** |
|---|
| 358 | * returns renderered form |
|---|
| 359 | * |
|---|
| 360 | * This method is abstract. It must be overwritten in the child classes. |
|---|
| 361 | * |
|---|
| 362 | * @abstract |
|---|
| 363 | */ |
|---|
| 364 | function render(){ |
|---|
| 365 | } |
|---|
| 366 | |
|---|
| 367 | /** |
|---|
| 368 | * displays rendered form |
|---|
| 369 | */ |
|---|
| 370 | function display(){ |
|---|
| 371 | echo $this->render(); |
|---|
| 372 | } |
|---|
| 373 | |
|---|
| 374 | /** |
|---|
| 375 | * Renders the Javascript function needed for client-side for validation |
|---|
| 376 | * |
|---|
| 377 | * @param boolean $withtags Include the < javascript > tags in the returned string |
|---|
| 378 | */ |
|---|
| 379 | function renderValidationJS( $withtags = true ) { |
|---|
| 380 | $js = ""; |
|---|
| 381 | if ( $withtags ) { |
|---|
| 382 | $js .= "\n<!-- Start Form Vaidation JavaScript //-->\n<script type='text/javascript'>\n<!--//\n"; |
|---|
| 383 | } |
|---|
| 384 | $myts =& MyTextSanitizer::getInstance(); |
|---|
| 385 | $formname = $this->getName(); |
|---|
| 386 | $required =& $this->getRequired(); |
|---|
| 387 | $reqcount = count($required); |
|---|
| 388 | $js .= "function xoopsFormValidate_{$formname}() { |
|---|
| 389 | myform = window.document.$formname;\n"; |
|---|
| 390 | for ($i = 0; $i < $reqcount; $i++) { |
|---|
| 391 | $eltname = $required[$i]->getName(); |
|---|
| 392 | $eltcaption = trim( $required[$i]->getCaption() ); |
|---|
| 393 | $eltmsg = empty($eltcaption) ? sprintf( _FORM_ENTER, $eltname ) : sprintf( _FORM_ENTER, $eltcaption ); |
|---|
| 394 | $eltmsg = str_replace('"', '\"', stripslashes($eltmsg)); |
|---|
| 395 | $js .= "if ( myform.{$eltname}.value == \"\" ) " |
|---|
| 396 | . "{ window.alert(\"{$eltmsg}\"); myform.{$eltname}.focus(); return false; }\n"; |
|---|
| 397 | } |
|---|
| 398 | $js .= "return true;\n}\n"; |
|---|
| 399 | if ( $withtags ) { |
|---|
| 400 | $js .= "//--></script>\n<!-- End Form Vaidation JavaScript //-->\n"; |
|---|
| 401 | } |
|---|
| 402 | return $js; |
|---|
| 403 | } |
|---|
| 404 | /** |
|---|
| 405 | * assign to smarty form template instead of displaying directly |
|---|
| 406 | * |
|---|
| 407 | * @param object &$tpl reference to a {@link Smarty} object |
|---|
| 408 | * @see Smarty |
|---|
| 409 | */ |
|---|
| 410 | function assign(&$tpl){ |
|---|
| 411 | $i = 0; |
|---|
| 412 | $elements = array(); |
|---|
| 413 | foreach ( $this->getElements() as $ele ) { |
|---|
| 414 | $n = ($ele->getName() != "") ? $ele->getName() : $i; |
|---|
| 415 | $elements[$n]['name'] = $ele->getName(); |
|---|
| 416 | $elements[$n]['caption'] = $ele->getCaption(); |
|---|
| 417 | $elements[$n]['body'] = $ele->render(); |
|---|
| 418 | $elements[$n]['hidden'] = $ele->isHidden(); |
|---|
| 419 | if ($ele->getDescription() != '') { |
|---|
| 420 | $elements[$n]['description'] = $ele->getDescription(); |
|---|
| 421 | } |
|---|
| 422 | $i++; |
|---|
| 423 | } |
|---|
| 424 | $js = $this->renderValidationJS(); |
|---|
| 425 | $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)); |
|---|
| 426 | } |
|---|
| 427 | } |
|---|
| 428 | ?> |
|---|