649c072aaa15f0b65c246f191844c1355c06781b
[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 * A list of global options given from parent to child elements
27 *
28 * @var array
29 */
30 protected $globalOptions = array();
31
32 /**
33 * Handler for single nodes
34 *
35 * @return array As defined in initializeResultArray() of AbstractNode
36 */
37 abstract public function render();
38
39 /**
40 * Set global options from parent instance
41 *
42 * @param array $globalOptions Global options like 'readonly' for all elements
43 * @return $this
44 */
45 public function setGlobalOptions(array $globalOptions) {
46 $this->globalOptions = $globalOptions;
47 return $this;
48 }
49
50 /**
51 * Initialize the array that is returned to parent after calling. This structure
52 * is identical for *all* nodes. Parent will merge the return of a child with its
53 * own stuff and in itself return an array of the same structure.
54 *
55 * @return array
56 */
57 protected function initializeResultArray() {
58 return array(
59 'additionalJavaScriptPost' => array(),
60 'additionalJavaScriptSubmit' => array(),
61 'additionalHiddenFields' => array(),
62 'additionalHeadTags' => array(),
63 'extJSCODE' => '',
64 'inlineData' => array(),
65 'html' => '',
66 );
67 }
68
69 /**
70 * Merge existing data with a child return array
71 *
72 * @param array $existing Currently merged array
73 * @param array $childReturn Array returned by child
74 * @return array Result array
75 */
76 protected function mergeChildReturnIntoExistingResult(array $existing, array $childReturn) {
77 if (!empty($childReturn['html'])) {
78 $existing['html'] .= LF . $childReturn['html'];
79 }
80 if (!empty($childReturn['extJSCODE'])) {
81 $existing['extJSCODE'] .= LF . $childReturn['extJSCODE'];
82 }
83 foreach ($childReturn['additionalJavaScriptPost'] as $value) {
84 $existing['additionalJavaScriptPost'][] = $value;
85 }
86 foreach ($childReturn['additionalJavaScriptSubmit'] as $value) {
87 $existing['additionalJavaScriptSubmit'][] = $value;
88 }
89 foreach ($childReturn['additionalHiddenFields'] as $value) {
90 $existing['additionalHiddenFields'][] = $value;
91 }
92 foreach ($childReturn['additionalHeadTags'] as $value) {
93 $existing['additionalHeadTags'][] = $value;
94 }
95 if (!empty($childReturn['inlineData'])) {
96 $existingInlineData = $existing['inlineData'];
97 $childInlineData = $childReturn['inlineData'];
98 ArrayUtility::mergeRecursiveWithOverrule($existingInlineData, $childInlineData);
99 $existing['inlineData'] = $existingInlineData;
100 }
101 return $existing;
102 }
103
104 /**
105 * Determine and get the value for the placeholder for an input field.
106 * Typically used in an inline relation where values from fields down the record chain
107 * are used as "default" values for fields.
108 *
109 * @param string $table
110 * @param array $config
111 * @param array $row
112 * @return mixed
113 */
114 protected function getPlaceholderValue($table, array $config, array $row) {
115 $value = trim($config['placeholder']);
116 if (!$value) {
117 return '';
118 }
119 // Check if we have a reference to another field value from the current record
120 if (substr($value, 0, 6) === '__row|') {
121 /** @var FormDataTraverser $traverser */
122 $traverseFields = GeneralUtility::trimExplode('|', substr($value, 6));
123 $traverser = GeneralUtility::makeInstance(FormDataTraverser::class);
124 $value = $traverser->getTraversedFieldValue($traverseFields, $table, $row, $this->globalOptions['inlineFirstPid']);
125 }
126
127 return $value;
128 }
129
130 /**
131 * Build JSON string for validations rules and return it
132 * as data attribute for HTML elements.
133 *
134 * @param array $config
135 * @return string
136 */
137 protected function getValidationDataAsDataAttribute(array $config) {
138 return sprintf(' data-formengine-validation-rules="%s" ', htmlspecialchars($this->getValidationDataAsJsonString($config)));
139 }
140
141 /**
142 * Build JSON string for validations rules.
143 *
144 * @param array $config
145 * @return string
146 */
147 protected function getValidationDataAsJsonString(array $config) {
148 $validationRules = array();
149 if (!empty($config['maxitems']) || !empty($config['minitems'])) {
150 $minItems = (isset($config['minitems'])) ? (int)$config['minitems'] : 0;
151 $maxItems = (isset($config['maxitems'])) ? (int)$config['maxitems'] : 10000;
152 $type = ($config['type']) ?: 'range';
153 if ($config['renderMode'] !== 'tree' && $maxItems <= 1 && $minItems > 0) {
154 $validationRules[] = array(
155 'type' => $type,
156 'minItems' => 1,
157 'maxItems' => 100000
158 );
159 } else {
160 $validationRules[] = array(
161 'type' => $type,
162 'minItems' => $minItems,
163 'maxItems' => $maxItems
164 );
165 }
166 }
167 if (!empty($config['required'])) {
168 $validationRules[] = array('type' => 'required');
169 }
170 return json_encode($validationRules);
171 }
172
173 }