TYPO3 CMS  TYPO3_8-7
SaveToDatabaseFinisher.php
Go to the documentation of this file.
1 <?php
2 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 
23 
173 {
174 
178  protected $defaultOptions = [
179  'table' => null,
180  'mode' => 'insert',
181  'whereClause' => [],
182  'elements' => [],
183  'databaseColumnMappings' => [],
184  ];
185 
189  protected $databaseConnection = null;
190 
197  protected function executeInternal()
198  {
199  if (isset($this->options['table'])) {
201  } else {
203  }
204 
205  foreach ($options as $optionKey => $option) {
206  $this->options = $option;
207  $this->process($optionKey);
208  }
209  }
210 
218  protected function prepareData(array $elementsConfiguration, array $databaseData)
219  {
220  foreach ($this->getFormValues() as $elementIdentifier => $elementValue) {
221  if (
222  ($elementValue === null || $elementValue === '')
223  && isset($elementsConfiguration[$elementIdentifier])
224  && isset($elementsConfiguration[$elementIdentifier]['skipIfValueIsEmpty'])
225  && $elementsConfiguration[$elementIdentifier]['skipIfValueIsEmpty'] === true
226  ) {
227  continue;
228  }
229 
230  $element = $this->getElementByIdentifier($elementIdentifier);
231  if (
232  !$element instanceof FormElementInterface
233  || !isset($elementsConfiguration[$elementIdentifier])
234  || !isset($elementsConfiguration[$elementIdentifier]['mapOnDatabaseColumn'])
235  ) {
236  continue;
237  }
238 
239  if ($elementValue instanceof FileReference) {
240  if (isset($elementsConfiguration[$elementIdentifier]['saveFileIdentifierInsteadOfUid'])) {
241  $saveFileIdentifierInsteadOfUid = (bool)$elementsConfiguration[$elementIdentifier]['saveFileIdentifierInsteadOfUid'];
242  } else {
243  $saveFileIdentifierInsteadOfUid = false;
244  }
245 
246  if ($saveFileIdentifierInsteadOfUid) {
247  $elementValue = $elementValue->getOriginalResource()->getCombinedIdentifier();
248  } else {
249  $elementValue = $elementValue->getOriginalResource()->getProperty('uid_local');
250  }
251  } elseif (is_array($elementValue)) {
252  $elementValue = implode(',', $elementValue);
253  } elseif ($elementValue instanceof \DateTimeInterface) {
254  $format = $elementsConfiguration[$elementIdentifier]['dateFormat'] ?? 'U';
255  $elementValue = $elementValue->format($format);
256  }
257 
258  $databaseData[$elementsConfiguration[$elementIdentifier]['mapOnDatabaseColumn']] = $elementValue;
259  }
260  return $databaseData;
261  }
262 
268  protected function process(int $iterationCount)
269  {
271 
272  $table = $this->parseOption('table');
273  $elementsConfiguration = $this->parseOption('elements');
274  $databaseColumnMappingsConfiguration = $this->parseOption('databaseColumnMappings');
275 
276  $this->databaseConnection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($table);
277 
278  $databaseData = [];
279  foreach ($databaseColumnMappingsConfiguration as $databaseColumnName => $databaseColumnConfiguration) {
280  $value = $this->parseOption('databaseColumnMappings.' . $databaseColumnName . '.value');
281  if (
282  empty($value)
283  && $databaseColumnConfiguration['skipIfValueIsEmpty'] === true
284  ) {
285  continue;
286  }
287 
288  $databaseData[$databaseColumnName] = $value;
289  }
290 
291  $databaseData = $this->prepareData($elementsConfiguration, $databaseData);
292 
293  $this->saveToDatabase($databaseData, $table, $iterationCount);
294  }
295 
304  protected function saveToDatabase(array $databaseData, string $table, int $iterationCount)
305  {
306  if (!empty($databaseData)) {
307  if ($this->options['mode'] === 'update') {
308  $whereClause = $this->options['whereClause'];
309  foreach ($whereClause as $columnName => $columnValue) {
310  $whereClause[$columnName] = $this->parseOption('whereClause.' . $columnName);
311  }
312  $this->databaseConnection->update(
313  $table,
314  $databaseData,
315  $whereClause
316  );
317  } else {
318  $this->databaseConnection->insert($table, $databaseData);
319  $insertedUid = (int)$this->databaseConnection->lastInsertId($table);
320  $this->finisherContext->getFinisherVariableProvider()->add(
321  $this->shortFinisherIdentifier,
322  'insertedUids.' . $iterationCount,
323  $insertedUid
324  );
325  }
326  }
327  }
328 
336  {
337  if (
338  $this->options['mode'] === 'update'
339  && empty($this->options['whereClause'])
340  ) {
341  throw new FinisherException(
342  'An empty option "whereClause" is not allowed in update mode.',
343  1480469086
344  );
345  }
346  }
347 
353  protected function getFormValues(): array
354  {
355  return $this->finisherContext->getFormValues();
356  }
357 
364  protected function getElementByIdentifier(string $elementIdentifier)
365  {
366  return $this
367  ->finisherContext
368  ->getFormRuntime()
369  ->getFormDefinition()
370  ->getElementByIdentifier($elementIdentifier);
371  }
372 }
saveToDatabase(array $databaseData, string $table, int $iterationCount)
prepareData(array $elementsConfiguration, array $databaseData)
static makeInstance($className,... $constructorArguments)