[TASK] FormEngine: Replace placeholder resolving with a data provider
[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 = array();
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 $this->data = $data;
46 }
47
48 /**
49 * Handler for single nodes
50 *
51 * @return array As defined in initializeResultArray() of AbstractNode
52 */
53 abstract public function render();
54
55 /**
56 * Initialize the array that is returned to parent after calling. This structure
57 * is identical for *all* nodes. Parent will merge the return of a child with its
58 * own stuff and in itself return an array of the same structure.
59 *
60 * @return array
61 */
62 protected function initializeResultArray() {
63 return array(
64 'additionalJavaScriptPost' => array(),
65 'additionalJavaScriptSubmit' => array(),
66 'additionalHiddenFields' => array(),
67 'stylesheetFiles' => array(),
68 // can hold strings or arrays, string = requireJS module, array = requireJS module + callback e.g. array('TYPO3/Foo/Bar', 'function() {}')
69 'requireJsModules' => array(),
70 'extJSCODE' => '',
71 'inlineData' => array(),
72 'html' => '',
73 );
74 }
75
76 /**
77 * Merge existing data with a child return array
78 *
79 * @param array $existing Currently merged array
80 * @param array $childReturn Array returned by child
81 * @return array Result array
82 */
83 protected function mergeChildReturnIntoExistingResult(array $existing, array $childReturn) {
84 if (!empty($childReturn['html'])) {
85 $existing['html'] .= LF . $childReturn['html'];
86 }
87 if (!empty($childReturn['extJSCODE'])) {
88 $existing['extJSCODE'] .= LF . $childReturn['extJSCODE'];
89 }
90 foreach ($childReturn['additionalJavaScriptPost'] as $value) {
91 $existing['additionalJavaScriptPost'][] = $value;
92 }
93 foreach ($childReturn['additionalJavaScriptSubmit'] as $value) {
94 $existing['additionalJavaScriptSubmit'][] = $value;
95 }
96 foreach ($childReturn['additionalHiddenFields'] as $value) {
97 $existing['additionalHiddenFields'][] = $value;
98 }
99 foreach ($childReturn['stylesheetFiles'] as $value) {
100 $existing['stylesheetFiles'][] = $value;
101 }
102 if (!empty($childReturn['requireJsModules'])) {
103 foreach ($childReturn['requireJsModules'] as $module) {
104 $existing['requireJsModules'][] = $module;
105 }
106 }
107 if (!empty($childReturn['inlineData'])) {
108 $existingInlineData = $existing['inlineData'];
109 $childInlineData = $childReturn['inlineData'];
110 ArrayUtility::mergeRecursiveWithOverrule($existingInlineData, $childInlineData);
111 $existing['inlineData'] = $existingInlineData;
112 }
113 return $existing;
114 }
115
116 /**
117 * Build JSON string for validations rules and return it
118 * as data attribute for HTML elements.
119 *
120 * @param array $config
121 * @return string
122 */
123 protected function getValidationDataAsDataAttribute(array $config) {
124 return sprintf(' data-formengine-validation-rules="%s" ', htmlspecialchars($this->getValidationDataAsJsonString($config)));
125 }
126
127 /**
128 * Build JSON string for validations rules.
129 *
130 * @param array $config
131 * @return string
132 */
133 protected function getValidationDataAsJsonString(array $config) {
134 $validationRules = array();
135 if (!empty($config['eval'])) {
136 $evalList = GeneralUtility::trimExplode(',', $config['eval'], TRUE);
137 unset($config['eval']);
138 foreach ($evalList as $evalType) {
139 $validationRules[] = array(
140 'type' => $evalType,
141 'config' => $config
142 );
143 }
144 }
145 if (!empty($config['range'])) {
146 $validationRules[] = array(
147 'type' => 'range',
148 'config' => $config['range']
149 );
150 }
151 if (!empty($config['maxitems']) || !empty($config['minitems'])) {
152 $minItems = (isset($config['minitems'])) ? (int)$config['minitems'] : 0;
153 $maxItems = (isset($config['maxitems'])) ? (int)$config['maxitems'] : 10000;
154 $type = ($config['type']) ?: 'range';
155 if ($config['renderMode'] !== 'tree' && $maxItems <= 1 && $minItems > 0) {
156 $validationRules[] = array(
157 'type' => $type,
158 'minItems' => 1,
159 'maxItems' => 100000
160 );
161 } else {
162 $validationRules[] = array(
163 'type' => $type,
164 'minItems' => $minItems,
165 'maxItems' => $maxItems
166 );
167 }
168 }
169 if (!empty($config['required'])) {
170 $validationRules[] = array('type' => 'required');
171 }
172 return json_encode($validationRules);
173 }
174
175 }