TYPO3 CMS  TYPO3_7-6
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 
19 
24 {
28  protected $title = 'Migrate the Flexform for CType "table" to regular fields in tt_content';
29 
36  public function checkForUpdate(&$description)
37  {
38  $flexFormCount = $this->getDatabaseConnection()->exec_SELECTcountRows(
39  'uid',
40  'tt_content',
41  'CType=\'table\' AND pi_flexform IS NOT NULL AND deleted = 0'
42  );
43 
44  if (
45  $this->isWizardDone() || $flexFormCount === 0
46  || ExtensionManagementUtility::isLoaded('css_styled_content')
47  ) {
48  return false;
49  }
50 
51  $description = 'The extension "frontend" uses regular database fields in the tt_content table ' .
52  'for the CType "table". Before this was a FlexForm.<br /><br />' .
53  'This update wizard migrates these FlexForms to regular database fields.';
54 
55  return true;
56  }
57 
65  public function performUpdate(array &$databaseQueries, &$customMessages)
66  {
67  $databaseConnection = $this->getDatabaseConnection();
68 
69  $databaseResult = $databaseConnection->exec_SELECTquery(
70  'uid, pi_flexform',
71  'tt_content',
72  'CType=\'table\' AND pi_flexform IS NOT NULL AND deleted = 0'
73  );
74 
75  while ($tableRecord = $databaseConnection->sql_fetch_assoc($databaseResult)) {
76  $flexForm = $this->initializeFlexForm($tableRecord['pi_flexform']);
77 
78  if (is_array($flexForm)) {
79  $fields = $this->mapFieldsFromFlexForm($flexForm);
80 
81  // Set pi_flexform to NULL
82  $fields['pi_flexform'] = null;
83 
84  $databaseConnection->exec_UPDATEquery(
85  'tt_content',
86  'uid=' . (int)$tableRecord['uid'],
87  $fields
88  );
89 
90  $databaseQueries[] = $databaseConnection->debug_lastBuiltQuery;
91  }
92  }
93 
94  $databaseConnection->sql_free_result($databaseResult);
95 
96  $this->markWizardAsDone();
97 
98  return true;
99  }
100 
108  protected function mapFieldsFromFlexForm($flexForm)
109  {
110  $fields = [];
111 
112  $mapping = [
113  'table_caption' => [
114  'sheet' => 'sDEF',
115  'fieldName' => 'acctables_caption',
116  'default' => '',
117  'values' => 'passthrough'
118  ],
119  'table_delimiter' => [
120  'sheet' => 's_parsing',
121  'fieldName' => 'tableparsing_delimiter',
122  'default' => 124,
123  'values' => 'passthrough'
124  ],
125  'table_enclosure' => [
126  'sheet' => 's_parsing',
127  'fieldName' => 'tableparsing_quote',
128  'default' => 0,
129  'values' => 'passthrough'
130  ],
131  'table_header_position' => [
132  'sheet' => 'sDEF',
133  'fieldName' => 'acctables_headerpos',
134  'default' => 0,
135  'values' => [
136  'top' => 1,
137  'left' => 2
138  ]
139  ],
140  'table_tfoot' => [
141  'sheet' => 'sDEF',
142  'fieldName' => 'acctables_tfoot',
143  'default' => 0,
144  'values' => 'passthrough'
145  ]
146  ];
147 
148  foreach ($mapping as $fieldName => $configuration) {
149  $flexFormValue = $this->getFlexFormValue($flexForm, $configuration['fieldName'], $configuration['sheet']);
150 
151  if ($flexFormValue !== '') {
152  if ($configuration['values'] === 'passthrough') {
153  $fields[$fieldName] = $flexFormValue;
154  } elseif (is_array($configuration['values'])) {
155  $fields[$fieldName] = $configuration['values'][$flexFormValue];
156  }
157  } else {
158  $fields[$fieldName] = $configuration['default'];
159  }
160  }
161 
162  return $fields;
163  }
164 
171  protected function initializeFlexForm($flexFormXml)
172  {
173  $flexForm = null;
174 
175  if ($flexFormXml) {
176  $flexForm = GeneralUtility::xml2array($flexFormXml);
177  if (!is_array($flexForm)) {
178  $flexForm = null;
179  }
180  }
181 
182  return $flexForm;
183  }
184 
191  protected function getFlexFormValue(array $flexForm, $fieldName, $sheet = 'sDEF')
192  {
193  return $flexForm['data'][$sheet]['lDEF'][$fieldName]['vDEF'];
194  }
195 }
static xml2array($string, $NSprefix='', $reportDocTag=false)