[!!!][TASK] Improve flex and TCA handling in FormEngine
[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\GeneralUtility;
25 use TYPO3\CMS\Core\Utility\StringUtility;
26
27 /**
28 * Handle FormEngine flex field ajax calls
29 */
30 class FormFlexAjaxController
31 {
32 /**
33 * Render a single flex form section container to add it to the DOM
34 *
35 * @param ServerRequestInterface $request
36 * @param ResponseInterface $response
37 * @return ResponseInterface
38 */
39 public function containerAdd(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
40 {
41 $queryParameters = $request->getParsedBody();
42
43 $vanillaUid = (int)$queryParameters['vanillaUid'];
44 $databaseRowUid = $queryParameters['databaseRowUid'];
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 'databaseRow' => [
75 'uid' => $databaseRowUid,
76 ],
77 'command' => $command,
78 'recordTypeValue' => $recordTypeValue,
79 'processedTca' => $processedTca,
80 'flexSectionContainerPreparation' => $flexSectionContainerPreparation,
81 ];
82 $formData = $formDataCompiler->compile($formDataCompilerInput);
83
84 $dataStructure = $formData['processedTca']['columns'][$fieldName]['config']['ds'];
85 $formData['fieldName'] = $fieldName;
86 $formData['flexFormDataStructureArray'] = $dataStructure['sheets'][$flexFormSheetName]['ROOT']['el'][$flexFormFieldName]['children'][$flexFormContainerIdentifier];
87 $formData['flexFormDataStructureIdentifier'] = $dataStructureIdentifier;
88 $formData['flexFormFieldName'] = $flexFormFieldName;
89 $formData['flexFormSheetName'] = $flexFormSheetName;
90 $formData['flexFormContainerName'] = $flexFormContainerName;
91 $formData['flexFormContainerIdentifier'] = $flexFormContainerIdentifier;
92 $formData['flexFormContainerElementCollapsed'] = false;
93
94 $formData['flexFormFormPrefix'] = '[data][' . $flexFormSheetName . '][lDEF]' . '[' . $flexFormFieldName . ']' . '[el]';
95 $formData['parameterArray']['itemFormElName'] = 'data[' . $tableName . '][' . $formData['databaseRow']['uid'] . '][' . $fieldName . ']';
96
97 // JavaScript code for event handlers:
98 // @todo: see if we can get rid of this - used in group elements, and also for the "reload" on type field changes
99 $formData['parameterArray']['fieldChangeFunc'] = [];
100 $formData['parameterArray']['fieldChangeFunc']['TBE_EDITOR_fieldChanged'] = 'TBE_EDITOR.fieldChanged('
101 . GeneralUtility::quoteJSvalue($tableName)
102 . ',' . GeneralUtility::quoteJSvalue($formData['databaseRow']['uid'])
103 . ',' . GeneralUtility::quoteJSvalue($fieldName)
104 . ',' . GeneralUtility::quoteJSvalue($formData['parameterArray']['itemFormElName'])
105 . ');';
106
107 // @todo: check GroupElement for usage of elementBaseName ... maybe kick that thing?
108
109 // Feed resulting form data to container structure to render HTML and other result data
110 $nodeFactory = GeneralUtility::makeInstance(NodeFactory::class);
111 $formData['renderType'] = 'flexFormContainerContainer';
112 $newContainerResult = $nodeFactory->create($formData)->render();
113
114 $jsonResult = [
115 'html' => $newContainerResult['html'],
116 'scriptCall' => [],
117 ];
118
119 if (!empty($newContainerResult['additionalJavaScriptSubmit'])) {
120 $additionalJavaScriptSubmit = implode('', $newContainerResult['additionalJavaScriptSubmit']);
121 $additionalJavaScriptSubmit = str_replace([CR, LF], '', $additionalJavaScriptSubmit);
122 $jsonResult['scriptCall'][] = 'TBE_EDITOR.addActionChecks("submit", "' . addslashes($additionalJavaScriptSubmit) . '");';
123 }
124 foreach ($newContainerResult['additionalJavaScriptPost'] as $singleAdditionalJavaScriptPost) {
125 $jsonResult['scriptCall'][] = $singleAdditionalJavaScriptPost;
126 }
127 // @todo: handle stylesheetFiles, additionalInlineLanguageLabelFiles
128
129 // @todo: copied from inline ajax handler - maybe extract to some abstract?
130 if (!empty($newContainerResult['requireJsModules'])) {
131 foreach ($newContainerResult['requireJsModules'] as $module) {
132 $moduleName = null;
133 $callback = null;
134 if (is_string($module)) {
135 // if $module is a string, no callback
136 $moduleName = $module;
137 $callback = null;
138 } elseif (is_array($module)) {
139 // if $module is an array, callback is possible
140 foreach ($module as $key => $value) {
141 $moduleName = $key;
142 $callback = $value;
143 break;
144 }
145 }
146 if ($moduleName !== null) {
147 $inlineCodeKey = $moduleName;
148 $javaScriptCode = 'require(["' . $moduleName . '"]';
149 if ($callback !== null) {
150 $inlineCodeKey .= sha1($callback);
151 $javaScriptCode .= ', ' . $callback;
152 }
153 $javaScriptCode .= ');';
154 $jsonResult['scriptCall'][] = '/*RequireJS-Module-' . $inlineCodeKey . '*/' . LF . $javaScriptCode;
155 }
156 }
157 }
158
159 $response->getBody()->write(json_encode($jsonResult));
160
161 return $response;
162 }
163 }