TYPO3 CMS  TYPO3_8-7
TableFlexFormToTtContentFieldsUpdate.php
Go to the documentation of this file.
1 <?php
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 
20 
25 {
29  protected $title = 'Migrate the Flexform for CType "table" to regular fields in tt_content';
30 
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 
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 
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 
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 
202  protected function getFlexFormValue(array $flexForm, $fieldName, $sheet = 'sDEF')
203  {
204  return $flexForm['data'][$sheet]['lDEF'][$fieldName]['vDEF'];
205  }
206 }
static makeInstance($className,... $constructorArguments)
$fields
Definition: pages.php:4
static xml2array($string, $NSprefix='', $reportDocTag=false)