[FEATURE] EXT:form - integrate new form framework
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / Domain / Finishers / SaveToDatabaseFinisher.php
1 <?php
2 declare(strict_types=1);
3 namespace TYPO3\CMS\Form\Domain\Finishers;
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
18 use TYPO3\CMS\Core\Database\ConnectionPool;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20 use TYPO3\CMS\Extbase\Domain\Model\FileReference;
21 use TYPO3\CMS\Form\Domain\Finishers\Exception\FinisherException;
22 use TYPO3\CMS\Form\Domain\Model\FormElements\FormElementInterface;
23
24 /**
25 * This finisher saves the data from a submitted form into
26 * a database table.
27 *
28 * Scope: frontend
29 */
30 class SaveToDatabaseFinisher extends AbstractFinisher
31 {
32
33 /**
34 * @var array
35 */
36 protected $defaultOptions = [
37 'table' => null,
38 'elements' => [],
39 ];
40
41 /**
42 * Executes this finisher
43 * @see AbstractFinisher::execute()
44 *
45 * @return void
46 * @throws FinisherException
47 */
48 protected function executeInternal()
49 {
50 $table = $this->parseOption('table');
51 $elementsConfiguration = $this->parseOption('elements');
52
53 $databaseConnection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($table);
54 $schemaManager = $databaseConnection->getSchemaManager();
55
56 if ($schemaManager->tablesExist([$table]) === false) {
57 throw new FinisherException('The table "' . $table . '" does not exist.', 1476362091);
58 }
59
60 $databaseColumns = $schemaManager->listTableColumns($table);
61 foreach ($elementsConfiguration as $elementIdentifier => $elementConfiguration) {
62 if (!array_key_exists($elementConfiguration['mapOnDatabaseColumn'], $databaseColumns)) {
63 throw new FinisherException('The column "' . $elementConfiguration['mapOnDatabaseColumn'] . '" does not exist in table "' . $table . '".', 1476362572);
64 }
65 }
66
67 $formRuntime = $this->finisherContext->getFormRuntime();
68
69 $insertData = [];
70 foreach ($this->finisherContext->getFormValues() as $elementIdentifier => $elementValue) {
71 $element = $formRuntime->getFormDefinition()->getElementByIdentifier($elementIdentifier);
72 if (
73 !$element instanceof FormElementInterface
74 || !isset($elementsConfiguration[$elementIdentifier])
75 || !isset($elementsConfiguration[$elementIdentifier]['mapOnDatabaseColumn'])
76 ) {
77 continue;
78 }
79
80 if ($elementValue instanceof FileReference) {
81 if (isset($elementsConfiguration[$elementIdentifier]['saveFileIdentifierInsteadOfUid'])) {
82 $saveFileIdentifierInsteadOfUid = (bool)$elementsConfiguration[$elementIdentifier]['saveFileIdentifierInsteadOfUid'];
83 } else {
84 $saveFileIdentifierInsteadOfUid = false;
85 }
86
87 if ($saveFileIdentifierInsteadOfUid) {
88 $elementValue = $elementValue->getOriginalResource()->getCombinedIdentifier();
89 } else {
90 $elementValue = $elementValue->getOriginalResource()->getProperty('uid_local');
91 }
92 }
93 $insertData[$elementsConfiguration[$elementIdentifier]['mapOnDatabaseColumn']] = $elementValue;
94 }
95
96 if (!empty($insertData)) {
97 $databaseConnection->insert($table, $insertData);
98 }
99 }
100 }