‪TYPO3CMS  ‪main
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 
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 
266  protected function ‪process(int $iterationCount)
267  {
269 
270  $table = $this->‪parseOption('table');
271  $table = is_string($table) ? $table : '';
272  $elementsConfiguration = $this->‪parseOption('elements');
273  $elementsConfiguration = is_array($elementsConfiguration) ? $elementsConfiguration : [];
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'] ?? false) === 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 
300  protected function ‪saveToDatabase(array $databaseData, string $table, int $iterationCount)
301  {
302  if (!empty($databaseData)) {
303  if ($this->‪parseOption('mode') === 'update') {
304  $whereClause = $this->‪parseOption('whereClause');
305  foreach ($whereClause as $columnName => $columnValue) {
306  $whereClause[$columnName] = $this->‪parseOption('whereClause.' . $columnName);
307  }
308  $this->databaseConnection->update(
309  $table,
310  $databaseData,
311  $whereClause
312  );
313  } else {
314  $this->databaseConnection->insert($table, $databaseData);
315  $insertedUid = (int)$this->databaseConnection->lastInsertId();
316  $this->finisherContext->getFinisherVariableProvider()->add(
317  $this->shortFinisherIdentifier,
318  'insertedUids.' . $iterationCount,
319  $insertedUid
320  );
321  }
322  }
323  }
324 
332  {
333  if (
334  $this->‪parseOption('mode') === 'update'
335  && empty($this->‪parseOption('whereClause'))
336  ) {
337  throw new ‪FinisherException(
338  'An empty option "whereClause" is not allowed in update mode.',
339  1480469086
340  );
341  }
342  }
343 
347  protected function ‪getFormValues(): array
348  {
349  return $this->finisherContext->getFormValues();
350  }
351 
357  protected function ‪getElementByIdentifier(string $elementIdentifier)
358  {
359  return $this
360  ->finisherContext
361  ->getFormRuntime()
362  ->getFormDefinition()
363  ->getElementByIdentifier($elementIdentifier);
364  }
365 }
‪TYPO3\CMS\Form\Domain\Finishers\SaveToDatabaseFinisher\getElementByIdentifier
‪FormElementInterface null getElementByIdentifier(string $elementIdentifier)
Definition: SaveToDatabaseFinisher.php:355
‪TYPO3\CMS\Form\Domain\Finishers\SaveToDatabaseFinisher\$defaultOptions
‪array $defaultOptions
Definition: SaveToDatabaseFinisher.php:178
‪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\parseOption
‪string array int null parseOption(string $optionName)
Definition: AbstractFinisher.php:139
‪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\SaveToDatabaseFinisher\getFormValues
‪getFormValues()
Definition: SaveToDatabaseFinisher.php:345
‪TYPO3\CMS\Form\Domain\Finishers\Exception\FinisherException
Definition: FinisherException.php:25
‪TYPO3\CMS\Form\Domain\Finishers\AbstractFinisher\$options
‪array $options
Definition: AbstractFinisher.php:55
‪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:216
‪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\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Form\Domain\Finishers\SaveToDatabaseFinisher\throwExceptionOnInconsistentConfiguration
‪throwExceptionOnInconsistentConfiguration()
Definition: SaveToDatabaseFinisher.php:329
‪TYPO3\CMS\Form\Domain\Finishers\SaveToDatabaseFinisher\saveToDatabase
‪saveToDatabase(array $databaseData, string $table, int $iterationCount)
Definition: SaveToDatabaseFinisher.php:298
‪TYPO3\CMS\Form\Domain\Finishers\SaveToDatabaseFinisher\process
‪process(int $iterationCount)
Definition: SaveToDatabaseFinisher.php:264