Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
33.95% |
73 / 215 |
|
32.35% |
11 / 34 |
CRAP | |
0.00% |
0 / 1 |
| tao_helpers_form_Form | |
33.95% |
73 / 215 |
|
32.35% |
11 / 34 |
4481.73 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| setName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setOptions | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| hasElement | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| getElement | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
20 | |||
| getElements | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setElements | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| removeElement | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
30 | |||
| addElement | |
70.00% |
7 / 10 |
|
0.00% |
0 / 1 |
5.68 | |||
| setActions | |
57.14% |
4 / 7 |
|
0.00% |
0 / 1 |
3.71 | |||
| getActions | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| getAction | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
20 | |||
| setDecorator | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setDecorators | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| getDecorator | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| renderElements | |
26.03% |
19 / 73 |
|
0.00% |
0 / 1 |
622.49 | |||
| renderActions | |
85.71% |
12 / 14 |
|
0.00% |
0 / 1 |
7.14 | |||
| initElements | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| hasFileUpload | |
66.67% |
4 / 6 |
|
0.00% |
0 / 1 |
3.33 | |||
| isValid | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isSubmited | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setValues | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
20 | |||
| setElementValue | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
20 | |||
| setValue | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| disable | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
4 | |||
| getValues | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| getValue | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| getGroups | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setGroups | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| createGroup | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
30 | |||
| addToGroup | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
| getElementGroup | |
50.00% |
3 / 6 |
|
0.00% |
0 / 1 |
4.12 | |||
| removeGroup | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| evaluateInputValues | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| evaluate | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| render | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| 1 | <?php |
| 2 | |
| 3 | // phpcs:disable Generic.Files.LineLength |
| 4 | /** |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License |
| 7 | * as published by the Free Software Foundation; under version 2 |
| 8 | * of the License (non-upgradable). |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 | * |
| 19 | * Copyright (c) 2008-2010 (original work) Deutsche Institut für Internationale Pädagogische Forschung |
| 20 | * (under the project TAO-TRANSFER); |
| 21 | * 2009-2012 (update and modification) Public Research Centre Henri Tudor |
| 22 | * (under the project TAO-SUSTAIN & TAO-DEV); |
| 23 | * 2021-2022 (original work) Open Assessment Technologies SA |
| 24 | */ |
| 25 | // phpcs:enable |
| 26 | |
| 27 | declare(strict_types=1); |
| 28 | |
| 29 | /** |
| 30 | * Represents a form. It provides the default behavior for form management and |
| 31 | * be overridden for any rendering mode. |
| 32 | * A form is composed by a set of FormElements. |
| 33 | * |
| 34 | * The form data flow is: |
| 35 | * 1. add the elements to the form instance |
| 36 | * 2. run evaluate (initElements, update states (submited, valid, etc), update |
| 37 | * ) |
| 38 | * 3. render form |
| 39 | * |
| 40 | * @abstract |
| 41 | * @access public |
| 42 | * @author Cédric Alfonsi, <cedric.alfonsi@tudor.lu> |
| 43 | * @package tao |
| 44 | |
| 45 | */ |
| 46 | // phpcs:ignore |
| 47 | abstract class tao_helpers_form_Form |
| 48 | { |
| 49 | /** |
| 50 | * the form name |
| 51 | * |
| 52 | * @access protected |
| 53 | * @var string |
| 54 | */ |
| 55 | protected $name = ''; |
| 56 | |
| 57 | /** |
| 58 | * the list of element composing the form |
| 59 | * |
| 60 | * @access protected |
| 61 | * @var tao_helpers_form_FormElement[] |
| 62 | */ |
| 63 | protected $elements = []; |
| 64 | |
| 65 | /** |
| 66 | * the actions of the form by context |
| 67 | * |
| 68 | * @access protected |
| 69 | * @var tao_helpers_form_FormElement[][] |
| 70 | */ |
| 71 | protected $actions = []; |
| 72 | |
| 73 | /** |
| 74 | * if the form is valid or not |
| 75 | * |
| 76 | * @access public |
| 77 | * @var bool |
| 78 | */ |
| 79 | public $valid = false; |
| 80 | |
| 81 | /** |
| 82 | * if the form has been submited or not |
| 83 | * |
| 84 | * @access protected |
| 85 | * @var bool |
| 86 | */ |
| 87 | protected $submited = false; |
| 88 | |
| 89 | /** |
| 90 | * It represents the logicall groups |
| 91 | * |
| 92 | * @access protected |
| 93 | * @var array |
| 94 | */ |
| 95 | protected $groups = []; |
| 96 | |
| 97 | /** |
| 98 | * The list of Decorator linked to the form |
| 99 | * |
| 100 | * @access protected |
| 101 | * @var array |
| 102 | */ |
| 103 | protected $decorators = []; |
| 104 | |
| 105 | /** |
| 106 | * The form's options |
| 107 | * |
| 108 | * @access protected |
| 109 | * @var array |
| 110 | */ |
| 111 | protected $options = []; |
| 112 | |
| 113 | /** |
| 114 | * Global form error message |
| 115 | * |
| 116 | * @access public |
| 117 | * @var string |
| 118 | */ |
| 119 | public $error = ''; |
| 120 | |
| 121 | /** |
| 122 | * List of fields names that are system only and which values doesn't need to be returned by `getValues()` call |
| 123 | * |
| 124 | * @access protected |
| 125 | * @var array |
| 126 | */ |
| 127 | protected $systemElements = []; |
| 128 | |
| 129 | /** |
| 130 | * the form constructor |
| 131 | * |
| 132 | * @access public |
| 133 | * @author Cédric Alfonsi, <cedric.alfonsi@tudor.lu> |
| 134 | * @param string $name |
| 135 | * @param array $options |
| 136 | */ |
| 137 | public function __construct($name = '', array $options = []) |
| 138 | { |
| 139 | $this->name = $name; |
| 140 | $this->options = $options; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * set the form name |
| 145 | * |
| 146 | * @access public |
| 147 | * @author Cédric Alfonsi, <cedric.alfonsi@tudor.lu> |
| 148 | * @param string $name |
| 149 | */ |
| 150 | public function setName($name) |
| 151 | { |
| 152 | $this->name = $name; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Get the form name |
| 157 | * |
| 158 | * @access public |
| 159 | * @author Cédric Alfonsi, <cedric.alfonsi@tudor.lu> |
| 160 | * @return string |
| 161 | */ |
| 162 | public function getName() |
| 163 | { |
| 164 | return (string) $this->name; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * set the form options |
| 169 | * |
| 170 | * @access public |
| 171 | * @author Cédric Alfonsi, <cedric.alfonsi@tudor.lu> |
| 172 | * @param array $options |
| 173 | */ |
| 174 | public function setOptions(array $options) |
| 175 | { |
| 176 | $this->options = $options; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Has an element of the form identified by it's name |
| 181 | * |
| 182 | * @param string $name |
| 183 | * |
| 184 | * @return bool |
| 185 | */ |
| 186 | public function hasElement($name) |
| 187 | { |
| 188 | foreach ($this->elements as $element) { |
| 189 | if ($element->getName() === $name) { |
| 190 | return true; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | return false; |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * get an element of the form identified by it's name |
| 199 | * |
| 200 | * @access public |
| 201 | * @author Cédric Alfonsi, <cedric.alfonsi@tudor.lu> |
| 202 | * @param string $name |
| 203 | * @return tao_helpers_form_FormElement |
| 204 | */ |
| 205 | public function getElement($name) |
| 206 | { |
| 207 | $returnValue = null; |
| 208 | |
| 209 | foreach ($this->elements as $element) { |
| 210 | if ($element->getName() === $name) { |
| 211 | $returnValue = $element; |
| 212 | break; |
| 213 | } |
| 214 | } |
| 215 | if ($returnValue === null) { |
| 216 | common_Logger::w('Element with name "' . $name . '" not found'); |
| 217 | } |
| 218 | |
| 219 | |
| 220 | return $returnValue; |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * get all the form elements |
| 225 | * |
| 226 | * @access public |
| 227 | * @author Cédric Alfonsi, <cedric.alfonsi@tudor.lu> |
| 228 | * @return array |
| 229 | */ |
| 230 | public function getElements() |
| 231 | { |
| 232 | return $this->elements; |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Define the list of form elements |
| 237 | * |
| 238 | * @access public |
| 239 | * @author Cédric Alfonsi, <cedric.alfonsi@tudor.lu> |
| 240 | * @param array $elements |
| 241 | */ |
| 242 | public function setElements(array $elements) |
| 243 | { |
| 244 | $this->elements = $elements; |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Remove an element identified by it's name. |
| 249 | * |
| 250 | * @access public |
| 251 | * @author Cédric Alfonsi, <cedric.alfonsi@tudor.lu> |
| 252 | * @param string $name |
| 253 | * @return bool |
| 254 | */ |
| 255 | public function removeElement($name) |
| 256 | { |
| 257 | $returnValue = false; |
| 258 | |
| 259 | foreach ($this->elements as $index => $element) { |
| 260 | if ($element->getName() === $name) { |
| 261 | unset($this->elements[$index]); |
| 262 | $groupName = $this->getElementGroup($name); |
| 263 | if (!empty($groupName) && isset($this->groups[$groupName]['elements'][$name])) { |
| 264 | unset($this->groups[$groupName]['elements'][$name]); |
| 265 | } |
| 266 | $returnValue = true; |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | return $returnValue; |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * Add an element to the form |
| 275 | * |
| 276 | * @access public |
| 277 | * @author Cédric Alfonsi, <cedric.alfonsi@tudor.lu> |
| 278 | * @param tao_helpers_form_FormElement $element |
| 279 | * @param bool|false $isSystem |
| 280 | */ |
| 281 | public function addElement(tao_helpers_form_FormElement $element, $isSystem = false) |
| 282 | { |
| 283 | $elementPosition = -1; |
| 284 | foreach ($this->elements as $i => $elt) { |
| 285 | if ($elt->getName() === $element->getName()) { |
| 286 | $elementPosition = $i; |
| 287 | break; |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | if ($elementPosition >= 0) { |
| 292 | $this->elements[$elementPosition] = $element; |
| 293 | } else { |
| 294 | $this->elements[] = $element; |
| 295 | } |
| 296 | |
| 297 | if ($isSystem) { |
| 298 | $this->systemElements[] = $element->getName(); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * Define the form actions for a context. |
| 304 | * The different contexts are top and bottom. |
| 305 | * |
| 306 | * @access public |
| 307 | * @author Cédric Alfonsi, <cedric.alfonsi@tudor.lu> |
| 308 | * |
| 309 | * @param array $actions |
| 310 | * @param string $context |
| 311 | * |
| 312 | * @throws Exception |
| 313 | */ |
| 314 | public function setActions($actions, $context = 'bottom') |
| 315 | { |
| 316 | $this->actions[$context] = []; |
| 317 | |
| 318 | foreach ($actions as $action) { |
| 319 | if (!$action instanceof tao_helpers_form_FormElement) { |
| 320 | throw new Exception( |
| 321 | 'The actions parameter must only contains instances of tao_helpers_form_FormElement' |
| 322 | ); |
| 323 | } |
| 324 | $this->actions[$context][] = $action; |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * Get the defined actions for a context |
| 330 | * |
| 331 | * @access public |
| 332 | * @author Cédric Alfonsi, <cedric.alfonsi@tudor.lu> |
| 333 | * @param string $context |
| 334 | * @return array |
| 335 | */ |
| 336 | public function getActions($context = 'bottom') |
| 337 | { |
| 338 | $returnValue = []; |
| 339 | |
| 340 | if (isset($this->actions[$context])) { |
| 341 | $returnValue = $this->actions[$context]; |
| 342 | } |
| 343 | |
| 344 | return (array) $returnValue; |
| 345 | } |
| 346 | |
| 347 | /** |
| 348 | * Get specific action element from context |
| 349 | * @param $name |
| 350 | * @param string $context |
| 351 | * |
| 352 | * @return mixed |
| 353 | */ |
| 354 | public function getAction($name, $context = 'bottom') |
| 355 | { |
| 356 | $returnValue = null; |
| 357 | |
| 358 | foreach ($this->getActions($context) as $element) { |
| 359 | if ($element->getName() === $name) { |
| 360 | $returnValue = $element; |
| 361 | break; |
| 362 | } |
| 363 | } |
| 364 | if ($returnValue === null) { |
| 365 | common_Logger::w('Action with name \'' . $name . '\' not found'); |
| 366 | } |
| 367 | |
| 368 | return $returnValue; |
| 369 | } |
| 370 | |
| 371 | /** |
| 372 | * Set the decorator of the type defined in parameter. |
| 373 | * The different types are element, error, group. |
| 374 | * By default it uses the element decorator. |
| 375 | * |
| 376 | * @access public |
| 377 | * @author Cédric Alfonsi, <cedric.alfonsi@tudor.lu> |
| 378 | * |
| 379 | * @param tao_helpers_form_Decorator $decorator |
| 380 | * @param string $type type |
| 381 | */ |
| 382 | public function setDecorator(tao_helpers_form_Decorator $decorator, $type = 'element') |
| 383 | { |
| 384 | $this->decorators[$type] = $decorator; |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * Set the form decorators |
| 389 | * |
| 390 | * @access public |
| 391 | * @author Cédric Alfonsi, <cedric.alfonsi@tudor.lu> |
| 392 | * @param array decorators |
| 393 | */ |
| 394 | public function setDecorators($decorators) |
| 395 | { |
| 396 | foreach ($decorators as $type => $decorator) { |
| 397 | $this->setDecorator($decorator, $type); |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | /** |
| 402 | * Get the decorator of the type defined in parameter. |
| 403 | * The different types are element, error, group. |
| 404 | * By default it uses the element decorator. |
| 405 | * |
| 406 | * @access public |
| 407 | * @author Cédric Alfonsi, <cedric.alfonsi@tudor.lu> |
| 408 | * @param string $type |
| 409 | * @return tao_helpers_form_Decorator |
| 410 | */ |
| 411 | public function getDecorator($type = 'element') |
| 412 | { |
| 413 | $returnValue = null; |
| 414 | |
| 415 | |
| 416 | if (array_key_exists($type, $this->decorators)) { |
| 417 | $returnValue = $this->decorators[$type]; |
| 418 | } |
| 419 | |
| 420 | |
| 421 | return $returnValue; |
| 422 | } |
| 423 | |
| 424 | /** |
| 425 | * render all the form elements |
| 426 | * |
| 427 | * @access public |
| 428 | * @author Cédric Alfonsi, <cedric.alfonsi@tudor.lu> |
| 429 | * @return string |
| 430 | */ |
| 431 | public function renderElements() |
| 432 | { |
| 433 | $returnValue = ''; |
| 434 | |
| 435 | |
| 436 | foreach ($this->elements as $element) { |
| 437 | if ($this->getElementGroup($element->getName()) !== '') { |
| 438 | continue; |
| 439 | } |
| 440 | |
| 441 | if ($this->getDecorator() !== null && !($element instanceof tao_helpers_form_elements_Hidden)) { |
| 442 | $returnValue .= $this->getDecorator()->preRender(); |
| 443 | } |
| 444 | |
| 445 | if (!$this->isValid() && $element->getError()) { |
| 446 | $element->addClass('error'); |
| 447 | } |
| 448 | |
| 449 | //Pass Resource Type |
| 450 | $element->addAttribute('resourceType', $this->options['resourceType'] ?? null); |
| 451 | |
| 452 | //render element |
| 453 | $returnValue .= $element->render(); |
| 454 | |
| 455 | //render element help |
| 456 | $help = trim($element->getHelp()); |
| 457 | if (!empty($help)) { |
| 458 | if ($this->getDecorator('help') !== null) { |
| 459 | $returnValue .= $this->getDecorator('help')->preRender(); |
| 460 | } |
| 461 | |
| 462 | $returnValue .= $help; |
| 463 | |
| 464 | if ($this->getDecorator('help') !== null) { |
| 465 | $returnValue .= $this->getDecorator('help')->postRender(); |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | //render error message |
| 470 | if (!$this->isValid() && $element->getError()) { |
| 471 | if ($this->getDecorator('error') !== null) { |
| 472 | $returnValue .= $this->getDecorator('error')->preRender(); |
| 473 | } |
| 474 | |
| 475 | $returnValue .= nl2br($element->getError()); |
| 476 | |
| 477 | if ($this->getDecorator('error') !== null) { |
| 478 | $returnValue .= $this->getDecorator('error')->postRender(); |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | if (!$element instanceof tao_helpers_form_elements_Hidden && $this->getDecorator() !== null) { |
| 483 | $returnValue .= $this->getDecorator()->postRender(); |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | $subGroupDecorator = null; |
| 488 | if ($this->getDecorator('group') instanceof tao_helpers_form_Decorator) { |
| 489 | $decoratorClass = get_class($this->getDecorator('group')); |
| 490 | $subGroupDecorator = new $decoratorClass(); |
| 491 | } |
| 492 | |
| 493 | //render group |
| 494 | foreach ($this->groups as $groupName => $group) { |
| 495 | if ($this->getDecorator('group') !== null) { |
| 496 | $this->getDecorator('group')->setOption('id', $groupName); |
| 497 | if (isset($group['options']['class'])) { |
| 498 | $cssClasses = explode(' ', $this->getDecorator('group')->getOption('cssClass')); |
| 499 | $currentClasses = array_map('trim', $cssClasses); |
| 500 | if (!in_array($group['options']['class'], $currentClasses, true)) { |
| 501 | $currentClasses[] = $group['options']['class']; |
| 502 | $this->getDecorator('group')->setOption( |
| 503 | 'cssClass', |
| 504 | implode(' ', array_unique($currentClasses)) |
| 505 | ); |
| 506 | } |
| 507 | } |
| 508 | $returnValue .= $this->getDecorator('group')->preRender(); |
| 509 | } |
| 510 | $returnValue .= $group['title']; |
| 511 | if ($subGroupDecorator instanceof tao_helpers_form_Decorator) { |
| 512 | $returnValue .= $subGroupDecorator->preRender(); |
| 513 | } |
| 514 | foreach ($group['elements'] as $elementName) { |
| 515 | if ($this->getElementGroup($elementName) === $groupName && $element = $this->getElement($elementName)) { |
| 516 | if ($this->getDecorator() !== null) { |
| 517 | $returnValue .= $this->getDecorator()->preRender(); |
| 518 | } |
| 519 | |
| 520 | //render element |
| 521 | if (! $this->isValid() && $element->getError()) { |
| 522 | $element->addClass('error'); |
| 523 | } |
| 524 | $returnValue .= $element->render(); |
| 525 | |
| 526 | //render element help |
| 527 | $help = trim($element->getHelp()); |
| 528 | if (!empty($help)) { |
| 529 | if ($this->getDecorator('help') !== null) { |
| 530 | $returnValue .= $this->getDecorator('help')->preRender(); |
| 531 | } |
| 532 | |
| 533 | $returnValue .= $help; |
| 534 | |
| 535 | if ($this->getDecorator('help') !== null) { |
| 536 | $returnValue .= $this->getDecorator('help')->postRender(); |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | //render error message |
| 541 | if (!$this->isValid() && $element->getError()) { |
| 542 | if ($this->getDecorator('error') !== null) { |
| 543 | $returnValue .= $this->getDecorator('error')->preRender(); |
| 544 | } |
| 545 | $returnValue .= nl2br($element->getError()); |
| 546 | if ($this->getDecorator('error') !== null) { |
| 547 | $returnValue .= $this->getDecorator('error')->postRender(); |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | if ($this->getDecorator() !== null) { |
| 552 | $returnValue .= $this->getDecorator()->postRender(); |
| 553 | } |
| 554 | } |
| 555 | } |
| 556 | if ($subGroupDecorator instanceof tao_helpers_form_Decorator) { |
| 557 | $returnValue .= $subGroupDecorator->postRender(); |
| 558 | } |
| 559 | if ($this->getDecorator('group') !== null) { |
| 560 | $returnValue .= $this->getDecorator('group')->postRender(); |
| 561 | $this->getDecorator('group')->setOption('id', ''); |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | |
| 566 | return $returnValue; |
| 567 | } |
| 568 | |
| 569 | /** |
| 570 | * render the form actions by context |
| 571 | * |
| 572 | * @access public |
| 573 | * @author Cédric Alfonsi, <cedric.alfonsi@tudor.lu> |
| 574 | * @param string $context |
| 575 | * @return string |
| 576 | */ |
| 577 | public function renderActions($context = 'bottom') |
| 578 | { |
| 579 | $returnValue = ''; |
| 580 | |
| 581 | if (isset($this->actions[$context])) { |
| 582 | $decorator = null; |
| 583 | if ($this->getDecorator('actions-' . $context) !== null) { |
| 584 | $decorator = $this->getDecorator('actions-' . $context); |
| 585 | } elseif ($this->getDecorator('actions') !== null) { |
| 586 | $decorator = $this->getDecorator('actions'); |
| 587 | } |
| 588 | |
| 589 | if ($decorator !== null) { |
| 590 | $returnValue .= $decorator->preRender(); |
| 591 | } |
| 592 | |
| 593 | foreach ($this->actions[$context] as $action) { |
| 594 | $returnValue .= $action->render(); |
| 595 | } |
| 596 | |
| 597 | if ($decorator !== null) { |
| 598 | $returnValue .= $decorator->postRender(); |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | return $returnValue; |
| 603 | } |
| 604 | |
| 605 | /** |
| 606 | * Initialize the elements set |
| 607 | */ |
| 608 | protected function initElements() |
| 609 | { |
| 610 | } |
| 611 | |
| 612 | /** |
| 613 | * Check if the form contains a file upload element |
| 614 | * |
| 615 | * @access public |
| 616 | * @author Cédric Alfonsi, <cedric.alfonsi@tudor.lu> |
| 617 | * @return bool |
| 618 | */ |
| 619 | public function hasFileUpload() |
| 620 | { |
| 621 | $returnValue = false; |
| 622 | |
| 623 | foreach ($this->elements as $element) { |
| 624 | if ($element instanceof tao_helpers_form_elements_File) { |
| 625 | $returnValue = true; |
| 626 | break; |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | return $returnValue; |
| 631 | } |
| 632 | |
| 633 | /** |
| 634 | * Enables you to know if the form is valid |
| 635 | * |
| 636 | * @access public |
| 637 | * @author Cédric Alfonsi, <cedric.alfonsi@tudor.lu> |
| 638 | * @return bool |
| 639 | */ |
| 640 | public function isValid() |
| 641 | { |
| 642 | return $this->valid; |
| 643 | } |
| 644 | |
| 645 | /** |
| 646 | * Enables you to know if the form has been submited |
| 647 | * |
| 648 | * @access public |
| 649 | * @author Cédric Alfonsi, <cedric.alfonsi@tudor.lu> |
| 650 | * @return bool |
| 651 | */ |
| 652 | public function isSubmited() |
| 653 | { |
| 654 | return $this->submited; |
| 655 | } |
| 656 | |
| 657 | /** |
| 658 | * Update manually the values of the form |
| 659 | * |
| 660 | * @access public |
| 661 | * @author Cédric Alfonsi, <cedric.alfonsi@tudor.lu> |
| 662 | * @param array $values |
| 663 | */ |
| 664 | public function setValues($values) |
| 665 | { |
| 666 | foreach ($values as $key => $value) { |
| 667 | foreach ($this->elements as $element) { |
| 668 | if ($element->getName() === $key) { |
| 669 | $this->setElementValue($element, $value); |
| 670 | |
| 671 | break; |
| 672 | } |
| 673 | } |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | public function setElementValue(tao_helpers_form_FormElement $element, $value): void |
| 678 | { |
| 679 | if ( |
| 680 | $element instanceof tao_helpers_form_elements_Checkbox |
| 681 | || (method_exists($element, 'setValues') && is_array($value)) |
| 682 | ) { |
| 683 | $element->setValues($value); |
| 684 | |
| 685 | return; |
| 686 | } |
| 687 | |
| 688 | $element->setValue($value); |
| 689 | } |
| 690 | |
| 691 | public function setValue(string $name, $value): void |
| 692 | { |
| 693 | $element = $this->getElement($name); |
| 694 | |
| 695 | if ($element) { |
| 696 | $this->setElementValue($element, $value); |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | /** |
| 701 | * Disables the whole form |
| 702 | */ |
| 703 | public function disable() |
| 704 | { |
| 705 | foreach ($this->elements as $element) { |
| 706 | $element->disable(); |
| 707 | } |
| 708 | |
| 709 | foreach ($this->actions as $context) { |
| 710 | foreach ($context as $action) { |
| 711 | $action->disable(); |
| 712 | } |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | /** |
| 717 | * Get the current values of the form |
| 718 | * |
| 719 | * @access public |
| 720 | * @author Cédric Alfonsi, <cedric.alfonsi@tudor.lu> |
| 721 | * @param string $groupName |
| 722 | * @return array |
| 723 | */ |
| 724 | abstract public function getValues($groupName = ''); |
| 725 | |
| 726 | /** |
| 727 | * get the current value of the element identified by the name in parameter |
| 728 | * |
| 729 | * @access public |
| 730 | * @author Cédric Alfonsi, <cedric.alfonsi@tudor.lu> |
| 731 | * @param string $name |
| 732 | * @return boolean|string |
| 733 | */ |
| 734 | public function getValue($name) |
| 735 | { |
| 736 | foreach ($this->elements as $element) { |
| 737 | if ($element->getName() === $name) { |
| 738 | return $element->getEvaluatedValue(); |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | return false; |
| 743 | } |
| 744 | |
| 745 | /** |
| 746 | * Short description of method getGroups |
| 747 | * |
| 748 | * @access public |
| 749 | * @author Cédric Alfonsi, <cedric.alfonsi@tudor.lu> |
| 750 | * @return array |
| 751 | */ |
| 752 | public function getGroups() |
| 753 | { |
| 754 | return $this->groups; |
| 755 | } |
| 756 | |
| 757 | /** |
| 758 | * Short description of method setGroups |
| 759 | * |
| 760 | * @access public |
| 761 | * @author Cédric Alfonsi, <cedric.alfonsi@tudor.lu> |
| 762 | * @param array $groups |
| 763 | */ |
| 764 | public function setGroups($groups) |
| 765 | { |
| 766 | $this->groups = $groups; |
| 767 | } |
| 768 | |
| 769 | /** |
| 770 | * Create a logical group of elements |
| 771 | * |
| 772 | * @access public |
| 773 | * @author Cédric Alfonsi, <cedric.alfonsi@tudor.lu> |
| 774 | * @param string $groupName |
| 775 | * @param string $groupTitle |
| 776 | * @param array $elements array of form elements or their identifiers |
| 777 | * @param array $options |
| 778 | * |
| 779 | * @throws common_Exception |
| 780 | */ |
| 781 | public function createGroup($groupName, $groupTitle = '', array $elements = [], array $options = []) |
| 782 | { |
| 783 | $identifier = []; |
| 784 | foreach ($elements as $element) { |
| 785 | if ($element instanceof tao_helpers_form_FormElement) { |
| 786 | $identifier[] = $element->getName(); |
| 787 | } elseif (is_string($element)) { |
| 788 | $identifier[] = $element; |
| 789 | } else { |
| 790 | throw new common_Exception('Unknown element of type ' . gettype($element) . ' in ' . __FUNCTION__); |
| 791 | } |
| 792 | } |
| 793 | $this->groups[$groupName] = [ |
| 794 | 'title' => empty($groupTitle) ? $groupName : $groupTitle, |
| 795 | 'elements' => $identifier, |
| 796 | 'options' => $options |
| 797 | ]; |
| 798 | } |
| 799 | |
| 800 | /** |
| 801 | * add an element to a group |
| 802 | * |
| 803 | * @access public |
| 804 | * @author Cédric Alfonsi, <cedric.alfonsi@tudor.lu> |
| 805 | * @param string $groupName |
| 806 | * @param string $elementName |
| 807 | */ |
| 808 | public function addToGroup($groupName, $elementName = '') |
| 809 | { |
| 810 | if ( |
| 811 | isset($this->groups[$groupName]['elements']) |
| 812 | && !in_array( |
| 813 | $elementName, |
| 814 | $this->groups[$groupName]['elements'], |
| 815 | true |
| 816 | ) |
| 817 | ) { |
| 818 | $this->groups[$groupName]['elements'][] = $elementName; |
| 819 | } |
| 820 | } |
| 821 | |
| 822 | /** |
| 823 | * get the group where is an element |
| 824 | * |
| 825 | * @access protected |
| 826 | * @author Cédric Alfonsi, <cedric.alfonsi@tudor.lu> |
| 827 | * @param string $elementName |
| 828 | * @return string |
| 829 | */ |
| 830 | protected function getElementGroup($elementName) |
| 831 | { |
| 832 | $returnValue = ''; |
| 833 | |
| 834 | foreach ($this->groups as $groupName => $group) { |
| 835 | if (in_array($elementName, $group['elements'], true)) { |
| 836 | $returnValue = $groupName; |
| 837 | break; |
| 838 | } |
| 839 | } |
| 840 | |
| 841 | return $returnValue; |
| 842 | } |
| 843 | |
| 844 | /** |
| 845 | * remove the group identified by the name in parameter |
| 846 | * |
| 847 | * @access public |
| 848 | * @author Cédric Alfonsi, <cedric.alfonsi@tudor.lu> |
| 849 | * @param string $groupName |
| 850 | */ |
| 851 | public function removeGroup($groupName) |
| 852 | { |
| 853 | if (isset($this->groups[$groupName])) { |
| 854 | foreach ($this->groups[$groupName]['elements'] as $element) { |
| 855 | $this->removeElement($element); |
| 856 | } |
| 857 | unset($this->groups[$groupName]); |
| 858 | } |
| 859 | } |
| 860 | |
| 861 | public function evaluateInputValues(): void |
| 862 | { |
| 863 | foreach ($this->elements as $id => $element) { |
| 864 | $this->elements[$id]->feedInputValue(); |
| 865 | } |
| 866 | } |
| 867 | |
| 868 | /** |
| 869 | * evaluate the form inside the current context. Must be overridden, for |
| 870 | * rendering mode: for example, it's used to populate and validate the data |
| 871 | * the http request for an xhtml context |
| 872 | * |
| 873 | * @abstract |
| 874 | * @access public |
| 875 | * @author Cédric Alfonsi, <cedric.alfonsi@tudor.lu> |
| 876 | * @return mixed |
| 877 | */ |
| 878 | abstract public function evaluate(); |
| 879 | |
| 880 | /** |
| 881 | * Render the form. Must be overridden for each rendering mode. |
| 882 | * |
| 883 | * @abstract |
| 884 | * @access public |
| 885 | * @author Cédric Alfonsi, <cedric.alfonsi@tudor.lu> |
| 886 | * @return string |
| 887 | */ |
| 888 | abstract public function render(); |
| 889 | } |