692dcb8ae21eea168d3db39dcfdb3de6d9195983
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Controller / FormFlexAjaxController.php
1 <?php
2 declare(strict_types=1);
3 namespace TYPO3\CMS\Backend\Controller;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use Psr\Http\Message\ResponseInterface;
19 use Psr\Http\Message\ServerRequestInterface;
20 use TYPO3\CMS\Backend\Form\FormDataCompiler;
21 use TYPO3\CMS\Backend\Form\FormDataGroup\TcaDatabaseRecord;
22 use TYPO3\CMS\Backend\Form\NodeFactory;
23 use TYPO3\CMS\Core\Configuration\FlexForm\FlexFormTools;
24 use TYPO3\CMS\Core\Utility\ArrayUtility;
25 use TYPO3\CMS\Core\Utility\GeneralUtility;
26 use TYPO3\CMS\Core\Utility\StringUtility;
27
28 /**
29 * Handle FormEngine flex field ajax calls
30 */
31 class FormFlexAjaxController extends AbstractFormEngineAjaxController
32 {
33 /**
34 * Render a single flex form section container to add it to the DOM
35 *
36 * @param ServerRequestInterface $request
37 * @param ResponseInterface $response
38 * @return ResponseInterface
39 */
40 public function containerAdd(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
41 {
42 $queryParameters = $request->getParsedBody();
43
44 $vanillaUid = (int)$queryParameters['vanillaUid'];
45 $command = $queryParameters['command'];
46 $tableName = $queryParameters['tableName'];
47 $fieldName = $queryParameters['fieldName'];
48 $recordTypeValue = $queryParameters['recordTypeValue'];
49 $dataStructureIdentifier = json_encode($queryParameters['dataStructureIdentifier']);
50 $flexFormSheetName = $queryParameters['flexFormSheetName'];
51 $flexFormFieldName = $queryParameters['flexFormFieldName'];
52 $flexFormContainerName = $queryParameters['flexFormContainerName'];
53
54 // Prepare TCA and data values for a new section container using data providers
55 $processedTca = $GLOBALS['TCA'][$tableName];
56 $flexFormTools = GeneralUtility::makeInstance(FlexFormTools::class);
57 $dataStructure = $flexFormTools->parseDataStructureByIdentifier($dataStructureIdentifier);
58 $processedTca['columns'][$fieldName]['config']['ds'] = $dataStructure;
59 $processedTca['columns'][$fieldName]['config']['dataStructureIdentifier'] = $dataStructureIdentifier;
60 // Get a new unique id for this container.
61 $flexFormContainerIdentifier = StringUtility::getUniqueId();
62 $flexSectionContainerPreparation = [
63 'flexFormSheetName' => $flexFormSheetName,
64 'flexFormFieldName' => $flexFormFieldName,
65 'flexFormContainerName' => $flexFormContainerName,
66 'flexFormContainerIdentifier' => $flexFormContainerIdentifier,
67 ];
68
69 $formDataGroup = GeneralUtility::makeInstance(TcaDatabaseRecord::class);
70 $formDataCompiler = GeneralUtility::makeInstance(FormDataCompiler::class, $formDataGroup);
71 $formDataCompilerInput = [
72 'tableName' => $tableName,
73 'vanillaUid' => (int)$vanillaUid,
74 'command' => $command,
75 'recordTypeValue' => $recordTypeValue,
76 'processedTca' => $processedTca,
77 'flexSectionContainerPreparation' => $flexSectionContainerPreparation,
78 ];
79 $formData = $formDataCompiler->compile($formDataCompilerInput);
80
81 $dataStructure = $formData['processedTca']['columns'][$fieldName]['config']['ds'];
82 $formData['fieldName'] = $fieldName;
83 $formData['flexFormDataStructureArray'] = $dataStructure['sheets'][$flexFormSheetName]['ROOT']['el'][$flexFormFieldName]['children'][$flexFormContainerIdentifier];
84 $formData['flexFormDataStructureIdentifier'] = $dataStructureIdentifier;
85 $formData['flexFormFieldName'] = $flexFormFieldName;
86 $formData['flexFormSheetName'] = $flexFormSheetName;
87 $formData['flexFormContainerName'] = $flexFormContainerName;
88 $formData['flexFormContainerIdentifier'] = $flexFormContainerIdentifier;
89 $formData['flexFormContainerElementCollapsed'] = false;
90
91 $formData['flexFormFormPrefix'] = '[data][' . $flexFormSheetName . '][lDEF]' . '[' . $flexFormFieldName . ']' . '[el]';
92
93 // Set initialized data of that section container from compiler to the array part used
94 // by flexFormElementContainer which prepares parameterArray. Important for initialized
95 // values of group element.
96 if (isset($formData['databaseRow'][$fieldName]
97 ['data'][$flexFormSheetName]
98 ['lDEF'][$flexFormFieldName]
99 ['el'][$flexFormContainerIdentifier][$flexFormContainerName]['el']
100 )
101 && is_array($formData['databaseRow'][$fieldName]
102 ['data'][$flexFormSheetName]
103 ['lDEF'][$flexFormFieldName]
104 ['el'][$flexFormContainerIdentifier][$flexFormContainerName]['el']
105 )
106 ) {
107 $formData['flexFormRowData'] = $formData['databaseRow'][$fieldName]
108 ['data'][$flexFormSheetName]
109 ['lDEF'][$flexFormFieldName]
110 ['el'][$flexFormContainerIdentifier][$flexFormContainerName]['el'];
111 }
112
113 $formData['parameterArray']['itemFormElName'] = 'data[' . $tableName . '][' . $formData['databaseRow']['uid'] . '][' . $fieldName . ']';
114
115 // JavaScript code for event handlers:
116 // @todo: see if we can get rid of this - used in group elements, and also for the "reload" on type field changes
117 $formData['parameterArray']['fieldChangeFunc'] = [];
118 $formData['parameterArray']['fieldChangeFunc']['TBE_EDITOR_fieldChanged'] = 'TBE_EDITOR.fieldChanged('
119 . GeneralUtility::quoteJSvalue($tableName)
120 . ',' . GeneralUtility::quoteJSvalue($formData['databaseRow']['uid'])
121 . ',' . GeneralUtility::quoteJSvalue($fieldName)
122 . ',' . GeneralUtility::quoteJSvalue($formData['parameterArray']['itemFormElName'])
123 . ');';
124
125 // @todo: check GroupElement for usage of elementBaseName ... maybe kick that thing?
126
127 // Feed resulting form data to container structure to render HTML and other result data
128 $nodeFactory = GeneralUtility::makeInstance(NodeFactory::class);
129 $formData['renderType'] = 'flexFormContainerContainer';
130 $newContainerResult = $nodeFactory->create($formData)->render();
131
132 $jsonResult = [
133 'html' => $newContainerResult['html'],
134 'stylesheetFiles' => [],
135 'scriptCall' => [],
136 ];
137
138 if (!empty($newContainerResult['additionalJavaScriptSubmit'])) {
139 $additionalJavaScriptSubmit = implode('', $newContainerResult['additionalJavaScriptSubmit']);
140 $additionalJavaScriptSubmit = str_replace([CR, LF], '', $additionalJavaScriptSubmit);
141 $jsonResult['scriptCall'][] = 'TBE_EDITOR.addActionChecks("submit", "' . addslashes($additionalJavaScriptSubmit) . '");';
142 }
143 foreach ($newContainerResult['additionalJavaScriptPost'] as $singleAdditionalJavaScriptPost) {
144 $jsonResult['scriptCall'][] = $singleAdditionalJavaScriptPost;
145 }
146 foreach ($newContainerResult['stylesheetFiles'] as $stylesheetFile) {
147 $jsonResult['stylesheetFiles'][] = $this->getRelativePathToStylesheetFile($stylesheetFile);
148 }
149 if (!empty($newContainerResult['additionalInlineLanguageLabelFiles'])) {
150 $labels = [];
151 foreach ($newContainerResult['additionalInlineLanguageLabelFiles'] as $additionalInlineLanguageLabelFile) {
152 ArrayUtility::mergeRecursiveWithOverrule(
153 $labels,
154 $this->getLabelsFromLocalizationFile($additionalInlineLanguageLabelFile)
155 );
156 }
157 $javaScriptCode = [];
158 $javaScriptCode[] = 'if (typeof TYPO3 === \'undefined\' || typeof TYPO3.lang === \'undefined\') {';
159 $javaScriptCode[] = ' TYPO3.lang = {}';
160 $javaScriptCode[] = '}';
161 $javaScriptCode[] = 'var additionalInlineLanguageLabels = ' . json_encode($labels) . ';';
162 $javaScriptCode[] = 'for (var attributeName in additionalInlineLanguageLabels) {';
163 $javaScriptCode[] = ' if (typeof TYPO3.lang[attributeName] === \'undefined\') {';
164 $javaScriptCode[] = ' TYPO3.lang[attributeName] = additionalInlineLanguageLabels[attributeName]';
165 $javaScriptCode[] = ' }';
166 $javaScriptCode[] = '}';
167
168 $jsonResult['scriptCall'][] = implode(LF, $javaScriptCode);
169 }
170
171 $requireJsModule = $this->createExecutableStringRepresentationOfRegisteredRequireJsModules($newContainerResult);
172 $jsonResult['scriptCall'] = array_merge($requireJsModule, $jsonResult['scriptCall']);
173
174 $response->getBody()->write(json_encode($jsonResult));
175
176 return $response;
177 }
178 }