[TASK] Move LanguageService to EXT:core
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / FormDataProvider / TcaGroup.php
1 <?php
2 namespace TYPO3\CMS\Backend\Form\FormDataProvider;
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 TYPO3\CMS\Backend\Clipboard\Clipboard;
18 use TYPO3\CMS\Backend\Form\FormDataProviderInterface;
19 use TYPO3\CMS\Backend\Utility\BackendUtility;
20 use TYPO3\CMS\Core\Database\RelationHandler;
21 use TYPO3\CMS\Core\Resource\Folder;
22 use TYPO3\CMS\Core\Resource\ResourceFactory;
23 use TYPO3\CMS\Core\Utility\GeneralUtility;
24 use TYPO3\CMS\Core\Utility\MathUtility;
25
26 /**
27 * Resolve databaseRow field content to the real connected rows for type=group
28 */
29 class TcaGroup implements FormDataProviderInterface
30 {
31 /**
32 * Initialize new row with default values from various sources
33 *
34 * @param array $result
35 * @return array
36 * @throws \UnexpectedValueException
37 * @throws \RuntimeException
38 */
39 public function addData(array $result)
40 {
41 foreach ($result['processedTca']['columns'] as $fieldName => $fieldConfig) {
42 if (empty($fieldConfig['config']['type'])
43 || $fieldConfig['config']['type'] !== 'group'
44 || empty($fieldConfig['config']['internal_type'])
45 ) {
46 continue;
47 }
48
49 // Sanitize max items, set to 99999 if not defined
50 $result['processedTca']['columns'][$fieldName]['config']['maxitems'] = MathUtility::forceIntegerInRange(
51 $fieldConfig['config']['maxitems'], 0, 99999
52 );
53 if ($result['processedTca']['columns'][$fieldName]['config']['maxitems'] === 0) {
54 $result['processedTca']['columns'][$fieldName]['config']['maxitems'] = 99999;
55 }
56
57 $databaseRowFieldContent = '';
58 if (!empty($result['databaseRow'][$fieldName])) {
59 $databaseRowFieldContent = (string)$result['databaseRow'][$fieldName];
60 }
61
62 $items = [];
63 $sanitizedClipboardElements = [];
64 $internalType = $fieldConfig['config']['internal_type'];
65
66 if ($internalType === 'file_reference' || $internalType === 'file') {
67 // Set 'allowed' config to "*" if not set
68 if (empty($fieldConfig['config']['allowed'])) {
69 $result['processedTca']['columns'][$fieldName]['config']['allowed'] = '*';
70 }
71 // Force uploadFolder for file_reference type
72 if ($internalType === 'file_reference') {
73 $config['uploadfolder'] = '';
74 }
75
76 // Simple list of files
77 $fileList = GeneralUtility::trimExplode(',', $databaseRowFieldContent, true);
78 $fileFactory = ResourceFactory::getInstance();
79 foreach ($fileList as $uidOrPath) {
80 $item = [
81 'uidOrPath' => $uidOrPath,
82 ];
83 if (MathUtility::canBeInterpretedAsInteger($uidOrPath)) {
84 $fileObject = $fileFactory->getFileObject($uidOrPath);
85 $item['title'] = $fileObject->getName();
86 } else {
87 $item['title'] = $uidOrPath;
88 }
89 $items[] = $item;
90 }
91
92 // Register elements from clipboard
93 $allowed = GeneralUtility::trimExplode(',', $result['processedTca']['columns'][$fieldName]['config']['allowed'], true);
94 $clipboard = GeneralUtility::makeInstance(Clipboard::class);
95 $clipboard->initializeClipboard();
96 $clipboardElements = $clipboard->elFromTable('_FILE');
97 if ($allowed[0] !== '*') {
98 // If there are a set of allowed extensions, filter the content
99 foreach ($clipboardElements as $elementValue) {
100 $pathInfo = pathinfo($elementValue);
101 if (in_array(strtolower($pathInfo['extension']), $allowed)) {
102 $sanitizedClipboardElements[] = [
103 'title' => $elementValue,
104 'value' => $elementValue,
105 ];
106 }
107 }
108 } else {
109 // If all is allowed, insert all. This does NOT respect any disallowed extensions,
110 // but those will be filtered away by the DataHandler
111 foreach ($clipboardElements as $elementValue) {
112 $sanitizedClipboardElements[] = [
113 'title' => $elementValue,
114 'value' => $elementValue,
115 ];
116 }
117 }
118 } elseif ($internalType === 'db') {
119 if (empty($fieldConfig['config']['allowed'])) {
120 throw new \RuntimeException(
121 'Mandatory TCA config setting "allowed" missing in field "' . $fieldName . '" of table "' . $result['tableName'] . '"',
122 1482250512
123 );
124 }
125
126 $relationHandler = GeneralUtility::makeInstance(RelationHandler::class);
127 $relationHandler->start(
128 $databaseRowFieldContent,
129 $fieldConfig['config']['allowed'],
130 $fieldConfig['config']['MM'],
131 $result['databaseRow']['uid'],
132 $result['tableName'],
133 $fieldConfig['config']
134 );
135 $relationHandler->getFromDB();
136 $relations = $relationHandler->getResolvedItemArray();
137 foreach ($relations as $relation) {
138 $tableName = $relation['table'];
139 $uid = $relation['uid'];
140 $record = BackendUtility::getRecordWSOL($tableName, $uid);
141 $title = BackendUtility::getRecordTitle($tableName, $record, false, false);
142 $items[] = [
143 'table' => $tableName,
144 'uid' => $record['uid'],
145 'title' => $title,
146 'row' => $record,
147 ];
148 }
149
150 // Register elements from clipboard
151 $allowed = GeneralUtility::trimExplode(',', $fieldConfig['config']['allowed'], true);
152 $clipboard = GeneralUtility::makeInstance(Clipboard::class);
153 $clipboard->initializeClipboard();
154 if ($allowed[0] !== '*') {
155 // Only some tables, filter them:
156 foreach ($allowed as $tablename) {
157 $elementValue = key($clipboard->elFromTable($tablename));
158 if ($elementValue) {
159 list($elementTable, $elementUid) = explode('|', $elementValue);
160 $record = BackendUtility::getRecordWSOL($elementTable, $elementUid);
161 $sanitizedClipboardElements[] = [
162 'title' => BackendUtility::getRecordTitle($elementTable, $record),
163 'value' => $elementTable . '_' . $elementUid,
164 ];
165 }
166 }
167 } else {
168 // All tables allowed for relation:
169 $clipboardElements = array_keys($clipboard->elFromTable(''));
170 foreach ($clipboardElements as $elementValue) {
171 list($elementTable, $elementUid) = explode('|', $elementValue);
172 $record = BackendUtility::getRecordWSOL($elementTable, $elementUid);
173 $sanitizedClipboardElements[] = [
174 'title' => BackendUtility::getRecordTitle($elementTable, $record),
175 'value' => $elementTable . '_' . $elementUid,
176 ];
177 }
178 }
179 } elseif ($internalType === 'folder') {
180 // Simple list of folders
181 $folderList = GeneralUtility::trimExplode(',', $databaseRowFieldContent, true);
182 foreach ($folderList as $folder) {
183 if ($folder) {
184 $folderObject = ResourceFactory::getInstance()->retrieveFileOrFolderObject($folder);
185 if ($folderObject instanceof Folder) {
186 $items[] = [
187 'folder' => $folder,
188 ];
189 }
190 }
191 }
192 } else {
193 throw new \UnexpectedValueException(
194 'TCA internal_type of field "' . $fieldName . '" in table ' . $result['tableName']
195 . ' must be set to either "db", "file" or "file_reference"',
196 1438780511
197 );
198 }
199
200 $result['databaseRow'][$fieldName] = $items;
201 $result['processedTca']['columns'][$fieldName]['config']['clipboardElements'] = $sanitizedClipboardElements;
202 }
203
204 return $result;
205 }
206 }