‪TYPO3CMS  10.4
AddController.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Psr\Http\Message\ResponseInterface;
21 use Psr\Http\Message\ServerRequestInterface;
31 
38 {
44  protected ‪$processDataFlag = 0;
45 
51  protected ‪$pid;
52 
58  protected ‪$table;
59 
65  protected ‪$id;
66 
72  protected ‪$P;
73 
79  protected ‪$returnEditConf;
80 
88  public function ‪mainAction(ServerRequestInterface $request): ResponseInterface
89  {
90  $this->‪getLanguageService()->‪includeLLFile('EXT:core/Resources/Private/Language/locallang_wizards.xlf');
91  $this->‪init($request);
92 
93  // Return if new record as parent (not possibly/allowed)
94  if ($this->pid === '') {
95  return new ‪RedirectResponse(GeneralUtility::sanitizeLocalUrl($this->P['returnUrl']));
96  }
97 
98  if ($this->returnEditConf) {
99  if ($this->processDataFlag) {
100  // Because OnTheFly can't handle MM relations with intermediate tables we use TcaDatabaseRecord here
101  // Otherwise already stored relations are overwritten with the new entry
103  $formDataGroup = GeneralUtility::makeInstance(TcaDatabaseRecord::class);
105  $formDataCompiler = GeneralUtility::makeInstance(FormDataCompiler::class, $formDataGroup);
106  $input = [
107  'tableName' => $this->P['table'],
108  'vanillaUid' => (int)$this->P['uid'],
109  'command' => 'edit',
110  ];
111  $result = $formDataCompiler->compile($input);
112  $currentParentRow = $result['databaseRow'];
113 
114  // If that record was found (should absolutely be...), then init DataHandler and set, prepend or append
115  // the record
116  if (is_array($currentParentRow)) {
118  $dataHandler = GeneralUtility::makeInstance(DataHandler::class);
119  $data = [];
120  $recordId = $this->table . '_' . ‪$this->id;
121  // Setting the new field data:
122  // If the field is a flexForm field, work with the XML structure instead:
123  if ($this->P['flexFormPath']) {
124  // Current value of flexForm path:
125  $currentFlexFormData = $currentParentRow[$this->P['field']];
127  $flexFormTools = GeneralUtility::makeInstance(FlexFormTools::class);
128  $currentFlexFormValueByPath = $flexFormTools->getArrayValueByPath(
129  $this->P['flexFormPath'],
130  $currentFlexFormData
131  );
132 
133  // Compile currentFlexFormData to functional string
134  $currentFlexFormValues = [];
135  foreach ($currentFlexFormValueByPath as $value) {
136  if (is_array($value)) {
137  // group fields are always resolved to array
138  $currentFlexFormValues[] = $value['table'] . '_' . $value['uid'];
139  } else {
140  // but select fields may be uids only
141  $currentFlexFormValues[] = $value;
142  }
143  }
144  $currentFlexFormValue = implode(',', $currentFlexFormValues);
145 
146  $insertValue = '';
147  switch ((string)$this->P['params']['setValue']) {
148  case 'set':
149  $insertValue = $recordId;
150  break;
151  case 'append':
152  $insertValue = $currentFlexFormValue . ',' . $recordId;
153  break;
154  case 'prepend':
155  $insertValue = $recordId . ',' . $currentFlexFormValue;
156  break;
157  }
158  $insertValue = implode(',', ‪GeneralUtility::trimExplode(',', $insertValue, true));
159  $data[$this->P['table']][$this->P['uid']][$this->P['field']] = [];
160  $flexFormTools->setArrayValueByPath(
161  $this->P['flexFormPath'],
162  $data[$this->P['table']][$this->P['uid']][$this->P['field']],
163  $insertValue
164  );
165  } else {
166  $currentValue = $currentParentRow[$this->P['field']];
167 
168  // Normalize CSV values
169  if (!is_array($currentValue)) {
170  $currentValue = ‪GeneralUtility::trimExplode(',', $currentValue, true);
171  }
172 
173  // Normalize all items to "<table>_<uid>" format
174  $currentValue = array_map(function ($item) {
175  // Handle per-item table for "group" elements
176  if (is_array($item)) {
177  $item = $item['table'] . '_' . $item['uid'];
178  } else {
179  $item = $this->table . '_' . $item;
180  }
181 
182  return $item;
183  }, $currentValue);
184 
185  switch ((string)$this->P['params']['setValue']) {
186  case 'set':
187  $currentValue = [$recordId];
188  break;
189  case 'append':
190  $currentValue[] = $recordId;
191  break;
192  case 'prepend':
193  array_unshift($currentValue, $recordId);
194  break;
195  }
196 
197  $data[$this->P['table']][$this->P['uid']][$this->P['field']] = implode(',', $currentValue);
198  }
199  // Submit the data:
200  $dataHandler->start($data, []);
201  $dataHandler->process_datamap();
202  }
203  }
204  // Return to the parent FormEngine record editing session:
205  return new ‪RedirectResponse(GeneralUtility::sanitizeLocalUrl($this->P['returnUrl']));
206  }
207 
208  // Redirecting to FormEngine with instructions to create a new record
209  // AND when closing to return back with information about that records ID etc.
211  $normalizedParams = $request->getAttribute('normalizedParams');
213  $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
214  $redirectUrl = (string)$uriBuilder->buildUriFromRoute('record_edit', [
215  'returnEditConf' => 1,
216  'edit[' . $this->P['params']['table'] . '][' . $this->pid . ']' => 'new',
217  'returnUrl' => $normalizedParams->getRequestUri(),
218  ]);
219 
220  return new ‪RedirectResponse($redirectUrl);
221  }
222 
227  protected function ‪init(ServerRequestInterface $request): void
228  {
229  $parsedBody = $request->getParsedBody();
230  $queryParams = $request->getQueryParams();
231  // Init GPvars:
232  $this->P = $parsedBody['P'] ?? $queryParams['P'] ?? [];
233  $this->returnEditConf = $parsedBody['returnEditConf'] ?? $queryParams['returnEditConf'] ?? null;
234  // Get this record
235  $record = ‪BackendUtility::getRecord($this->P['table'], $this->P['uid']);
236  // Set table:
237  $this->table = $this->P['params']['table'];
238  // Get TSconfig for it.
240  $this->P['table'],
241  is_array($record) ? $record : ['pid' => $this->P['pid']]
242  );
243  // Set [params][pid]
244  if (strpos($this->P['params']['pid'], '###') === 0 && substr($this->P['params']['pid'], -3) === '###') {
245  $keyword = substr($this->P['params']['pid'], 3, -3);
246  if (strpos($keyword, 'PAGE_TSCONFIG_') === 0) {
247  $this->pid = (int)$TSconfig[$this->P['field']][$keyword];
248  } else {
249  $this->pid = (int)$TSconfig['_' . $keyword];
250  }
251  } else {
252  $this->pid = (int)$this->P['params']['pid'];
253  }
254  // Return if new record as parent (not possibly/allowed)
255  if ($this->pid === '') {
256  // HTTP Redirect is performed by processRequest()
257  return;
258  }
259  // Else proceed:
260  // If a new id has returned from a newly created record...
261  if ($this->returnEditConf) {
262  $editConfiguration = json_decode($this->returnEditConf, true);
263  if (is_array($editConfiguration[$this->table]) && ‪MathUtility::canBeInterpretedAsInteger($this->P['uid'])) {
264  // Getting id and cmd from returning editConf array.
265  reset($editConfiguration[$this->table]);
266  $this->id = (int)key($editConfiguration[$this->table]);
267  $cmd = current($editConfiguration[$this->table]);
268  // ... and if everything seems OK we will register some classes for inclusion and instruct the object
269  // to perform processing later.
270  if ($this->P['params']['setValue']
271  && $cmd === 'edit'
272  && $this->id
273  && $this->P['table']
274  && $this->P['field'] && $this->P['uid']
275  ) {
276  $liveRecord = ‪BackendUtility::getLiveVersionOfRecord($this->table, $this->id, 'uid');
277  if ($liveRecord) {
278  $this->id = $liveRecord['uid'];
279  }
280  $this->processDataFlag = 1;
281  }
282  }
283  }
284  }
285 }
‪TYPO3\CMS\Core\DataHandling\DataHandler
Definition: DataHandler.php:84
‪TYPO3\CMS\Backend\Controller\Wizard\AddController
Definition: AddController.php:38
‪TYPO3\CMS\Backend\Controller\Wizard\AbstractWizardController\getLanguageService
‪LanguageService getLanguageService()
Definition: AbstractWizardController.php:77
‪TYPO3\CMS\Core\Localization\LanguageService\includeLLFile
‪array includeLLFile($fileRef, $setGlobal=null, $mergeLocalOntoDefault=null)
Definition: LanguageService.php:297
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsInteger
‪static bool canBeInterpretedAsInteger($var)
Definition: MathUtility.php:74
‪TYPO3\CMS\Backend\Controller\Wizard\AddController\$table
‪string $table
Definition: AddController.php:55
‪TYPO3\CMS\Backend\Controller\Wizard\AddController\$pid
‪int $pid
Definition: AddController.php:49
‪TYPO3\CMS\Backend\Controller\Wizard\AbstractWizardController
Definition: AbstractWizardController.php:28
‪TYPO3\CMS\Backend\Controller\Wizard\AddController\$processDataFlag
‪int $processDataFlag
Definition: AddController.php:43
‪TYPO3\CMS\Backend\Controller\Wizard\AddController\$id
‪int $id
Definition: AddController.php:61
‪TYPO3\CMS\Backend\Controller\Wizard
Definition: AbstractWizardController.php:16
‪TYPO3\CMS\Backend\Routing\UriBuilder
Definition: UriBuilder.php:38
‪TYPO3\CMS\Core\Configuration\FlexForm\FlexFormTools
Definition: FlexFormTools.php:38
‪TYPO3\CMS\Backend\Utility\BackendUtility\getLiveVersionOfRecord
‪static array null getLiveVersionOfRecord($table, $uid, $fields=' *')
Definition: BackendUtility.php:3748
‪TYPO3\CMS\Backend\Controller\Wizard\AddController\$P
‪array $P
Definition: AddController.php:67
‪TYPO3\CMS\Backend\Controller\Wizard\AddController\$returnEditConf
‪string $returnEditConf
Definition: AddController.php:73
‪TYPO3\CMS\Backend\Controller\Wizard\AddController\init
‪init(ServerRequestInterface $request)
Definition: AddController.php:221
‪TYPO3\CMS\Backend\Utility\BackendUtility
Definition: BackendUtility.php:75
‪TYPO3\CMS\Backend\Utility\BackendUtility\getRecord
‪static array null getRecord($table, $uid, $fields=' *', $where='', $useDeleteClause=true)
Definition: BackendUtility.php:95
‪TYPO3\CMS\Core\Http\RedirectResponse
Definition: RedirectResponse.php:28
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static string[] trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
Definition: GeneralUtility.php:1059
‪TYPO3\CMS\Backend\Controller\Wizard\AddController\mainAction
‪ResponseInterface mainAction(ServerRequestInterface $request)
Definition: AddController.php:82
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:22
‪TYPO3\CMS\Backend\Utility\BackendUtility\getTCEFORM_TSconfig
‪static array getTCEFORM_TSconfig($table, $row)
Definition: BackendUtility.php:3096
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Backend\Form\FormDataCompiler
Definition: FormDataCompiler.php:25
‪TYPO3\CMS\Backend\Form\FormDataGroup\TcaDatabaseRecord
Definition: TcaDatabaseRecord.php:25