TYPO3 CMS  TYPO3_7-6
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->setPid($result);
52 
53  return $result;
54  }
55 
62  protected function setDefaultsFromUserTsConfig(array $result)
63  {
64  $tableNameWithDot = $result['tableName'] . '.';
65  // Apply default values from user typo script "TCAdefaults" if any
66  if (isset($result['userTsConfig']['TCAdefaults.'][$tableNameWithDot])
67  && is_array($result['userTsConfig']['TCAdefaults.'][$tableNameWithDot])
68  ) {
69  foreach ($result['userTsConfig']['TCAdefaults.'][$tableNameWithDot] as $fieldName => $fieldValue) {
70  if (isset($result['processedTca']['columns'][$fieldName])) {
71  $result['databaseRow'][$fieldName] = $fieldValue;
72  }
73  }
74  }
75  return $result;
76  }
77 
84  protected function setDefaultsFromPageTsConfig(array $result)
85  {
86  $tableNameWithDot = $result['tableName'] . '.';
87  if (isset($result['pageTsConfig']['TCAdefaults.'][$tableNameWithDot])
88  && is_array($result['pageTsConfig']['TCAdefaults.'][$tableNameWithDot])
89  ) {
90  foreach ($result['pageTsConfig']['TCAdefaults.'][$tableNameWithDot] as $fieldName => $fieldValue) {
91  if (isset($result['processedTca']['columns'][$fieldName])) {
92  $result['databaseRow'][$fieldName] = $fieldValue;
93  }
94  }
95  }
96  return $result;
97  }
98 
106  protected function setDefaultsFromNeighborRow(array $result)
107  {
108  if (is_array($result['neighborRow'])
109  && !empty($result['processedTca']['ctrl']['useColumnsForDefaultValues'])
110  ) {
111  $defaultColumns = GeneralUtility::trimExplode(',', $result['processedTca']['ctrl']['useColumnsForDefaultValues'], true);
112  foreach ($defaultColumns as $fieldName) {
113  if (isset($result['processedTca']['columns'][$fieldName])
114  && isset($result['neighborRow'][$fieldName])
115  ) {
116  $result['databaseRow'][$fieldName] = $result['neighborRow'][$fieldName];
117  }
118  }
119  }
120  return $result;
121  }
122 
133  protected function setDefaultsFromDevVals(array $result)
134  {
135  $tableName = $result['tableName'];
136  $defaultValuesFromGetPost = GeneralUtility::_GP('defVals');
137  if (isset($defaultValuesFromGetPost[$tableName])
138  && is_array($defaultValuesFromGetPost[$tableName])
139  ) {
140  foreach ($defaultValuesFromGetPost[$tableName] as $fieldName => $fieldValue) {
141  if (isset($result['processedTca']['columns'][$fieldName])) {
142  $result['databaseRow'][$fieldName] = $fieldValue;
143  }
144  }
145  }
146  return $result;
147  }
148 
159  protected function setDefaultsFromInlineRelations(array $result)
160  {
161  if ($result['inlineChildChildUid'] === null) {
162  return $result;
163  }
164  if (!is_int($result['inlineChildChildUid'])) {
165  throw new \UnexpectedValueException(
166  'An inlineChildChildUid is given for table ' . $result['tableName'] . ', but is not an integer',
167  1444434103
168  );
169  }
170  if (!isset($result['inlineParentConfig']['foreign_selector'])) {
171  throw new \UnexpectedValueException(
172  'An inlineChildChildUid is given for table ' . $result['tableName'] . ', but no foreign_selector in inlineParentConfig',
173  1444434102
174  );
175  }
176  $selectorFieldName = $result['inlineParentConfig']['foreign_selector'];
177  if (!isset($result['processedTca']['columns'][$selectorFieldName]['config']['type'])
178  || ($result['processedTca']['columns'][$selectorFieldName]['config']['type'] !== 'select'
179  && $result['processedTca']['columns'][$selectorFieldName]['config']['type'] !== 'group'
180  )
181  ) {
182  throw new \UnexpectedValueException(
183  $selectorFieldName . ' is target type of a foreign_selector field to table ' . $result['tableName'] . ' and must be either a select or group type field',
184  1444434104
185  );
186  }
187 
188  if ($result['inlineChildChildUid']) {
189  $result['databaseRow'][$selectorFieldName] = $result['inlineChildChildUid'];
190  }
191 
192  return $result;
193  }
194 
203  protected function setPid(array $result)
204  {
205  // Set pid to vanillaUid. This can be a negative value
206  // if the record is added relative to another record.
207  $result['databaseRow']['pid'] = $result['vanillaUid'];
208 
209  // In case a new inline record is created, the pid can be set to a different value
210  // by pageTsConfig, but not by userTsConfig. This overrides the above pid selection
211  // and forces the pid of new inline children.
212  $tableNameWithDot = $result['tableName'] . '.';
213  if ($result['isInlineChild'] && isset($result['pageTsConfig']['TCAdefaults.'][$tableNameWithDot]['pid'])) {
214  if (!MathUtility::canBeInterpretedAsInteger($result['pageTsConfig']['TCAdefaults.'][$tableNameWithDot]['pid'])) {
215  throw new \UnexpectedValueException(
216  'page TSConfig setting TCAdefaults.' . $tableNameWithDot . 'pid must be a number, but given string '
217  . $result['pageTsConfig']['TCAdefaults.'][$tableNameWithDot]['pid'] . ' can not be interpreted as integer',
218  1461598332
219  );
220  }
221  $result['databaseRow']['pid'] = (int)$result['pageTsConfig']['TCAdefaults.'][$tableNameWithDot]['pid'];
222  }
223 
224  return $result;
225  }
226 }
static trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)