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