[CLEANUP] Improve the @param/@return/@var PHPDoc
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / Domain / Model / Element / AbstractElement.php
1 <?php
2 namespace TYPO3\CMS\Form\Domain\Model\Element;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Utility\GeneralUtility;
18 use TYPO3\CMS\Form\Filter\FilterInterface;
19
20 /**
21 * Abstract for the form elements
22 *
23 * @author Patrick Broens <patrick@patrickbroens.nl>
24 */
25 abstract class AbstractElement {
26
27 /**
28 * @var string
29 */
30 const ELEMENT_TYPE_FORM = 'FORM';
31
32 /**
33 * @var string
34 */
35 const ELEMENT_TYPE_PLAIN = 'PLAIN';
36
37 /**
38 * @var string
39 */
40 const ELEMENT_TYPE_CONTENT = 'CONTENT';
41 /**
42 * Internal Id of the element
43 *
44 * @var int
45 */
46 protected $elementId;
47
48 /**
49 * @var string
50 */
51 protected $elementType = self::ELEMENT_TYPE_FORM;
52
53 /**
54 * The name of the element
55 *
56 * @var string
57 */
58 protected $name;
59
60 /**
61 * True if it accepts the parent name instead of its own
62 * This is necessary for groups
63 *
64 * @var bool
65 */
66 protected $acceptsParentName = FALSE;
67
68 /**
69 * Attribute object
70 *
71 * @var \TYPO3\CMS\Form\Domain\Model\Attribute\AttributesAttribute
72 */
73 protected $attributes;
74
75 /**
76 * Additional object
77 *
78 * @var \TYPO3\CMS\Form\Domain\Model\Additional\AdditionalAdditionalElement
79 */
80 protected $additional;
81
82 /**
83 * Layout override for the element
84 *
85 * @var string
86 */
87 protected $layout;
88
89 /**
90 * Value of the element
91 *
92 * @var mixed
93 */
94 protected $value;
95
96 /**
97 * Content of the element when no singleton tag
98 * <option>, <textarea>
99 *
100 * @var mixed
101 */
102 protected $data;
103
104 /**
105 * Allowed additionals for elements
106 *
107 * @var array
108 */
109 protected $allowedAdditional = array(
110 'label',
111 'legend'
112 );
113
114 /**
115 * Mandatory attributes for elements
116 *
117 * @var array
118 */
119 protected $mandatoryAttributes = array();
120
121 /**
122 * @var array
123 */
124 protected $allowedAttributes = array();
125
126 /**
127 * The content object
128 *
129 * @var \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
130 */
131 protected $localCobj;
132
133 /**
134 * @var \TYPO3\CMS\Form\Request
135 */
136 protected $requestHandler;
137
138 /**
139 * @var \TYPO3\CMS\Form\ElementCounter
140 */
141 protected $elementCounter;
142
143 /**
144 * @var \TYPO3\CMS\Form\Utility\ValidatorUtility
145 */
146 protected $validateClass;
147
148 /**
149 * @var \TYPO3\CMS\Form\Utility\FilterUtility
150 */
151 protected $filter;
152
153 /**
154 * Constructor
155 */
156 public function __construct() {
157 $this->localCobj = GeneralUtility::makeInstance(\TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::class);
158 $this->requestHandler = GeneralUtility::makeInstance(\TYPO3\CMS\Form\Request::class);
159 $this->validateClass = GeneralUtility::makeInstance(\TYPO3\CMS\Form\Utility\ValidatorUtility::class);
160 $this->elementCounter = GeneralUtility::makeInstance(\TYPO3\CMS\Form\ElementCounter::class);
161 $this->setElementId();
162 $this->createAttributes();
163 $this->createAdditional();
164 $this->createFilter();
165 }
166
167 /**
168 * Set the internal ID of the element
169 *
170 * @return void
171 */
172 public function setElementId() {
173 $this->elementId = $this->elementCounter->getElementId();
174 }
175
176 /**
177 * Get the internal ID for the id attribute
178 * of the outer tag of an element like <li>
179 *
180 * @return string
181 */
182 public function getElementId() {
183 return $this->elementId;
184 }
185
186 /**
187 * Gets the element type.
188 *
189 * @return string
190 */
191 public function getElementType() {
192 return $this->elementType;
193 }
194
195 /**
196 * Set the name for the element
197 *
198 * @param string $name The name
199 * @return void
200 */
201 public function setName($name = '') {
202 if (is_string($name) === FALSE) {
203 $name = '';
204 }
205 if ($name !== '') {
206 $this->name = $name;
207 } else {
208 $this->name = 'id-' . $this->getElementId();
209 }
210 }
211
212 /**
213 * Get the name of the element
214 *
215 * @return string
216 */
217 public function getName() {
218 return $this->name;
219 }
220
221 /**
222 * Check to see if this element accepts the parent name instead of its own
223 *
224 * @return bool
225 */
226 public function acceptsParentName() {
227 return $this->acceptsParentName;
228 }
229
230 /**
231 * Set a specific attribute by name and value
232 *
233 * @param string $attribute Name of the attribute
234 * @param mixed $value Value of the attribute
235 * @return \TYPO3\CMS\Form\Domain\Model\Element\AbstractElement
236 */
237 public function setAttribute($attribute, $value) {
238 if (array_key_exists($attribute, $this->allowedAttributes)) {
239 $this->attributes->addAttribute($attribute, $value);
240 }
241 return $this;
242 }
243
244 /**
245 * Get the allowed attributes for an element
246 *
247 * @return array The allowed attributes
248 */
249 public function getAllowedAttributes() {
250 return $this->allowedAttributes;
251 }
252
253 /**
254 * Get the mandatory attributes for an element
255 *
256 * @return array The mandatory attributes
257 */
258 public function getMandatoryAttributes() {
259 return $this->mandatoryAttributes;
260 }
261
262 /**
263 * Check if element has attributes which are allowed
264 *
265 * @return bool TRUE if there is a list of allowed attributes
266 */
267 public function hasAllowedAttributes() {
268 return empty($this->allowedAttributes) === FALSE;
269 }
270
271 /**
272 * Check if element has additionals which are allowed
273 *
274 * @return bool TRUE if there is a list of allowed additionals
275 */
276 public function hasAllowedAdditionals() {
277 return empty($this->allowedAdditional) === FALSE;
278 }
279
280 /**
281 * Get the allowed additionals for an element
282 *
283 * @return array The allowed additionals
284 */
285 public function getAllowedAdditionals() {
286 return $this->allowedAdditional;
287 }
288
289 /**
290 * Get the array with all attribute objects for the element
291 *
292 * @return array
293 */
294 public function getAttributes() {
295 return $this->attributes->getAttributes();
296 }
297
298 /**
299 * Returns TRUE if attribute is set
300 *
301 * @param string $key The name of the attribute
302 * @return bool
303 */
304 public function hasAttribute($key) {
305 return $this->attributes->hasAttribute($key);
306 }
307
308 /**
309 * Get the value of a specific attribute by key
310 *
311 * @param string $key Name of the attribute
312 * @return mixed
313 */
314 public function getAttributeValue($key) {
315 return $this->attributes->getValue($key);
316 }
317
318 /**
319 * Get the array with all additional objects for the element
320 *
321 * @return array
322 */
323 public function getAdditional() {
324 return $this->additional->getAdditional();
325 }
326
327 /**
328 * Get a specific additional object by using the key
329 *
330 * @param string $key Key of the additional
331 * @return \TYPO3\CMS\Form\Domain\Model\Additional\AbstractAdditionalElement The additional object
332 */
333 public function getAdditionalObjectByKey($key) {
334 return $this->additional->getAdditionalObjectByKey($key);
335 }
336
337 /**
338 * Get the value of a specific additional by key
339 *
340 * @param string $key Name of the additional
341 * @return mixed
342 */
343 public function getAdditionalValue($key) {
344 return $this->additional->getValue($key);
345 }
346
347 /**
348 * Load the attributes object
349 *
350 * @return void
351 */
352 protected function createAttributes() {
353 $className = \TYPO3\CMS\Form\Domain\Model\Attribute\AttributesAttribute::class;
354 $this->attributes = GeneralUtility::makeInstance($className, $this->elementId);
355 }
356
357 /**
358 * Set the layout override for the element
359 *
360 * @param string $layout The layout
361 * @return void
362 */
363 public function setLayout($layout = '') {
364 $this->layout = (string)$layout;
365 }
366
367 /**
368 * Get the layout for an element
369 *
370 * @return string XML for layout
371 */
372 public function getLayout() {
373 return $this->layout;
374 }
375
376 /**
377 * Set the value for the element
378 *
379 * @param string $value The value
380 * @return void
381 */
382 public function setValue($value = '') {
383 $this->value = (string)$value;
384 }
385
386 /**
387 * Get the value for the element
388 *
389 * @return mixed
390 */
391 public function getValue() {
392 return $this->value;
393 }
394
395 /**
396 * Set the content for the element
397 *
398 * @param string $data The content
399 * @return void
400 */
401 public function setData($data = '') {
402 $this->data = (string)$data;
403 }
404
405 /**
406 * Set the additionals from validation rules
407 *
408 * @return void
409 */
410 public function setMessagesFromValidation() {
411 if ($this->validateClass->hasMessage($this->getName())) {
412 $messages = $this->validateClass->getMessagesByName($this->getName());
413 $this->setAdditional('mandatory', 'COA', $messages);
414 }
415 }
416
417 /**
418 * Set the additional error from validation rules
419 *
420 * @return void
421 */
422 public function setErrorsFromValidation() {
423 if ($this->validateClass->hasErrors($this->getName())) {
424 $errors = $this->validateClass->getErrorsByName($this->getName());
425 $this->setAdditional('error', 'COA', $errors);
426 }
427 }
428
429 /**
430 * Set a specific additional by name and value
431 *
432 * @param string $additional Name of the additional
433 * @param string $type Typoscript content object
434 * @param mixed $value Value of the additional
435 * @return \TYPO3\CMS\Form\Domain\Model\Element\AbstractElement
436 */
437 public function setAdditional($additional, $type, $value) {
438 $this->additional->addAdditional($additional, $type, $value);
439 return $this;
440 }
441
442 /**
443 * Check if additional exists
444 *
445 * @param string $key Name of the additional
446 * @return bool
447 */
448 public function additionalIsSet($key) {
449 return $this->additional->additionalIsSet($key);
450 }
451
452 /**
453 * Load the additional object
454 *
455 * @return void
456 */
457 protected function createAdditional() {
458 $className = \TYPO3\CMS\Form\Domain\Model\Additional\AdditionalAdditionalElement::class;
459 $this->additional = GeneralUtility::makeInstance($className);
460 }
461
462 /**
463 * Set the layout for an additional element
464 *
465 * @param string $key Name of the additional
466 * @param string $layout XML for layout
467 * @return void
468 */
469 public function setAdditionalLayout($key, $layout) {
470 $this->additional->setLayout($key, $layout);
471 }
472
473 /**
474 * Load the filter object
475 *
476 * @return void
477 */
478 protected function createFilter() {
479 $this->filter = GeneralUtility::makeInstance(\TYPO3\CMS\Form\Utility\FilterUtility::class);
480 }
481
482 /**
483 * Make a filter object for an element
484 * This is a shortcut to the function in _filter
485 *
486 * @param string $class Name of the filter
487 * @param array $arguments Arguments for the filter
488 * @return FilterInterface Filter object
489 */
490 public function makeFilter($class, $arguments = array()) {
491 $filter = $this->filter->makeFilter($class, $arguments);
492 return $filter;
493 }
494
495 /**
496 * Add a filter to the filter list
497 * This is a shortcut to the function in _filter
498 *
499 * @param \TYPO3\CMS\Form\Filter\FilterInterface $filter Filter object
500 * @return void
501 */
502 public function addFilter($filter) {
503 $this->filter->addFilter($filter);
504 }
505
506 /**
507 * Dummy function to check the request handler on input
508 * and set submitted data right for elements
509 *
510 * @return \TYPO3\CMS\Form\Domain\Model\Element\AbstractElement
511 */
512 public function checkFilterAndSetIncomingDataFromRequest() {
513 return $this;
514 }
515
516 }