[BUGFIX] Fix several typos in php comments
[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\Http\JsonResponse;
25 use TYPO3\CMS\Core\Utility\ArrayUtility;
26 use TYPO3\CMS\Core\Utility\GeneralUtility;
27 use TYPO3\CMS\Core\Utility\StringUtility;
28
29 /**
30 * Handle FormEngine flex field ajax calls
31 */
32 class FormFlexAjaxController extends AbstractFormEngineAjaxController
33 {
34 /**
35 * Render a single flex form section container to add it to the DOM
36 *
37 * @param ServerRequestInterface $request
38 * @return ResponseInterface
39 */
40 public function containerAdd(ServerRequestInterface $request): ResponseInterface
41 {
42 $queryParameters = $request->getParsedBody();
43
44 $vanillaUid = (int)$queryParameters['vanillaUid'];
45 $databaseRowUid = $queryParameters['databaseRowUid'];
46 $command = $queryParameters['command'];
47 $tableName = $queryParameters['tableName'];
48 $fieldName = $queryParameters['fieldName'];
49 $recordTypeValue = $queryParameters['recordTypeValue'];
50 $dataStructureIdentifier = json_encode($queryParameters['dataStructureIdentifier']);
51 $flexFormSheetName = $queryParameters['flexFormSheetName'];
52 $flexFormFieldName = $queryParameters['flexFormFieldName'];
53 $flexFormContainerName = $queryParameters['flexFormContainerName'];
54
55 // Prepare TCA and data values for a new section container using data providers
56 $processedTca = $GLOBALS['TCA'][$tableName];
57 $flexFormTools = GeneralUtility::makeInstance(FlexFormTools::class);
58 $dataStructure = $flexFormTools->parseDataStructureByIdentifier($dataStructureIdentifier);
59 $processedTca['columns'][$fieldName]['config']['ds'] = $dataStructure;
60 $processedTca['columns'][$fieldName]['config']['dataStructureIdentifier'] = $dataStructureIdentifier;
61 // Get a new unique id for this container.
62 $flexFormContainerIdentifier = StringUtility::getUniqueId();
63 $flexSectionContainerPreparation = [
64 'flexFormSheetName' => $flexFormSheetName,
65 'flexFormFieldName' => $flexFormFieldName,
66 'flexFormContainerName' => $flexFormContainerName,
67 'flexFormContainerIdentifier' => $flexFormContainerIdentifier,
68 ];
69
70 $formDataGroup = GeneralUtility::makeInstance(TcaDatabaseRecord::class);
71 $formDataCompiler = GeneralUtility::makeInstance(FormDataCompiler::class, $formDataGroup);
72 $formDataCompilerInput = [
73 'tableName' => $tableName,
74 'vanillaUid' => (int)$vanillaUid,
75 'command' => $command,
76 'recordTypeValue' => $recordTypeValue,
77 'processedTca' => $processedTca,
78 'flexSectionContainerPreparation' => $flexSectionContainerPreparation,
79 ];
80 // A new container on a new record needs the 'NEW123' uid here, see comment
81 // in DatabaseUniqueUidNewRow for more information on that.
82 // @todo: Resolve, maybe with a redefinition of vanillaUid to transport the information more clean through this var?
83 // @see issue #80100 for a series of changes in this area
84 if ($command === 'new') {
85 $formDataCompilerInput['databaseRow']['uid'] = $databaseRowUid;
86 }
87 $formData = $formDataCompiler->compile($formDataCompilerInput);
88
89 $dataStructure = $formData['processedTca']['columns'][$fieldName]['config']['ds'];
90 $formData['fieldName'] = $fieldName;
91 $formData['flexFormDataStructureArray'] = $dataStructure['sheets'][$flexFormSheetName]['ROOT']['el'][$flexFormFieldName]['children'][$flexFormContainerIdentifier];
92 $formData['flexFormDataStructureIdentifier'] = $dataStructureIdentifier;
93 $formData['flexFormFieldName'] = $flexFormFieldName;
94 $formData['flexFormSheetName'] = $flexFormSheetName;
95 $formData['flexFormContainerName'] = $flexFormContainerName;
96 $formData['flexFormContainerIdentifier'] = $flexFormContainerIdentifier;
97 $formData['flexFormContainerElementCollapsed'] = false;
98
99 $formData['flexFormFormPrefix'] = '[data][' . $flexFormSheetName . '][lDEF][' . $flexFormFieldName . '][el]';
100
101 // Set initialized data of that section container from compiler to the array part used
102 // by flexFormElementContainer which prepares parameterArray. Important for initialized
103 // values of group element.
104 if (isset($formData['databaseRow'][$fieldName]
105 ['data'][$flexFormSheetName]
106 ['lDEF'][$flexFormFieldName]
107 ['el'][$flexFormContainerIdentifier][$flexFormContainerName]['el']
108 )
109 && is_array(
110 $formData['databaseRow'][$fieldName]
111 ['data'][$flexFormSheetName]
112 ['lDEF'][$flexFormFieldName]
113 ['el'][$flexFormContainerIdentifier][$flexFormContainerName]['el']
114 )
115 ) {
116 $formData['flexFormRowData'] = $formData['databaseRow'][$fieldName]
117 ['data'][$flexFormSheetName]
118 ['lDEF'][$flexFormFieldName]
119 ['el'][$flexFormContainerIdentifier][$flexFormContainerName]['el'];
120 }
121
122 $formData['parameterArray']['itemFormElName'] = 'data[' . $tableName . '][' . $formData['databaseRow']['uid'] . '][' . $fieldName . ']';
123
124 // JavaScript code for event handlers:
125 // @todo: see if we can get rid of this - used in group elements, and also for the "reload" on type field changes
126 $formData['parameterArray']['fieldChangeFunc'] = [];
127 $formData['parameterArray']['fieldChangeFunc']['TBE_EDITOR_fieldChanged'] = 'TBE_EDITOR.fieldChanged('
128 . GeneralUtility::quoteJSvalue($tableName)
129 . ',' . GeneralUtility::quoteJSvalue($formData['databaseRow']['uid'])
130 . ',' . GeneralUtility::quoteJSvalue($fieldName)
131 . ',' . GeneralUtility::quoteJSvalue($formData['parameterArray']['itemFormElName'])
132 . ');';
133
134 // @todo: check GroupElement for usage of elementBaseName ... maybe kick that thing?
135
136 // Feed resulting form data to container structure to render HTML and other result data
137 $nodeFactory = GeneralUtility::makeInstance(NodeFactory::class);
138 $formData['renderType'] = 'flexFormContainerContainer';
139 $newContainerResult = $nodeFactory->create($formData)->render();
140
141 $jsonResult = [
142 'html' => $newContainerResult['html'],
143 'stylesheetFiles' => [],
144 'scriptCall' => [],
145 ];
146
147 foreach ($newContainerResult['additionalJavaScriptPost'] as $singleAdditionalJavaScriptPost) {
148 $jsonResult['scriptCall'][] = $singleAdditionalJavaScriptPost;
149 }
150 foreach ($newContainerResult['stylesheetFiles'] as $stylesheetFile) {
151 $jsonResult['stylesheetFiles'][] = $this->getRelativePathToStylesheetFile($stylesheetFile);
152 }
153 if (!empty($newContainerResult['additionalInlineLanguageLabelFiles'])) {
154 $labels = [];
155 foreach ($newContainerResult['additionalInlineLanguageLabelFiles'] as $additionalInlineLanguageLabelFile) {
156 ArrayUtility::mergeRecursiveWithOverrule(
157 $labels,
158 $this->getLabelsFromLocalizationFile($additionalInlineLanguageLabelFile)
159 );
160 }
161 $javaScriptCode = [];
162 $javaScriptCode[] = 'if (typeof TYPO3 === \'undefined\' || typeof TYPO3.lang === \'undefined\') {';
163 $javaScriptCode[] = ' TYPO3.lang = {}';
164 $javaScriptCode[] = '}';
165 $javaScriptCode[] = 'var additionalInlineLanguageLabels = ' . json_encode($labels) . ';';
166 $javaScriptCode[] = 'for (var attributeName in additionalInlineLanguageLabels) {';
167 $javaScriptCode[] = ' if (typeof TYPO3.lang[attributeName] === \'undefined\') {';
168 $javaScriptCode[] = ' TYPO3.lang[attributeName] = additionalInlineLanguageLabels[attributeName]';
169 $javaScriptCode[] = ' }';
170 $javaScriptCode[] = '}';
171
172 $jsonResult['scriptCall'][] = implode(LF, $javaScriptCode);
173 }
174
175 $requireJsModule = $this->createExecutableStringRepresentationOfRegisteredRequireJsModules($newContainerResult);
176 $jsonResult['scriptCall'] = array_merge($requireJsModule, $jsonResult['scriptCall']);
177
178 return new JsonResponse($jsonResult);
179 }
180 }