[!!!][TASK] Improve flex and TCA handling in FormEngine
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / AbstractNode.php
1 <?php
2 namespace TYPO3\CMS\Backend\Form;
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\ArrayUtility;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19
20 /**
21 * Base class for container and single elements - their abstracts extend from here.
22 */
23 abstract class AbstractNode implements NodeInterface
24 {
25 /**
26 * Main data array to work on, given from parent to child elements
27 *
28 * @var array
29 */
30 protected $data = [];
31
32 /**
33 * Set data to data array.
34 *
35 * @todo: Should NOT set the nodeFactory instance, this is done by AbstractContainer only,
36 * @todo: but not done for Element classes: Elements are tree leaves, they MUST
37 * @todo: not create new nodes again.
38 * @todo: Currently, AbstractFormElement still does that, but do not rely on the fact that
39 * @todo: Element classes have an instance of NodeFactory at hand.
40 *
41 * @param NodeFactory $nodeFactory
42 * @param array $data
43 */
44 public function __construct(NodeFactory $nodeFactory, array $data)
45 {
46 $this->data = $data;
47 }
48
49 /**
50 * Handler for single nodes
51 *
52 * @return array As defined in initializeResultArray() of AbstractNode
53 */
54 abstract public function render();
55
56 /**
57 * Initialize the array that is returned to parent after calling. This structure
58 * is identical for *all* nodes. Parent will merge the return of a child with its
59 * own stuff and in itself return an array of the same structure.
60 *
61 * @return array
62 */
63 protected function initializeResultArray()
64 {
65 return [
66 'additionalJavaScriptPost' => [],
67 'additionalJavaScriptSubmit' => [],
68 'additionalHiddenFields' => [],
69 'additionalInlineLanguageLabelFiles' => [],
70 'stylesheetFiles' => [],
71 // can hold strings or arrays, string = requireJS module, array = requireJS module + callback e.g. array('TYPO3/Foo/Bar', 'function() {}')
72 'requireJsModules' => [],
73 'inlineData' => [],
74 'html' => '',
75 ];
76 }
77
78 /**
79 * Merge existing data with a child return array
80 *
81 * @param array $existing Currently merged array
82 * @param array $childReturn Array returned by child
83 * @return array Result array
84 */
85 protected function mergeChildReturnIntoExistingResult(array $existing, array $childReturn)
86 {
87 if (!empty($childReturn['html'])) {
88 $existing['html'] .= LF . $childReturn['html'];
89 }
90 if (!empty($childReturn['extJSCODE'])) {
91 $existing['extJSCODE'] .= LF . $childReturn['extJSCODE'];
92 }
93 foreach ($childReturn['additionalJavaScriptPost'] as $value) {
94 $existing['additionalJavaScriptPost'][] = $value;
95 }
96 foreach ($childReturn['additionalJavaScriptSubmit'] as $value) {
97 $existing['additionalJavaScriptSubmit'][] = $value;
98 }
99 foreach ($childReturn['additionalHiddenFields'] as $value) {
100 $existing['additionalHiddenFields'][] = $value;
101 }
102 foreach ($childReturn['stylesheetFiles'] as $value) {
103 $existing['stylesheetFiles'][] = $value;
104 }
105 if (!empty($childReturn['requireJsModules'])) {
106 foreach ($childReturn['requireJsModules'] as $module) {
107 $existing['requireJsModules'][] = $module;
108 }
109 }
110 if (!empty($childReturn['additionalInlineLanguageLabelFiles'])) {
111 foreach ($childReturn['additionalInlineLanguageLabelFiles'] as $inlineLanguageLabelFile) {
112 $existing['additionalInlineLanguageLabelFiles'][] = $inlineLanguageLabelFile;
113 }
114 }
115 if (!empty($childReturn['inlineData'])) {
116 $existingInlineData = $existing['inlineData'];
117 $childInlineData = $childReturn['inlineData'];
118 ArrayUtility::mergeRecursiveWithOverrule($existingInlineData, $childInlineData);
119 $existing['inlineData'] = $existingInlineData;
120 }
121 return $existing;
122 }
123
124 /**
125 * Build JSON string for validations rules and return it
126 * as data attribute for HTML elements.
127 *
128 * @param array $config
129 * @return string
130 */
131 protected function getValidationDataAsDataAttribute(array $config)
132 {
133 return sprintf(' data-formengine-validation-rules="%s" ', htmlspecialchars($this->getValidationDataAsJsonString($config)));
134 }
135
136 /**
137 * Build JSON string for validations rules.
138 *
139 * @param array $config
140 * @return string
141 */
142 protected function getValidationDataAsJsonString(array $config)
143 {
144 $validationRules = [];
145 if (!empty($config['eval'])) {
146 $evalList = GeneralUtility::trimExplode(',', $config['eval'], true);
147 unset($config['eval']);
148 foreach ($evalList as $evalType) {
149 $validationRules[] = [
150 'type' => $evalType,
151 'config' => $config
152 ];
153 }
154 }
155 if (!empty($config['range'])) {
156 $validationRules[] = [
157 'type' => 'range',
158 'config' => $config['range']
159 ];
160 }
161 if (!empty($config['maxitems']) || !empty($config['minitems'])) {
162 $minItems = (isset($config['minitems'])) ? (int)$config['minitems'] : 0;
163 $maxItems = (isset($config['maxitems'])) ? (int)$config['maxitems'] : 99999;
164 $type = ($config['type']) ?: 'range';
165 $validationRules[] = [
166 'type' => $type,
167 'minItems' => $minItems,
168 'maxItems' => $maxItems
169 ];
170 }
171 if (!empty($config['required'])) {
172 $validationRules[] = ['type' => 'required'];
173 }
174 return json_encode($validationRules);
175 }
176 }