TYPO3 CMS  TYPO3_8-7
DatabaseRowInitializeNew.php
Go to the documentation of this file.
1 <?php
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 
20 
25 {
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 
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 
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 
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 
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 
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  || (
180  $result['processedTca']['columns'][$selectorFieldName]['config']['type'] !== 'select'
181  && $result['processedTca']['columns'][$selectorFieldName]['config']['type'] !== 'group'
182  )
183  ) {
184  throw new \UnexpectedValueException(
185  $selectorFieldName . ' is target type of a foreign_selector field to table ' . $result['tableName'] . ' and must be either a select or group type field',
186  1444434104
187  );
188  }
189 
190  if ($result['inlineChildChildUid']) {
191  $result['databaseRow'][$selectorFieldName] = $result['inlineChildChildUid'];
192  }
193 
194  return $result;
195  }
196 
208  protected function setDefaultsFromInlineParentLanguage(array $result): array
209  {
210  if (!isset($result['inlineParentConfig']['inline']['parentSysLanguageUid'])
211  || empty($result['processedTca']['ctrl']['languageField'])
212  || empty($result['processedTca']['ctrl']['transOrigPointerField'])
213  ) {
214  return $result;
215  }
216 
217  if (!MathUtility::canBeInterpretedAsInteger($result['inlineParentConfig']['inline']['parentSysLanguageUid'])) {
218  throw new \UnexpectedValueException(
219  'A sys_language_uid is set from inline parent config but the value is no integer',
220  1490360772
221  );
222  }
223  $parentSysLanguageUid = (int)$result['inlineParentConfig']['inline']['parentSysLanguageUid'];
224  $languageFieldName = $result['processedTca']['ctrl']['languageField'];
225  $result['databaseRow'][$languageFieldName] = $parentSysLanguageUid;
226 
227  return $result;
228  }
229 
238  protected function setPid(array $result)
239  {
240  // Set pid to vanillaUid. This can be a negative value
241  // if the record is added relative to another record.
242  $result['databaseRow']['pid'] = $result['vanillaUid'];
243 
244  // In case a new inline record is created, the pid can be set to a different value
245  // by pageTsConfig, but not by userTsConfig. This overrides the above pid selection
246  // and forces the pid of new inline children.
247  $tableNameWithDot = $result['tableName'] . '.';
248  if ($result['isInlineChild'] && isset($result['pageTsConfig']['TCAdefaults.'][$tableNameWithDot]['pid'])) {
249  if (!MathUtility::canBeInterpretedAsInteger($result['pageTsConfig']['TCAdefaults.'][$tableNameWithDot]['pid'])) {
250  throw new \UnexpectedValueException(
251  'page TSConfig setting TCAdefaults.' . $tableNameWithDot . 'pid must be a number, but given string '
252  . $result['pageTsConfig']['TCAdefaults.'][$tableNameWithDot]['pid'] . ' can not be interpreted as integer',
253  1461598332
254  );
255  }
256  $result['databaseRow']['pid'] = (int)$result['pageTsConfig']['TCAdefaults.'][$tableNameWithDot]['pid'];
257  }
258 
259  return $result;
260  }
261 }
static trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)