[!!!][TASK] Remove deprecated code from DatabaseRowInitializeNew
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / FormDataProvider / DatabaseRowInitializeNew.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\Form\FormDataProviderInterface;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19 use TYPO3\CMS\Core\Utility\MathUtility;
20
21 /**
22 * On "new" command, initialize new database row with default data
23 */
24 class DatabaseRowInitializeNew implements FormDataProviderInterface
25 {
26 /**
27 * Initialize new row with default values from various sources
28 * There are 4 sources of default values. Mind the order, the last takes precedence.
29 *
30 * @param array $result
31 * @return array
32 * @throws \UnexpectedValueException
33 */
34 public function addData(array $result)
35 {
36 if ($result['command'] !== 'new') {
37 return $result;
38 }
39 if (!is_array($result['databaseRow'])) {
40 throw new \UnexpectedValueException(
41 'databaseRow of table ' . $result['tableName'] . ' is not an array',
42 1444431128
43 );
44 }
45
46 $result = $this->setDefaultsFromUserTsConfig($result);
47 $result = $this->setDefaultsFromPageTsConfig($result);
48 $result = $this->setDefaultsFromNeighborRow($result);
49 $result = $this->setDefaultsFromDefaultValues($result);
50 $result = $this->setDefaultsFromInlineRelations($result);
51 $result = $this->setDefaultsFromInlineParentLanguage($result);
52 $result = $this->setPid($result);
53
54 return $result;
55 }
56
57 /**
58 * Set defaults defined by user ts "TCAdefaults"
59 *
60 * @param array $result Result array
61 * @return array Modified result array
62 */
63 protected function setDefaultsFromUserTsConfig(array $result)
64 {
65 $tableNameWithDot = $result['tableName'] . '.';
66 // Apply default values from user typo script "TCAdefaults" if any
67 if (isset($result['userTsConfig']['TCAdefaults.'][$tableNameWithDot])
68 && is_array($result['userTsConfig']['TCAdefaults.'][$tableNameWithDot])
69 ) {
70 foreach ($result['userTsConfig']['TCAdefaults.'][$tableNameWithDot] as $fieldName => $fieldValue) {
71 if (isset($result['processedTca']['columns'][$fieldName])) {
72 $result['databaseRow'][$fieldName] = $fieldValue;
73 }
74 }
75 }
76 return $result;
77 }
78
79 /**
80 * Set defaults defined by page ts "TCAdefaults"
81 *
82 * @param array $result Result array
83 * @return array Modified result array
84 */
85 protected function setDefaultsFromPageTsConfig(array $result)
86 {
87 $tableNameWithDot = $result['tableName'] . '.';
88 if (isset($result['pageTsConfig']['TCAdefaults.'][$tableNameWithDot])
89 && is_array($result['pageTsConfig']['TCAdefaults.'][$tableNameWithDot])
90 ) {
91 foreach ($result['pageTsConfig']['TCAdefaults.'][$tableNameWithDot] as $fieldName => $fieldValue) {
92 if (isset($result['processedTca']['columns'][$fieldName])) {
93 $result['databaseRow'][$fieldName] = $fieldValue;
94 }
95 }
96 }
97 return $result;
98 }
99
100 /**
101 * If a neighbor row is given (if vanillaUid was negative), field can be initialized with values
102 * from neighbor for fields registered in TCA['ctrl']['useColumnsForDefaultValues'].
103 *
104 * @param array $result Result array
105 * @return array Modified result array
106 */
107 protected function setDefaultsFromNeighborRow(array $result)
108 {
109 if (is_array($result['neighborRow'])
110 && !empty($result['processedTca']['ctrl']['useColumnsForDefaultValues'])
111 ) {
112 $defaultColumns = GeneralUtility::trimExplode(',', $result['processedTca']['ctrl']['useColumnsForDefaultValues'], true);
113 foreach ($defaultColumns as $fieldName) {
114 if (isset($result['processedTca']['columns'][$fieldName])
115 && isset($result['neighborRow'][$fieldName])
116 ) {
117 $result['databaseRow'][$fieldName] = $result['neighborRow'][$fieldName];
118 }
119 }
120 }
121 return $result;
122 }
123
124 /**
125 * Apply default values.
126 * These are typically carried around as "defVals" GET vars and set by controllers
127 * in $result['defaultValues'] array as init values.
128 *
129 * @param array $result Result array
130 * @return array Modified result array
131 */
132 protected function setDefaultsFromDefaultValues(array $result)
133 {
134 $tableName = $result['tableName'];
135 $defaultValues = $result['defaultValues'] ?? [];
136 if (isset($defaultValues[$tableName]) && is_array($defaultValues[$tableName])) {
137 foreach ($defaultValues[$tableName] as $fieldName => $fieldValue) {
138 if (isset($result['processedTca']['columns'][$fieldName])) {
139 $result['databaseRow'][$fieldName] = $fieldValue;
140 }
141 }
142 }
143 return $result;
144 }
145
146 /**
147 * Inline scenario if a new intermediate record to an existing child-child is
148 * compiled. Set "foreign_selector" field of this intermediate row to given
149 * "childChildUid". See TcaDataCompiler array comment of inlineChildChildUid
150 * for more details.
151 *
152 * @param array $result Result array
153 * @return array Modified result array
154 * @throws \UnexpectedValueException
155 */
156 protected function setDefaultsFromInlineRelations(array $result)
157 {
158 if ($result['inlineChildChildUid'] === null) {
159 return $result;
160 }
161 if (!is_int($result['inlineChildChildUid'])) {
162 throw new \UnexpectedValueException(
163 'An inlineChildChildUid is given for table ' . $result['tableName'] . ', but is not an integer',
164 1444434103
165 );
166 }
167 if (!isset($result['inlineParentConfig']['foreign_selector'])) {
168 throw new \UnexpectedValueException(
169 'An inlineChildChildUid is given for table ' . $result['tableName'] . ', but no foreign_selector in inlineParentConfig',
170 1444434102
171 );
172 }
173 $selectorFieldName = $result['inlineParentConfig']['foreign_selector'];
174 if (!isset($result['processedTca']['columns'][$selectorFieldName]['config']['type'])
175 || (
176 $result['processedTca']['columns'][$selectorFieldName]['config']['type'] !== 'select'
177 && $result['processedTca']['columns'][$selectorFieldName]['config']['type'] !== 'group'
178 )
179 ) {
180 throw new \UnexpectedValueException(
181 $selectorFieldName . ' is target type of a foreign_selector field to table ' . $result['tableName'] . ' and must be either a select or group type field',
182 1444434104
183 );
184 }
185
186 if ($result['inlineChildChildUid']) {
187 $result['databaseRow'][$selectorFieldName] = $result['inlineChildChildUid'];
188 }
189
190 return $result;
191 }
192
193 /**
194 * If a new child is created in an inline relation via ajax, and if the parent is a localized record,
195 * the child should have the same sys_language_uid set in the field declared in ['ctrl']['languageField']
196 * if the child is localizable itself.
197 * A localized parent transfers its sys_language_uid via inlineParentConfig['inline']['parentSysLanguageUid'],
198 * use that value as default for the child record languageField.
199 *
200 * @param array $result Result array
201 * @return array Modified result array
202 * @throws \UnexpectedValueException
203 */
204 protected function setDefaultsFromInlineParentLanguage(array $result): array
205 {
206 if (!isset($result['inlineParentConfig']['inline']['parentSysLanguageUid'])
207 || empty($result['processedTca']['ctrl']['languageField'])
208 || empty($result['processedTca']['ctrl']['transOrigPointerField'])
209 ) {
210 return $result;
211 }
212
213 if (!MathUtility::canBeInterpretedAsInteger($result['inlineParentConfig']['inline']['parentSysLanguageUid'])) {
214 throw new \UnexpectedValueException(
215 'A sys_language_uid is set from inline parent config but the value is no integer',
216 1490360772
217 );
218 }
219 $parentSysLanguageUid = (int)$result['inlineParentConfig']['inline']['parentSysLanguageUid'];
220 $languageFieldName = $result['processedTca']['ctrl']['languageField'];
221 $result['databaseRow'][$languageFieldName] = $parentSysLanguageUid;
222
223 return $result;
224 }
225
226 /**
227 * Set the pid. This is either the vanillaUid (see description in FormDataCompiler),
228 * or a pid given by pageTsConfig for inline children.
229 *
230 * @param array $result Result array
231 * @return array Modified result array
232 * @throws \UnexpectedValueException
233 */
234 protected function setPid(array $result)
235 {
236 // Set pid to vanillaUid. This can be a negative value
237 // if the record is added relative to another record.
238 $result['databaseRow']['pid'] = $result['vanillaUid'];
239
240 // In case a new inline record is created, the pid can be set to a different value
241 // by pageTsConfig, but not by userTsConfig. This overrides the above pid selection
242 // and forces the pid of new inline children.
243 $tableNameWithDot = $result['tableName'] . '.';
244 if ($result['isInlineChild'] && isset($result['pageTsConfig']['TCAdefaults.'][$tableNameWithDot]['pid'])) {
245 if (!MathUtility::canBeInterpretedAsInteger($result['pageTsConfig']['TCAdefaults.'][$tableNameWithDot]['pid'])) {
246 throw new \UnexpectedValueException(
247 'page TSConfig setting TCAdefaults.' . $tableNameWithDot . 'pid must be a number, but given string '
248 . $result['pageTsConfig']['TCAdefaults.'][$tableNameWithDot]['pid'] . ' can not be interpreted as integer',
249 1461598332
250 );
251 }
252 $result['databaseRow']['pid'] = (int)$result['pageTsConfig']['TCAdefaults.'][$tableNameWithDot]['pid'];
253 }
254
255 return $result;
256 }
257 }