TYPO3 CMS  TYPO3_8-7
FormFlexAjaxController.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
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 
27 
32 {
40  public function containerAdd(ServerRequestInterface $request, ResponseInterface $response): 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 redifinition 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  if (!empty($newContainerResult['additionalJavaScriptSubmit'])) {
148  $additionalJavaScriptSubmit = implode('', $newContainerResult['additionalJavaScriptSubmit']);
149  $additionalJavaScriptSubmit = str_replace([CR, LF], '', $additionalJavaScriptSubmit);
150  $jsonResult['scriptCall'][] = 'TBE_EDITOR.addActionChecks("submit", "' . addslashes($additionalJavaScriptSubmit) . '");';
151  }
152  foreach ($newContainerResult['additionalJavaScriptPost'] as $singleAdditionalJavaScriptPost) {
153  $jsonResult['scriptCall'][] = $singleAdditionalJavaScriptPost;
154  }
155  foreach ($newContainerResult['stylesheetFiles'] as $stylesheetFile) {
156  $jsonResult['stylesheetFiles'][] = $this->getRelativePathToStylesheetFile($stylesheetFile);
157  }
158  if (!empty($newContainerResult['additionalInlineLanguageLabelFiles'])) {
159  $labels = [];
160  foreach ($newContainerResult['additionalInlineLanguageLabelFiles'] as $additionalInlineLanguageLabelFile) {
162  $labels,
163  $this->getLabelsFromLocalizationFile($additionalInlineLanguageLabelFile)
164  );
165  }
166  $javaScriptCode = [];
167  $javaScriptCode[] = 'if (typeof TYPO3 === \'undefined\' || typeof TYPO3.lang === \'undefined\') {';
168  $javaScriptCode[] = ' TYPO3.lang = {}';
169  $javaScriptCode[] = '}';
170  $javaScriptCode[] = 'var additionalInlineLanguageLabels = ' . json_encode($labels) . ';';
171  $javaScriptCode[] = 'for (var attributeName in additionalInlineLanguageLabels) {';
172  $javaScriptCode[] = ' if (typeof TYPO3.lang[attributeName] === \'undefined\') {';
173  $javaScriptCode[] = ' TYPO3.lang[attributeName] = additionalInlineLanguageLabels[attributeName]';
174  $javaScriptCode[] = ' }';
175  $javaScriptCode[] = '}';
176 
177  $jsonResult['scriptCall'][] = implode(LF, $javaScriptCode);
178  }
179 
180  $requireJsModule = $this->createExecutableStringRepresentationOfRegisteredRequireJsModules($newContainerResult);
181  $jsonResult['scriptCall'] = array_merge($requireJsModule, $jsonResult['scriptCall']);
182 
183  $response->getBody()->write(json_encode($jsonResult));
184 
185  return $response;
186  }
187 }
containerAdd(ServerRequestInterface $request, ResponseInterface $response)
static makeInstance($className,... $constructorArguments)
static mergeRecursiveWithOverrule(array &$original, array $overrule, $addKeys=true, $includeEmptyValues=true, $enableUnsetFeature=true)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']