eee0d931519ffe0d4421e0ca04953a0614534855
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Controller / SelectTreeController.php
1 <?php
2 namespace TYPO3\CMS\Backend\Controller;
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 Psr\Http\Message\ResponseInterface;
18 use Psr\Http\Message\ServerRequestInterface;
19 use TYPO3\CMS\Backend\Form\FormDataCompiler;
20 use TYPO3\CMS\Backend\Form\FormDataGroup\TcaDatabaseRecord;
21 use TYPO3\CMS\Core\Configuration\FlexForm\FlexFormTools;
22 use TYPO3\CMS\Core\Utility\GeneralUtility;
23
24 /**
25 * Backend controller for selectTree ajax operations
26 */
27 class SelectTreeController
28 {
29 /**
30 * Returns json representing category tree
31 *
32 * @param ServerRequestInterface $request
33 * @param ResponseInterface $response
34 * @return ResponseInterface
35 */
36 public function fetchDataAction(ServerRequestInterface $request, ResponseInterface $response)
37 {
38 $tableName = $request->getQueryParams()['table'];
39 $fieldName = $request->getQueryParams()['field'];
40
41 // Prepare processedTca: Remove all column definitions except the one that contains
42 // our tree definition. This way only this field is calculated, everything else is ignored.
43 if (!isset($GLOBALS['TCA'][$tableName]) || !is_array($GLOBALS['TCA'][$tableName])) {
44 throw new \RuntimeException(
45 'TCA for table ' . $tableName . ' not found',
46 1479386729
47 );
48 }
49 $processedTca = $GLOBALS['TCA'][$tableName];
50 if (!isset($processedTca['columns'][$fieldName]) || !is_array($processedTca['columns'][$fieldName])) {
51 throw new \RuntimeException(
52 'TCA for table ' . $tableName . ' and field ' . $fieldName . ' not found',
53 1479386990
54 );
55 }
56
57 // Force given record type and set showitem to our field only
58 $recordTypeValue = $request->getQueryParams()['record_type_value'];
59 $processedTca['types'][$recordTypeValue]['showitem'] = $fieldName;
60 // Unset all columns except our field
61 $processedTca['columns'] = [
62 $fieldName => $processedTca['columns'][$fieldName],
63 ];
64
65 $flexFormPath = [];
66 if ($processedTca['columns'][$fieldName]['config']['type'] === 'flex') {
67 $flexFormTools = GeneralUtility::makeInstance(FlexFormTools::class);
68 $dataStructureIdentifier = json_encode($request->getQueryParams()['flex_form_datastructure_identifier']);
69 $dataStructure = $flexFormTools->parseDataStructureByIdentifier($dataStructureIdentifier);
70 // Try to reduce given data structure down to the relevant element only
71 $flexFormPath = $request->getQueryParams()['flex_form_path'];
72 $fieldPattern = 'data[' . $tableName . '][';
73 $flexFormPath = str_replace($fieldPattern, '', $flexFormPath);
74 $flexFormPath = substr($flexFormPath, 0, -1);
75 $flexFormPath = explode('][', $flexFormPath);
76 if (isset($dataStructure['sheets'][$flexFormPath[3]]['ROOT']['el'][$flexFormPath[5]])) {
77 $dataStructure = [
78 'sheets' => [
79 $flexFormPath[3] => [
80 'ROOT' => [
81 'type' => 'array',
82 'el' => [
83 $flexFormPath[5] => $dataStructure['sheets'][$flexFormPath[3]]['ROOT']['el'][$flexFormPath[5]],
84 ],
85 ],
86 ],
87 ],
88 ];
89 }
90 $processedTca['columns'][$fieldName]['config']['ds'] = $dataStructure;
91 $processedTca['columns'][$fieldName]['config']['dataStructureIdentifier'] = $dataStructureIdentifier;
92 }
93
94 $formDataGroup = GeneralUtility::makeInstance(TcaDatabaseRecord::class);
95 $formDataCompiler = GeneralUtility::makeInstance(FormDataCompiler::class, $formDataGroup);
96 $formDataCompilerInput = [
97 'tableName' => $request->getQueryParams()['table'],
98 'vanillaUid' => (int)$request->getQueryParams()['uid'],
99 'command' => $request->getQueryParams()['command'],
100 'processedTca' => $processedTca,
101 'recordTypeValue' => $recordTypeValue,
102 'selectTreeCompileItems' => true,
103 ];
104 $formData = $formDataCompiler->compile($formDataCompilerInput);
105
106 if ($formData['processedTca']['columns'][$fieldName]['config']['type'] === 'flex') {
107 $treeData = $formData['processedTca']['columns'][$fieldName]['config']['ds']
108 ['sheets'][$flexFormPath[3]]['ROOT']['el'][$flexFormPath[5]]['config']['items'];
109 } else {
110 $treeData = $formData['processedTca']['columns'][$fieldName]['config']['items'];
111 }
112
113 $json = json_encode($treeData);
114 $response->getBody()->write($json);
115 return $response;
116 }
117 }