| Total Complexity | 88 |
| Total Lines | 639 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like XoopsForm often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use XoopsForm, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class XoopsForm |
||
| 32 | { |
||
| 33 | /** |
||
| 34 | * *#@+ |
||
| 35 | * |
||
| 36 | * @access private |
||
| 37 | */ |
||
| 38 | /** |
||
| 39 | * "action" attribute for the html form |
||
| 40 | * |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | public $_action; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * "method" attribute for the form. |
||
| 47 | * |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | public $_method; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * "name" attribute of the form |
||
| 54 | * |
||
| 55 | * @var string |
||
| 56 | */ |
||
| 57 | public $_name; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * title for the form |
||
| 61 | * |
||
| 62 | * @var string |
||
| 63 | */ |
||
| 64 | public $_title; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * summary for the form (WGAC2 Requirement) |
||
| 68 | * |
||
| 69 | * @var string |
||
| 70 | */ |
||
| 71 | public $_summary = ''; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * array of {@link XoopsFormElement} objects |
||
| 75 | * |
||
| 76 | * @var array |
||
| 77 | */ |
||
| 78 | public $_elements = array(); |
||
| 79 | |||
| 80 | /** |
||
| 81 | * HTML classes for the <form> tag |
||
| 82 | * |
||
| 83 | * @var array |
||
| 84 | */ |
||
| 85 | public $_class = array(); |
||
| 86 | |||
| 87 | /** |
||
| 88 | * extra information for the <form> tag |
||
| 89 | * |
||
| 90 | * @var array |
||
| 91 | */ |
||
| 92 | public $_extra = array(); |
||
| 93 | |||
| 94 | /** |
||
| 95 | * required elements |
||
| 96 | * |
||
| 97 | * @var array |
||
| 98 | */ |
||
| 99 | public $_required = array(); |
||
| 100 | |||
| 101 | /** |
||
| 102 | * additional serialised object checksum (ERM Analysis - Requirement) |
||
| 103 | * @deprecated |
||
| 104 | * @access private |
||
| 105 | */ |
||
| 106 | public $_objid = 'da39a3ee5e6b4b0d3255bfef95601890afd80709'; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * *#@- |
||
| 110 | */ |
||
| 111 | |||
| 112 | /** |
||
| 113 | * constructor |
||
| 114 | * |
||
| 115 | * @param string $title title of the form |
||
| 116 | * @param string $name "name" attribute for the <form> tag |
||
| 117 | * @param string $action "action" attribute for the <form> tag |
||
| 118 | * @param string $method "method" attribute for the <form> tag |
||
| 119 | * @param bool $addtoken whether to add a security token to the form |
||
| 120 | * @param string $summary |
||
| 121 | */ |
||
| 122 | public function __construct($title, $name, $action, $method = 'post', $addtoken = false, $summary = '') |
||
| 123 | { |
||
| 124 | $this->_title = $title; |
||
| 125 | $this->_name = $name; |
||
| 126 | $this->_action = $action; |
||
| 127 | $this->_method = $method; |
||
| 128 | $this->_summary = $summary; |
||
| 129 | if ($addtoken != false) { |
||
|
|
|||
| 130 | $this->addElement(new XoopsFormHiddenToken()); |
||
| 131 | } |
||
| 132 | } |
||
| 133 | /** |
||
| 134 | * PHP 4 style constructor compatibility shim |
||
| 135 | * @deprecated all callers should be using parent::__construct() |
||
| 136 | */ |
||
| 137 | public function XoopsForm() |
||
| 138 | { |
||
| 139 | $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1); |
||
| 140 | trigger_error("Should call parent::__construct in {$trace[0]['file']} line {$trace[0]['line']},"); |
||
| 141 | self::__construct(); |
||
| 142 | } |
||
| 143 | /** |
||
| 144 | * *#@+ |
||
| 145 | * retrieves object serialisation/identification id (sha1 used) |
||
| 146 | * |
||
| 147 | * each object has serialisation<br> |
||
| 148 | * - legal requirement of enterprise relational management (ERM) |
||
| 149 | * |
||
| 150 | * @deprecated |
||
| 151 | * @access public |
||
| 152 | * @param $object |
||
| 153 | * @param string $hashinfo |
||
| 154 | * @return string |
||
| 155 | */ |
||
| 156 | public function getObjectID($object, $hashinfo = 'sha1') |
||
| 157 | { |
||
| 158 | if (!is_object($object)) { |
||
| 159 | $object = $this; |
||
| 160 | } |
||
| 161 | |||
| 162 | switch ($hashinfo) { |
||
| 163 | case 'md5': |
||
| 164 | |||
| 165 | @$var['name'] = md5(get_class($object)); |
||
| 166 | |||
| 167 | foreach (get_object_vars($object) as $key => $value) { |
||
| 168 | if ($key !== '_objid') { |
||
| 169 | @$var['value'] = $this->getArrayID($value, $key, $var['value'], $hashinfo); |
||
| 170 | } |
||
| 171 | } |
||
| 172 | |||
| 173 | foreach (get_class_methods($object) as $key => $value) { |
||
| 174 | @$var['func'] = $this->getArrayID($value, $key, $var['func'], $hashinfo); |
||
| 175 | } |
||
| 176 | |||
| 177 | @$this->_objid = md5($var['name'] . ':' . $var['func'] . ':' . $var['value']); |
||
| 178 | |||
| 179 | return $this->_objid; |
||
| 180 | break; |
||
| 181 | |||
| 182 | default: |
||
| 183 | |||
| 184 | @$var['name'] = sha1(get_class($object)); |
||
| 185 | |||
| 186 | foreach (get_object_vars($object) as $key => $value) { |
||
| 187 | if ($key !== '_objid') { |
||
| 188 | @$var['value'] = $this->getArrayID($value, $key, $var['value'], $hashinfo); |
||
| 189 | } |
||
| 190 | } |
||
| 191 | |||
| 192 | foreach (get_class_methods($object) as $key => $value) { |
||
| 193 | @$var['func'] = $this->getArrayID($value, $key, $var['func'], $hashinfo); |
||
| 194 | } |
||
| 195 | |||
| 196 | @$this->_objid = sha1($var['name'] . ':' . $var['func'] . ':' . $var['value']); |
||
| 197 | |||
| 198 | return $this->_objid; |
||
| 199 | |||
| 200 | } |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @param $value |
||
| 205 | * @param $key |
||
| 206 | * @param $ret |
||
| 207 | * @param string $hashinfo |
||
| 208 | * |
||
| 209 | * @return string |
||
| 210 | */ |
||
| 211 | public function getArrayID($value, $key, $ret, $hashinfo = 'sha1') |
||
| 212 | { |
||
| 213 | switch ($hashinfo) { |
||
| 214 | case 'md5': |
||
| 215 | if (is_array($value)) { |
||
| 216 | foreach ($value as $keyb => $valueb) { |
||
| 217 | @$ret = md5($ret . ':' . $this->getArrayID($valueb, $keyb, $ret, $hashinfo)); |
||
| 218 | } |
||
| 219 | } else { |
||
| 220 | @$ret = md5($ret . ':' . $key . ':' . $value); |
||
| 221 | } |
||
| 222 | |||
| 223 | return $ret; |
||
| 224 | break; |
||
| 225 | default: |
||
| 226 | if (is_array($value)) { |
||
| 227 | foreach ($value as $keyb => $valueb) { |
||
| 228 | @$ret = sha1($ret . ':' . $this->getArrayID($valueb, $keyb, $ret, $hashinfo)); |
||
| 229 | } |
||
| 230 | } else { |
||
| 231 | @$ret = sha1($ret . ':' . $key . ':' . $value); |
||
| 232 | } |
||
| 233 | |||
| 234 | return $ret; |
||
| 235 | break; |
||
| 236 | } |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * return the summary of the form |
||
| 241 | * |
||
| 242 | * @param bool $encode To sanitizer the text? |
||
| 243 | * @return string |
||
| 244 | */ |
||
| 245 | public function getSummary($encode = false) |
||
| 246 | { |
||
| 247 | return $encode ? htmlspecialchars($this->_summary, ENT_QUOTES) : $this->_summary; |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * return the title of the form |
||
| 252 | * |
||
| 253 | * @param bool $encode To sanitizer the text? |
||
| 254 | * @return string |
||
| 255 | */ |
||
| 256 | public function getTitle($encode = false) |
||
| 257 | { |
||
| 258 | return $encode ? htmlspecialchars($this->_title, ENT_QUOTES) : $this->_title; |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * get the "name" attribute for the <form> tag |
||
| 263 | * |
||
| 264 | * Deprecated, to be refactored |
||
| 265 | * |
||
| 266 | * @param bool $encode To sanitizer the text? |
||
| 267 | * @return string |
||
| 268 | */ |
||
| 269 | public function getName($encode = true) |
||
| 270 | { |
||
| 271 | return $encode ? htmlspecialchars($this->_name, ENT_QUOTES) : $this->_name; |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * get the "action" attribute for the <form> tag |
||
| 276 | * |
||
| 277 | * @param bool $encode To sanitizer the text? |
||
| 278 | * @return string |
||
| 279 | */ |
||
| 280 | public function getAction($encode = true) |
||
| 281 | { |
||
| 282 | // Convert & to & for backward compatibility |
||
| 283 | return $encode ? htmlspecialchars(str_replace('&', '&', $this->_action), ENT_QUOTES) : $this->_action; |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * get the "method" attribute for the <form> tag |
||
| 288 | * |
||
| 289 | * @return string |
||
| 290 | */ |
||
| 291 | public function getMethod() |
||
| 292 | { |
||
| 293 | return (strtolower($this->_method) === 'get') ? 'get' : 'post'; |
||
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Add an element to the form |
||
| 298 | * |
||
| 299 | * @param string|XoopsFormElement $formElement reference to a {@link XoopsFormElement} |
||
| 300 | * @param bool $required is this a "required" element? |
||
| 301 | * |
||
| 302 | */ |
||
| 303 | public function addElement($formElement, $required = false) |
||
| 304 | { |
||
| 305 | if (is_string($formElement)) { |
||
| 306 | $this->_elements[] = $formElement; |
||
| 307 | } elseif (is_subclass_of($formElement, 'XoopsFormElement')) { |
||
| 308 | $this->_elements[] = &$formElement; |
||
| 309 | if (!$formElement->isContainer()) { |
||
| 310 | if ($required) { |
||
| 311 | $formElement->_required = true; |
||
| 312 | $this->_required[] = &$formElement; |
||
| 313 | } |
||
| 314 | } else { |
||
| 315 | $required_elements = &$formElement->getRequired(); |
||
| 316 | $count = count($required_elements); |
||
| 317 | for ($i = 0; $i < $count; ++$i) { |
||
| 318 | $this->_required[] = &$required_elements[$i]; |
||
| 319 | } |
||
| 320 | } |
||
| 321 | } |
||
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | * get an array of forms elements |
||
| 326 | * |
||
| 327 | * @param bool $recurse get elements recursively? |
||
| 328 | * |
||
| 329 | * @return XoopsFormElement[] array of {@link XoopsFormElement}s |
||
| 330 | */ |
||
| 331 | public function &getElements($recurse = false) |
||
| 332 | { |
||
| 333 | if (!$recurse) { |
||
| 334 | return $this->_elements; |
||
| 335 | } else { |
||
| 336 | $ret = array(); |
||
| 337 | $count = count($this->_elements); |
||
| 338 | for ($i = 0; $i < $count; ++$i) { |
||
| 339 | if (is_object($this->_elements[$i])) { |
||
| 340 | if (!$this->_elements[$i]->isContainer()) { |
||
| 341 | $ret[] = &$this->_elements[$i]; |
||
| 342 | } else { |
||
| 343 | $elements = &$this->_elements[$i]->getElements(true); |
||
| 344 | $count2 = count($elements); |
||
| 345 | for ($j = 0; $j < $count2; ++$j) { |
||
| 346 | $ret[] = &$elements[$j]; |
||
| 347 | } |
||
| 348 | unset($elements); |
||
| 349 | } |
||
| 350 | } |
||
| 351 | } |
||
| 352 | |||
| 353 | return $ret; |
||
| 354 | } |
||
| 355 | } |
||
| 356 | |||
| 357 | /** |
||
| 358 | * get an array of "name" attributes of form elements |
||
| 359 | * |
||
| 360 | * @return array array of form element names |
||
| 361 | */ |
||
| 362 | public function getElementNames() |
||
| 363 | { |
||
| 364 | $ret = array(); |
||
| 365 | $elements = &$this->getElements(true); |
||
| 366 | $count = count($elements); |
||
| 367 | for ($i = 0; $i < $count; ++$i) { |
||
| 368 | $ret[] = $elements[$i]->getName(); |
||
| 369 | } |
||
| 370 | |||
| 371 | return $ret; |
||
| 372 | } |
||
| 373 | |||
| 374 | /** |
||
| 375 | * get a reference to a {@link XoopsFormElement} object by its "name" |
||
| 376 | * |
||
| 377 | * @param string $name "name" attribute assigned to a {@link XoopsFormElement} |
||
| 378 | * @return object reference to a {@link XoopsFormElement}, false if not found |
||
| 379 | */ |
||
| 380 | public function &getElementByName($name) |
||
| 381 | { |
||
| 382 | $elements =& $this->getElements(true); |
||
| 383 | $count = count($elements); |
||
| 384 | for ($i = 0; $i < $count; ++$i) { |
||
| 385 | if ($name == $elements[$i]->getName(false)) { |
||
| 386 | return $elements[$i]; |
||
| 387 | } |
||
| 388 | } |
||
| 389 | $elt = null; |
||
| 390 | |||
| 391 | return $elt; |
||
| 392 | } |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Sets the "value" attribute of a form element |
||
| 396 | * |
||
| 397 | * @param string $name the "name" attribute of a form element |
||
| 398 | * @param string $value the "value" attribute of a form element |
||
| 399 | */ |
||
| 400 | public function setElementValue($name, $value) |
||
| 401 | { |
||
| 402 | $ele = &$this->getElementByName($name); |
||
| 403 | if (is_object($ele) && method_exists($ele, 'setValue')) { |
||
| 404 | $ele->setValue($value); |
||
| 405 | } |
||
| 406 | } |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Sets the "value" attribute of form elements in a batch |
||
| 410 | * |
||
| 411 | * @param array $values array of name/value pairs to be assigned to form elements |
||
| 412 | */ |
||
| 413 | public function setElementValues($values) |
||
| 414 | { |
||
| 415 | if (is_array($values) && !empty($values)) { |
||
| 416 | // will not use getElementByName() for performance.. |
||
| 417 | $elements = &$this->getElements(true); |
||
| 418 | $count = count($elements); |
||
| 419 | for ($i = 0; $i < $count; ++$i) { |
||
| 420 | $name = $elements[$i]->getName(false); |
||
| 421 | if ($name && isset($values[$name]) && method_exists($elements[$i], 'setValue')) { |
||
| 422 | $elements[$i]->setValue($values[$name]); |
||
| 423 | } |
||
| 424 | } |
||
| 425 | } |
||
| 426 | } |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Gets the "value" attribute of a form element |
||
| 430 | * |
||
| 431 | * @param string $name the "name" attribute of a form element |
||
| 432 | * @param bool $encode To sanitizer the text? |
||
| 433 | * @return string the "value" attribute assigned to a form element, null if not set |
||
| 434 | */ |
||
| 435 | public function getElementValue($name, $encode = false) |
||
| 443 | } |
||
| 444 | |||
| 445 | /** |
||
| 446 | * gets the "value" attribute of all form elements |
||
| 447 | * |
||
| 448 | * @param bool $encode To sanitizer the text? |
||
| 449 | * @return array array of name/value pairs assigned to form elements |
||
| 450 | */ |
||
| 451 | public function getElementValues($encode = false) |
||
| 452 | { |
||
| 453 | // will not use getElementByName() for performance.. |
||
| 454 | $elements = &$this->getElements(true); |
||
| 455 | $count = count($elements); |
||
| 456 | $values = array(); |
||
| 457 | for ($i = 0; $i < $count; ++$i) { |
||
| 458 | $name = $elements[$i]->getName(false); |
||
| 459 | if ($name && method_exists($elements[$i], 'getValue')) { |
||
| 460 | $values[$name] = &$elements[$i]->getValue($encode); |
||
| 461 | } |
||
| 462 | } |
||
| 463 | |||
| 464 | return $values; |
||
| 465 | } |
||
| 466 | |||
| 467 | /** |
||
| 468 | * set the "class" attribute for the <form> tag |
||
| 469 | * |
||
| 470 | * @param string $class |
||
| 471 | */ |
||
| 472 | public function setClass($class) |
||
| 473 | { |
||
| 474 | $class = trim($class); |
||
| 475 | if (!empty($class)) { |
||
| 476 | $this->_class[] = $class; |
||
| 477 | } |
||
| 478 | } |
||
| 479 | |||
| 480 | /** |
||
| 481 | * set the extra attributes for the <form> tag |
||
| 482 | * |
||
| 483 | * @param string $extra extra attributes for the <form> tag |
||
| 484 | */ |
||
| 485 | public function setExtra($extra) |
||
| 486 | { |
||
| 487 | if (!empty($extra)) { |
||
| 488 | $this->_extra[] = $extra; |
||
| 489 | } |
||
| 490 | } |
||
| 491 | |||
| 492 | /** |
||
| 493 | * set the summary tag for the <form> tag |
||
| 494 | * |
||
| 495 | * @param string $summary |
||
| 496 | */ |
||
| 497 | public function setSummary($summary) |
||
| 498 | { |
||
| 499 | if (!empty($summary)) { |
||
| 500 | $this->summary = strip_tags($summary); |
||
| 501 | } |
||
| 502 | } |
||
| 503 | |||
| 504 | /** |
||
| 505 | * get the "class" attribute for the <form> tag |
||
| 506 | * |
||
| 507 | * @return string "class" attribute value |
||
| 508 | */ |
||
| 509 | public function &getClass() |
||
| 510 | { |
||
| 511 | if (empty($this->_class)) { |
||
| 512 | return false; |
||
| 513 | } |
||
| 514 | $classes = array(); |
||
| 515 | foreach ($this->_class as $class) { |
||
| 516 | $classes[] = htmlspecialchars($class, ENT_QUOTES); |
||
| 517 | } |
||
| 518 | |||
| 519 | return implode(' ', $classes); |
||
| 520 | } |
||
| 521 | |||
| 522 | /** |
||
| 523 | * get the extra attributes for the <form> tag |
||
| 524 | * |
||
| 525 | * @return string |
||
| 526 | */ |
||
| 527 | public function &getExtra() |
||
| 528 | { |
||
| 529 | $extra = empty($this->_extra) ? '' : ' ' . implode(' ', $this->_extra); |
||
| 530 | |||
| 531 | return $extra; |
||
| 532 | } |
||
| 533 | |||
| 534 | /** |
||
| 535 | * make an element "required" |
||
| 536 | * |
||
| 537 | * @param XoopsFormElement $formElement reference to a {@link XoopsFormElement} |
||
| 538 | */ |
||
| 539 | public function setRequired(XoopsFormElement $formElement) |
||
| 542 | } |
||
| 543 | |||
| 544 | /** |
||
| 545 | * get an array of "required" form elements |
||
| 546 | * |
||
| 547 | * @return array array of {@link XoopsFormElement}s |
||
| 548 | */ |
||
| 549 | public function &getRequired() |
||
| 550 | { |
||
| 551 | return $this->_required; |
||
| 552 | } |
||
| 553 | |||
| 554 | /** |
||
| 555 | * insert a break in the form |
||
| 556 | * |
||
| 557 | * This method is abstract. It must be overwritten in the child classes. |
||
| 558 | * |
||
| 559 | * @param string $extra extra information for the break |
||
| 560 | * @abstract |
||
| 561 | */ |
||
| 562 | public function insertBreak($extra = null) |
||
| 563 | { |
||
| 564 | } |
||
| 565 | |||
| 566 | /** |
||
| 567 | * returns renderered form |
||
| 568 | * |
||
| 569 | * This method is abstract. It must be overwritten in the child classes. |
||
| 570 | * |
||
| 571 | * @abstract |
||
| 572 | */ |
||
| 573 | public function render() |
||
| 574 | { |
||
| 575 | } |
||
| 576 | |||
| 577 | /** |
||
| 578 | * displays rendered form |
||
| 579 | */ |
||
| 580 | public function display() |
||
| 581 | { |
||
| 582 | echo $this->render(); |
||
| 583 | } |
||
| 584 | |||
| 585 | /** |
||
| 586 | * Renders the Javascript function needed for client-side for validation |
||
| 587 | * |
||
| 588 | * Form elements that have been declared "required" and not set will prevent the form from being |
||
| 589 | * submitted. Additionally, each element class may provide its own "renderValidationJS" method |
||
| 590 | * that is supposed to return custom validation code for the element. |
||
| 591 | * |
||
| 592 | * The element validation code can assume that the JS "myform" variable points to the form, and must |
||
| 593 | * execute <i>return false</i> if validation fails. |
||
| 594 | * |
||
| 595 | * A basic element validation method may contain something like this: |
||
| 596 | * <code> |
||
| 597 | * function renderValidationJS() { |
||
| 598 | * $name = $this->getName(); |
||
| 599 | * return "if (myform.{$name}.value != 'valid') { " . |
||
| 600 | * "myform.{$name}.focus(); window.alert( '$name is invalid' ); return false;" . |
||
| 601 | * " }"; |
||
| 602 | * } |
||
| 603 | * </code> |
||
| 604 | * |
||
| 605 | * @param boolean $withtags Include the < javascript > tags in the returned string |
||
| 606 | * |
||
| 607 | * @return string |
||
| 608 | */ |
||
| 609 | public function renderValidationJS($withtags = true) |
||
| 629 | } |
||
| 630 | |||
| 631 | /** |
||
| 632 | * assign to smarty form template instead of displaying directly |
||
| 633 | * |
||
| 634 | * @param XoopsTpl $tpl reference to a {@link Smarty} object object |
||
| 635 | * @see Smarty |
||
| 636 | */ |
||
| 637 | public function assign(XoopsTpl $tpl) |
||
| 638 | { |
||
| 639 | $i = -1; |
||
| 640 | $elements = array(); |
||
| 641 | // Removed hard-coded legacy pseudo-element - XoopsFormRenderer is now responsible for the legend |
||
| 642 | foreach ($this->getElements() as $ele) { |
||
| 643 | ++$i; |
||
| 644 | if (is_string($ele)) { |
||
| 645 | $elements[$i]['body'] = $ele; |
||
| 646 | continue; |
||
| 647 | } |
||
| 648 | $ele_name = $ele->getName(); |
||
| 649 | $ele_description = $ele->getDescription(); |
||
| 673 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.