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