forked from jweiland-net/maps2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.ext_update.php
290 lines (262 loc) · 9.97 KB
/
class.ext_update.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<?php
declare(strict_types=1);
/*
* This file is part of the package jweiland/maps2.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/
namespace JWeiland\Maps2;
use JWeiland\Maps2\Utility\DatabaseUtility;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
use TYPO3\CMS\Core\DataHandling\DataHandler;
use TYPO3\CMS\Core\Messaging\FlashMessage;
use TYPO3\CMS\Core\Messaging\FlashMessageRendererResolver;
use TYPO3\CMS\Core\Resource\FileInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Configuration\FlexForm\FlexFormTools;
/**
* Update class for the extension manager.
*/
class ext_update
{
/**
* Array of flash messages (params) array[][status,title,message]
*
* @var array
*/
protected $messageArray = [];
/**
* @var FlexFormTools
*/
protected $flexFormTools;
/**
* Main update function called by the extension manager.
*/
public function main(): string
{
$this->processUpdates();
return $this->generateOutput();
}
/**
* Called by the extension manager to determine if the update menu entry
* should by showed.
*/
public function access(): bool
{
$showAccess = false;
// check for SCA
$queryBuilder = $this->getConnectionPool()->getQueryBuilderForTable('tt_content');
$amountOfRecords = $queryBuilder
->count('*')
->from('tt_content')
->where(
$queryBuilder->expr()->eq(
'list_type',
$queryBuilder->createNamedParameter('maps2_maps2', \PDO::PARAM_STR)
),
$queryBuilder->expr()->like(
'pi_flexform',
$queryBuilder->createNamedParameter('%switchableControllerActions%', \PDO::PARAM_STR)
)
)
->execute()
->fetchColumn();
if ((bool)$amountOfRecords) {
$showAccess = true;
}
// check for old marker_icon column in sys_category
$fields = DatabaseUtility::getColumnsFromTable('sys_category');
if (array_key_exists('marker_icon', $fields)) {
$queryBuilder = $this->getConnectionPool()->getQueryBuilderForTable('sys_category');
$queryBuilder->getRestrictions()->removeAll()->add(
GeneralUtility::makeInstance(DeletedRestriction::class)
);
$amountOfRecords = $queryBuilder
->count('*')
->from('sys_category')
->where(
$queryBuilder->expr()->neq(
'marker_icon',
$queryBuilder->createNamedParameter('', \PDO::PARAM_STR)
)
)
->execute()
->fetchColumn();
}
if ($amountOfRecords) {
$showAccess = true;
}
return $showAccess;
}
/**
* The actual update function. Add your update task in here.
*/
protected function processUpdates(): void
{
$this->removeSCAFromTtContentRecords();
$this->migrateMarkerIconToFal();
}
/**
* Remove SwitchableControllerActions from tt_content records as they are not needed anymore
*/
protected function removeSCAFromTtContentRecords(): void
{
$queryBuilder = $this->getConnectionPool()->getQueryBuilderForTable('tt_content');
$rows = $queryBuilder
->select('uid', 'pi_flexform')
->from('tt_content')
->where(
$queryBuilder->expr()->eq(
'list_type',
$queryBuilder->createNamedParameter('maps2_maps2', \PDO::PARAM_STR)
),
$queryBuilder->expr()->like(
'pi_flexform',
$queryBuilder->createNamedParameter('%switchableControllerActions%', \PDO::PARAM_STR)
)
)
->execute()
->fetchAll(\PDO::FETCH_ASSOC);
if (is_array($rows)) {
$affectedRows = 0;
foreach ($rows as $row) {
$flexFormFields = GeneralUtility::xml2array($row['pi_flexform']);
if (isset($flexFormFields['data']['sDEFAULT']['lDEF']['switchableControllerActions'])) {
unset($flexFormFields['data']['sDEFAULT']['lDEF']['switchableControllerActions']);
}
$connection = $this->getConnectionPool()->getConnectionForTable('tt_content');
$affectedRows += $connection->update(
'tt_content',
[
'pi_flexform' => $this->getFlexFormTools()->flexArray2Xml($flexFormFields)
],
[
'uid' => (int)$row['uid']
]
);
}
$this->messageArray[] = [
FlashMessage::OK,
'Update records successful',
sprintf(
'We have updated %d of %d tt_content records',
$affectedRows,
count($rows)
)
];
} else {
$this->messageArray[] = [
FlashMessage::ERROR,
'Error while selecting tt_content records',
'SQL-Error'
];
}
}
/**
* Migrate old marker icon of sys_category to FAL
*/
protected function migrateMarkerIconToFal(): void
{
// check for old marker_icon column in sys_category first
$fields = DatabaseUtility::getColumnsFromTable('sys_category');
if (array_key_exists('marker_icon', $fields)) {
$queryBuilder = $this->getConnectionPool()->getQueryBuilderForTable('sys_category');
$queryBuilder->getRestrictions()->removeAll()->add(
GeneralUtility::makeInstance(DeletedRestriction::class)
);
$sysCategories = $queryBuilder
->select('uid', 'pid', 'marker_icon')
->from('sys_category')
->where(
$queryBuilder->expr()->neq(
'marker_icon',
$queryBuilder->createNamedParameter('', \PDO::PARAM_STR)
)
)
->execute()
->fetchAll(\PDO::FETCH_ASSOC);
if (is_array($sysCategories)) {
foreach ($sysCategories as $sysCategory) {
try {
$file = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
\TYPO3\CMS\Core\Resource\ResourceFactory::class
)->retrieveFileOrFolderObject($sysCategory['marker_icon']);
if ($file instanceof FileInterface) {
// Assemble DataHandler data
$newId = 'NEW1234';
$data = [];
$data['sys_file_reference'][$newId] = [
'table_local' => 'sys_file',
'uid_local' => $file->getUid(),
'tablenames' => 'sys_category',
'uid_foreign' => $sysCategory['uid'],
'fieldname' => 'maps2_marker_icons',
'pid' => $sysCategory['pid']
];
$data['sys_category'][$sysCategory['uid']] = [
'maps2_marker_icons' => $newId
];
// Get an instance of the DataHandler and process the data
$dataHandler = GeneralUtility::makeInstance(DataHandler::class);
$dataHandler->start($data, []);
$dataHandler->process_datamap();
}
} catch (\Exception $exception) {
// file does not exist or whatever
}
// remove old icon
$connection = $this->getConnectionPool()->getConnectionForTable('sys_category');
$connection->update(
'sys_category',
[
'marker_icon' => ''
],
[
'uid' => (int)$sysCategory['uid']
]
);
}
$this->messageArray[] = [
FlashMessage::OK,
'Migration successful',
sprintf(
'We have magrated %d sys_category records to FAL',
count($sysCategories)
)
];
}
}
}
/**
* Generates output by using flash messages
*/
protected function generateOutput(): string
{
$output = '';
foreach ($this->messageArray as $messageItem) {
/** @var \TYPO3\CMS\Core\Messaging\FlashMessage $flashMessage */
$flashMessage = GeneralUtility::makeInstance(
FlashMessage::class,
$messageItem[2],
$messageItem[1],
$messageItem[0]);
$output .= GeneralUtility::makeInstance(FlashMessageRendererResolver::class)
->resolve()
->render([$flashMessage]);
}
return $output;
}
protected function getConnectionPool(): ConnectionPool
{
return GeneralUtility::makeInstance(ConnectionPool::class);
}
protected function getFlexFormTools(): FlexFormTools
{
if (!$this->flexFormTools instanceof FlexFormTools) {
$this->flexFormTools = GeneralUtility::makeInstance(FlexFormTools::class);
}
return $this->flexFormTools;
}
}