‪TYPO3CMS  10.4
SaveToDatabaseFinisher.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 
25 
175 {
176 
180  protected ‪$defaultOptions = [
181  'table' => null,
182  'mode' => 'insert',
183  'whereClause' => [],
184  'elements' => [],
185  'databaseColumnMappings' => [],
186  ];
187 
191  protected ‪$databaseConnection;
192 
199  protected function ‪executeInternal()
200  {
201  ‪$options = [];
202  if (isset($this->options['table'])) {
204  } else {
206  }
207 
208  foreach (‪$options as $optionKey => $option) {
209  $this->options = $option;
210  $this->‪process($optionKey);
211  }
212  }
213 
221  protected function ‪prepareData(array $elementsConfiguration, array $databaseData)
222  {
223  foreach ($this->‪getFormValues() as $elementIdentifier => $elementValue) {
224  if (
225  ($elementValue === null || $elementValue === '')
226  && isset($elementsConfiguration[$elementIdentifier])
227  && isset($elementsConfiguration[$elementIdentifier]['skipIfValueIsEmpty'])
228  && $elementsConfiguration[$elementIdentifier]['skipIfValueIsEmpty'] === true
229  ) {
230  continue;
231  }
232 
233  $element = $this->‪getElementByIdentifier($elementIdentifier);
234  if (
235  !$element instanceof ‪FormElementInterface
236  || !isset($elementsConfiguration[$elementIdentifier])
237  || !isset($elementsConfiguration[$elementIdentifier]['mapOnDatabaseColumn'])
238  ) {
239  continue;
240  }
241 
242  if ($elementValue instanceof ‪FileReference) {
243  if (isset($elementsConfiguration[$elementIdentifier]['saveFileIdentifierInsteadOfUid'])) {
244  $saveFileIdentifierInsteadOfUid = (bool)$elementsConfiguration[$elementIdentifier]['saveFileIdentifierInsteadOfUid'];
245  } else {
246  $saveFileIdentifierInsteadOfUid = false;
247  }
248 
249  if ($saveFileIdentifierInsteadOfUid) {
250  $elementValue = $elementValue->getOriginalResource()->getCombinedIdentifier();
251  } else {
252  $elementValue = $elementValue->getOriginalResource()->getProperty('uid_local');
253  }
254  } elseif (is_array($elementValue)) {
255  $elementValue = implode(',', $elementValue);
256  } elseif ($elementValue instanceof \DateTimeInterface) {
257  $format = $elementsConfiguration[$elementIdentifier]['dateFormat'] ?? 'U';
258  $elementValue = $elementValue->format($format);
259  }
260 
261  $databaseData[$elementsConfiguration[$elementIdentifier]['mapOnDatabaseColumn']] = $elementValue;
262  }
263  return $databaseData;
264  }
265 
271  protected function ‪process(int $iterationCount)
272  {
274 
275  $table = $this->‪parseOption('table');
276  $table = is_string($table) ? $table : '';
277  $elementsConfiguration = $this->‪parseOption('elements');
278  $elementsConfiguration = is_array($elementsConfiguration) ? $elementsConfiguration : [];
279  $databaseColumnMappingsConfiguration = $this->‪parseOption('databaseColumnMappings');
280 
281  $this->databaseConnection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($table);
282 
283  $databaseData = [];
284  foreach ($databaseColumnMappingsConfiguration as $databaseColumnName => $databaseColumnConfiguration) {
285  $value = $this->‪parseOption('databaseColumnMappings.' . $databaseColumnName . '.value');
286  if (
287  empty($value)
288  && $databaseColumnConfiguration['skipIfValueIsEmpty'] === true
289  ) {
290  continue;
291  }
292 
293  $databaseData[$databaseColumnName] = $value;
294  }
295 
296  $databaseData = $this->‪prepareData($elementsConfiguration, $databaseData);
297 
298  $this->‪saveToDatabase($databaseData, $table, $iterationCount);
299  }
300 
309  protected function ‪saveToDatabase(array $databaseData, string $table, int $iterationCount)
310  {
311  if (!empty($databaseData)) {
312  if ($this->options['mode'] === 'update') {
313  $whereClause = $this->options['whereClause'];
314  foreach ($whereClause as $columnName => $columnValue) {
315  $whereClause[$columnName] = $this->‪parseOption('whereClause.' . $columnName);
316  }
317  $this->databaseConnection->update(
318  $table,
319  $databaseData,
320  $whereClause
321  );
322  } else {
323  $this->databaseConnection->insert($table, $databaseData);
324  $insertedUid = (int)$this->databaseConnection->lastInsertId($table);
325  $this->finisherContext->getFinisherVariableProvider()->add(
326  $this->shortFinisherIdentifier,
327  'insertedUids.' . $iterationCount,
328  $insertedUid
329  );
330  }
331  }
332  }
333 
341  {
342  if (
343  $this->options['mode'] === 'update'
344  && empty($this->options['whereClause'])
345  ) {
346  throw new ‪FinisherException(
347  'An empty option "whereClause" is not allowed in update mode.',
348  1480469086
349  );
350  }
351  }
352 
358  protected function ‪getFormValues(): array
359  {
360  return $this->finisherContext->getFormValues();
361  }
362 
369  protected function ‪getElementByIdentifier(string $elementIdentifier)
370  {
371  return $this
372  ->finisherContext
373  ->getFormRuntime()
374  ->getFormDefinition()
375  ->getElementByIdentifier($elementIdentifier);
376  }
377 }
‪TYPO3\CMS\Form\Domain\Finishers\SaveToDatabaseFinisher\getElementByIdentifier
‪FormElementInterface null getElementByIdentifier(string $elementIdentifier)
Definition: SaveToDatabaseFinisher.php:367
‪TYPO3\CMS\Form\Domain\Finishers\SaveToDatabaseFinisher\$defaultOptions
‪array $defaultOptions
Definition: SaveToDatabaseFinisher.php:179
‪TYPO3\CMS\Form\Domain\Finishers\SaveToDatabaseFinisher\getFormValues
‪array getFormValues()
Definition: SaveToDatabaseFinisher.php:356
‪TYPO3\CMS\Form\Domain\Finishers\SaveToDatabaseFinisher
Definition: SaveToDatabaseFinisher.php:175
‪TYPO3\CMS\Form\Domain\Finishers
Definition: AbstractFinisher.php:22
‪TYPO3\CMS\Form\Domain\Finishers\AbstractFinisher
Definition: AbstractFinisher.php:41
‪TYPO3\CMS\Form\Domain\Model\FormElements\FormElementInterface
Definition: FormElementInterface.php:40
‪TYPO3\CMS\Form\Domain\Finishers\Exception\FinisherException
Definition: FinisherException.php:26
‪TYPO3\CMS\Form\Domain\Finishers\AbstractFinisher\$options
‪array $options
Definition: AbstractFinisher.php:60
‪TYPO3\CMS\Extbase\Domain\Model\FileReference
Definition: FileReference.php:28
‪TYPO3\CMS\Form\Domain\Finishers\SaveToDatabaseFinisher\prepareData
‪array prepareData(array $elementsConfiguration, array $databaseData)
Definition: SaveToDatabaseFinisher.php:219
‪TYPO3\CMS\Form\Domain\Finishers\SaveToDatabaseFinisher\executeInternal
‪executeInternal()
Definition: SaveToDatabaseFinisher.php:197
‪TYPO3\CMS\Form\Domain\Finishers\SaveToDatabaseFinisher\$databaseConnection
‪TYPO3 CMS Core Database Connection $databaseConnection
Definition: SaveToDatabaseFinisher.php:189
‪TYPO3\CMS\Form\Domain\Finishers\AbstractFinisher\parseOption
‪string array null parseOption(string $optionName)
Definition: AbstractFinisher.php:161
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Form\Domain\Finishers\SaveToDatabaseFinisher\throwExceptionOnInconsistentConfiguration
‪throwExceptionOnInconsistentConfiguration()
Definition: SaveToDatabaseFinisher.php:338
‪TYPO3\CMS\Form\Domain\Finishers\SaveToDatabaseFinisher\saveToDatabase
‪saveToDatabase(array $databaseData, string $table, int $iterationCount)
Definition: SaveToDatabaseFinisher.php:307
‪TYPO3\CMS\Form\Domain\Finishers\SaveToDatabaseFinisher\process
‪process(int $iterationCount)
Definition: SaveToDatabaseFinisher.php:269