[CLEANUP] The correct case must be used for standard PHP types in phpdoc
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Classes / Updates / TableFlexFormToTtContentFieldsUpdate.php
1 <?php
2 namespace TYPO3\CMS\Install\Updates;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Database\ConnectionPool;
18 use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21 /**
22 * Migrate the Flexform for CType 'table' to regular fields in tt_content
23 */
24 class TableFlexFormToTtContentFieldsUpdate extends AbstractUpdate
25 {
26 /**
27 * @var string
28 */
29 protected $title = 'Migrate the Flexform for CType "table" to regular fields in tt_content';
30
31 /**
32 * Checks if an update is needed
33 *
34 * @param string &$description The description for the update
35 * @return bool Whether an update is needed (TRUE) or not (FALSE)
36 */
37 public function checkForUpdate(&$description)
38 {
39 if ($this->isWizardDone()) {
40 return false;
41 }
42
43 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tt_content');
44 $queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
45 $flexFormCount = $queryBuilder->count('uid')
46 ->from('tt_content')
47 ->where(
48 $queryBuilder->expr()->eq('CType', $queryBuilder->createNamedParameter('table', \PDO::PARAM_STR)),
49 $queryBuilder->expr()->isNotNull('pi_flexform')
50 )
51 ->execute()->fetchColumn(0);
52
53 if ($flexFormCount) {
54 $description = 'The extension "frontend" uses regular database fields in the tt_content table '
55 . 'for the CType "table". Before this was a FlexForm.<br /><br />'
56 . 'This update wizard migrates these FlexForms to regular database fields.';
57 }
58
59 return (bool)$flexFormCount;
60 }
61
62 /**
63 * Performs the database update if CType 'table' still has content in pi_flexform
64 *
65 * @param array &$databaseQueries Queries done in this update
66 * @param string &$customMessage Custom message
67 * @return bool
68 */
69 public function performUpdate(array &$databaseQueries, &$customMessage)
70 {
71 $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('tt_content');
72 $queryBuilder = $connection->createQueryBuilder();
73 $queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
74 $statement = $queryBuilder->select('uid', 'pi_flexform')
75 ->from('tt_content')
76 ->where(
77 $queryBuilder->expr()->eq('Ctype', $queryBuilder->createNamedParameter('table', \PDO::PARAM_STR)),
78 $queryBuilder->expr()->isNotNull('pi_flexform')
79 )
80 ->execute();
81 while ($tableRecord = $statement->fetch()) {
82 $flexForm = $this->initializeFlexForm($tableRecord['pi_flexform']);
83 if (is_array($flexForm)) {
84 $fields = $this->mapFieldsFromFlexForm($flexForm);
85 $queryBuilder = $connection->createQueryBuilder();
86 $queryBuilder->update('tt_content')
87 ->where(
88 $queryBuilder->expr()->eq(
89 'uid',
90 $queryBuilder->createNamedParameter($tableRecord['uid'], \PDO::PARAM_INT)
91 )
92 )
93 ->set('pi_flexform', 'null', false);
94 foreach ($fields as $identifier => $value) {
95 $queryBuilder->set($identifier, $value);
96 }
97 $databaseQueries[] = $queryBuilder->getSQL();
98 $queryBuilder->execute();
99 }
100 }
101
102 $this->markWizardAsDone();
103 return true;
104 }
105
106 /**
107 * Map the old FlexForm values to the new database fields
108 * and fill them with the proper data
109 *
110 * @param array $flexForm The content of the FlexForm
111 * @return array The fields which need to be updated in the tt_content table
112 */
113 protected function mapFieldsFromFlexForm($flexForm)
114 {
115 $fields = [];
116
117 $mapping = [
118 'table_caption' => [
119 'sheet' => 'sDEF',
120 'fieldName' => 'acctables_caption',
121 'default' => '',
122 'values' => 'passthrough'
123 ],
124 'table_delimiter' => [
125 'sheet' => 's_parsing',
126 'fieldName' => 'tableparsing_delimiter',
127 'default' => 124,
128 'values' => 'passthrough'
129 ],
130 'table_enclosure' => [
131 'sheet' => 's_parsing',
132 'fieldName' => 'tableparsing_quote',
133 'default' => 0,
134 'values' => 'passthrough'
135 ],
136 'table_header_position' => [
137 'sheet' => 'sDEF',
138 'fieldName' => 'acctables_headerpos',
139 'default' => 0,
140 'values' => [
141 'top' => 1,
142 'left' => 2
143 ]
144 ],
145 'table_tfoot' => [
146 'sheet' => 'sDEF',
147 'fieldName' => 'acctables_tfoot',
148 'default' => 0,
149 'values' => 'passthrough'
150 ],
151 'table_class' => [
152 'sheet' => 'sDEF',
153 'fieldName' => 'acctables_tableclass',
154 'default' => 0,
155 'values' => 'passthrough'
156 ]
157 ];
158
159 foreach ($mapping as $fieldName => $configuration) {
160 $flexFormValue = $this->getFlexFormValue($flexForm, $configuration['fieldName'], $configuration['sheet']);
161
162 if ((string)$flexFormValue !== '') {
163 if ($configuration['values'] === 'passthrough') {
164 $fields[$fieldName] = $flexFormValue;
165 } elseif (is_array($configuration['values'])) {
166 $fields[$fieldName] = $configuration['values'][$flexFormValue];
167 }
168 } else {
169 $fields[$fieldName] = $configuration['default'];
170 }
171 }
172
173 return $fields;
174 }
175
176 /**
177 * Convert the XML of the FlexForm to an array
178 *
179 * @param string|null $flexFormXml The XML of the FlexForm
180 * @return array|null Converted XML to array
181 */
182 protected function initializeFlexForm($flexFormXml)
183 {
184 $flexForm = null;
185
186 if ($flexFormXml) {
187 $flexForm = GeneralUtility::xml2array($flexFormXml);
188 if (!is_array($flexForm)) {
189 $flexForm = null;
190 }
191 }
192
193 return $flexForm;
194 }
195
196 /**
197 * @param array $flexForm The content of the FlexForm
198 * @param string $fieldName The field name to get the value for
199 * @param string $sheet The sheet on which this value is located
200 * @return string The value
201 */
202 protected function getFlexFormValue(array $flexForm, $fieldName, $sheet = 'sDEF')
203 {
204 return $flexForm['data'][$sheet]['lDEF'][$fieldName]['vDEF'];
205 }
206 }