[TASK] Move LanguageService to EXT:core
[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->setDefaultsFromDevVals($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 from GET / POST
126 *
127 * @todo: Fetch this stuff from request object as soon as modules were moved to PSR-7,
128 * @todo: or hand values over via $result array, so the _GP access is transferred to
129 * @todo: controllers concern.
130 *
131 * @param array $result Result array
132 * @return array Modified result array
133 */
134 protected function setDefaultsFromDevVals(array $result)
135 {
136 $tableName = $result['tableName'];
137 $defaultValuesFromGetPost = GeneralUtility::_GP('defVals');
138 if (isset($defaultValuesFromGetPost[$tableName])
139 && is_array($defaultValuesFromGetPost[$tableName])
140 ) {
141 foreach ($defaultValuesFromGetPost[$tableName] as $fieldName => $fieldValue) {
142 if (isset($result['processedTca']['columns'][$fieldName])) {
143 $result['databaseRow'][$fieldName] = $fieldValue;
144 }
145 }
146 }
147 return $result;
148 }
149
150 /**
151 * Inline scenario if a new intermediate record to an existing child-child is
152 * compiled. Set "foreign_selector" field of this intermediate row to given
153 * "childChildUid". See TcaDataCompiler array comment of inlineChildChildUid
154 * for more details.
155 *
156 * @param array $result Result array
157 * @return array Modified result array
158 * @throws \UnexpectedValueException
159 */
160 protected function setDefaultsFromInlineRelations(array $result)
161 {
162 if ($result['inlineChildChildUid'] === null) {
163 return $result;
164 }
165 if (!is_int($result['inlineChildChildUid'])) {
166 throw new \UnexpectedValueException(
167 'An inlineChildChildUid is given for table ' . $result['tableName'] . ', but is not an integer',
168 1444434103
169 );
170 }
171 if (!isset($result['inlineParentConfig']['foreign_selector'])) {
172 throw new \UnexpectedValueException(
173 'An inlineChildChildUid is given for table ' . $result['tableName'] . ', but no foreign_selector in inlineParentConfig',
174 1444434102
175 );
176 }
177 $selectorFieldName = $result['inlineParentConfig']['foreign_selector'];
178 if (!isset($result['processedTca']['columns'][$selectorFieldName]['config']['type'])
179 || ($result['processedTca']['columns'][$selectorFieldName]['config']['type'] !== 'select'
180 && $result['processedTca']['columns'][$selectorFieldName]['config']['type'] !== 'group'
181 )
182 ) {
183 throw new \UnexpectedValueException(
184 $selectorFieldName . ' is target type of a foreign_selector field to table ' . $result['tableName'] . ' and must be either a select or group type field',
185 1444434104
186 );
187 }
188
189 if ($result['inlineChildChildUid']) {
190 $result['databaseRow'][$selectorFieldName] = $result['inlineChildChildUid'];
191 }
192
193 return $result;
194 }
195
196 /**
197 * If a new child is created in an inline relation via ajax, and if the parent is a localized record,
198 * the child should have the same sys_language_uid set in the field declared in ['ctrl']['languageField']
199 * if the child is localizable itself.
200 * A localized parent transfers its sys_language_uid via inlineParentConfig['inline']['parentSysLanguageUid'],
201 * use that value as default for the child record languageField.
202 *
203 * @param array $result Result array
204 * @return array Modified result array
205 * @throws \UnexpectedValueException
206 */
207 protected function setDefaultsFromInlineParentLanguage(array $result): array
208 {
209 if (!isset($result['inlineParentConfig']['inline']['parentSysLanguageUid'])
210 || empty($result['processedTca']['ctrl']['languageField'])
211 || empty($result['processedTca']['ctrl']['transOrigPointerField'])
212 ) {
213 return $result;
214 }
215
216 if (!MathUtility::canBeInterpretedAsInteger($result['inlineParentConfig']['inline']['parentSysLanguageUid'])) {
217 throw new \UnexpectedValueException(
218 'A sys_language_uid is set from inline parent config but the value is no integer',
219 1490360772
220 );
221 }
222 $parentSysLanguageUid = (int)$result['inlineParentConfig']['inline']['parentSysLanguageUid'];
223 $languageFieldName = $result['processedTca']['ctrl']['languageField'];
224 $result['databaseRow'][$languageFieldName] = $parentSysLanguageUid;
225
226 return $result;
227 }
228
229 /**
230 * Set the pid. This is either the vanillaUid (see description in FormDataCompiler),
231 * or a pid given by pageTsConfig for inline children.
232 *
233 * @param array $result Result array
234 * @return array Modified result array
235 * @throws \UnexpectedValueException
236 */
237 protected function setPid(array $result)
238 {
239 // Set pid to vanillaUid. This can be a negative value
240 // if the record is added relative to another record.
241 $result['databaseRow']['pid'] = $result['vanillaUid'];
242
243 // In case a new inline record is created, the pid can be set to a different value
244 // by pageTsConfig, but not by userTsConfig. This overrides the above pid selection
245 // and forces the pid of new inline children.
246 $tableNameWithDot = $result['tableName'] . '.';
247 if ($result['isInlineChild'] && isset($result['pageTsConfig']['TCAdefaults.'][$tableNameWithDot]['pid'])) {
248 if (!MathUtility::canBeInterpretedAsInteger($result['pageTsConfig']['TCAdefaults.'][$tableNameWithDot]['pid'])) {
249 throw new \UnexpectedValueException(
250 'page TSConfig setting TCAdefaults.' . $tableNameWithDot . 'pid must be a number, but given string '
251 . $result['pageTsConfig']['TCAdefaults.'][$tableNameWithDot]['pid'] . ' can not be interpreted as integer',
252 1461598332
253 );
254 }
255 $result['databaseRow']['pid'] = (int)$result['pageTsConfig']['TCAdefaults.'][$tableNameWithDot]['pid'];
256 }
257
258 return $result;
259 }
260 }