-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathAbstractDataHandler.php
More file actions
434 lines (403 loc) · 14.2 KB
/
AbstractDataHandler.php
File metadata and controls
434 lines (403 loc) · 14.2 KB
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
<?php
declare(strict_types=1);
namespace GridElementsTeam\Gridelements\DataHandler;
/***************************************************************
* Copyright notice
* (c) 2013 Jo Hasenau <info@cybercraft.de>
* All rights reserved
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
use GridElementsTeam\Gridelements\Backend\LayoutSetup;
use GridElementsTeam\Gridelements\Helper\Helper;
use PDO;
use TYPO3\CMS\Core\Database\Connection;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
use TYPO3\CMS\Core\Database\Query\Restriction\EndTimeRestriction;
use TYPO3\CMS\Core\Database\Query\Restriction\HiddenRestriction;
use TYPO3\CMS\Core\Database\Query\Restriction\StartTimeRestriction;
use TYPO3\CMS\Core\DataHandling\DataHandler;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* Class/Function which offers TCE main hook functions.
*
* @author Jo Hasenau <info@cybercraft.de>
*/
abstract class AbstractDataHandler
{
/**
* @var Connection
*/
protected Connection $connection;
/**
* @var string
*/
protected string $table;
/**
* @var int
*/
protected int $pageUid;
/**
* @var int
*/
protected int $contentUid = 0;
/**
* @var DataHandler
*/
protected DataHandler $dataHandler;
/**
* @var LayoutSetup
*/
protected LayoutSetup $layoutSetup;
/**
* initializes this class
*
* @param string $table : The name of the table the data should be saved to
* @param string $uidPid : The uid of the record or page we are currently working on
* @param DataHandler $dataHandler
*/
public function init(string $table, string $uidPid, DataHandler $dataHandler)
{
$this->setTable($table);
if ($table === 'tt_content' && (int)$uidPid < 0) {
$this->setContentUid(abs((int)$uidPid));
$pageUid = Helper::getInstance()->getPidFromUid($this->getContentUid());
$this->setPageUid($pageUid);
} else {
$this->setPageUid((int)$uidPid);
}
$this->setTceMain($dataHandler);
$this->injectLayoutSetup(GeneralUtility::makeInstance(LayoutSetup::class)->init($this->getPageUid()));
}
/**
* getter for contentUid
*
* @return int contentUid
*/
public function getContentUid(): int
{
return $this->contentUid;
}
/**
* setter for contentUid
*
* @param int $contentUid
*/
public function setContentUid(int $contentUid)
{
$this->contentUid = $contentUid;
}
/**
* setter for dataHandler object
*
* @param DataHandler $dataHandler
*/
public function setTceMain(DataHandler $dataHandler)
{
$this->dataHandler = $dataHandler;
}
/**
* inject layout setup
*
* @param LayoutSetup $layoutSetup
*/
public function injectLayoutSetup(LayoutSetup $layoutSetup)
{
$this->layoutSetup = $layoutSetup;
}
/**
* getter for pageUid
*
* @return int pageUid
*/
public function getPageUid(): int
{
return $this->pageUid;
}
/**
* setter for pageUid
*
* @param int $pageUid
*/
public function setPageUid(int $pageUid)
{
$this->pageUid = $pageUid;
}
/**
* Function to remove any remains of versioned records after finalizing a workspace action
* via 'Discard' or 'Publish' commands
* @throws \Doctrine\DBAL\DBALException
*/
public function cleanupWorkspacesAfterFinalizing()
{
$queryBuilder = $this->getQueryBuilder();
$constraints = [
$queryBuilder->expr()->andX(
$queryBuilder->expr()->eq(
'pid',
$queryBuilder->createNamedParameter(-1, PDO::PARAM_INT)
),
$queryBuilder->expr()->eq(
't3ver_wsid',
$queryBuilder->createNamedParameter(0, PDO::PARAM_INT)
)
),
];
$queryBuilder->delete('tt_content')
->where(...$constraints)
->execute();
}
/**
* getter for queryBuilder
*
* @param string $table
* @return QueryBuilder $queryBuilder
*/
public function getQueryBuilder(string $table = 'tt_content'): QueryBuilder
{
/**@var QueryBuilder $queryBuilder */
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable($table);
$queryBuilder->getRestrictions()
->removeByType(HiddenRestriction::class)
->removeByType(StartTimeRestriction::class)
->removeByType(EndTimeRestriction::class);
return $queryBuilder;
}
/**
* Function to handle record actions for current or former children of translated grid containers
* as well as translated references
*
* @param int $uid
* @throws \Doctrine\DBAL\DBALException
*/
public function checkAndUpdateTranslatedElements(int $uid)
{
if ($uid <= 0) {
return;
}
$queryBuilder = $this->getQueryBuilder();
$currentValues = $queryBuilder
->select(
'uid',
'tx_gridelements_container',
'tx_gridelements_columns',
'sys_language_uid',
'colPos',
'l18n_parent'
)
->from('tt_content')
->where(
$queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($uid, PDO::PARAM_INT))
)
->setMaxResults(1)
->execute()
->fetch(PDO::FETCH_BOTH);
if (!empty($currentValues['l18n_parent'])) {
$originalUid = (int)$currentValues['uid'];
$queryBuilder = $this->getQueryBuilder();
$currentValues = $queryBuilder
->select(
'uid',
'tx_gridelements_container',
'tx_gridelements_columns',
'sys_language_uid',
'colPos',
'l18n_parent'
)
->from('tt_content')
->where(
$queryBuilder->expr()->eq(
'uid',
$queryBuilder->createNamedParameter((int)$currentValues['l18n_parent'], PDO::PARAM_INT)
)
)
->setMaxResults(1)
->execute()
->fetch(PDO::FETCH_BOTH);
if (is_array($currentValues)) {
$updateArray = $currentValues;
unset($updateArray['uid']);
unset($updateArray['sys_language_uid']);
unset($updateArray['l18n_parent']);
$this->getConnection()->update(
'tt_content',
$updateArray,
['uid' => $originalUid]
);
}
}
if (empty($currentValues['uid'])) {
return;
}
$queryBuilder = $this->getQueryBuilder();
$translatedElementQuery = $queryBuilder
->select(
'uid',
'tx_gridelements_container',
'tx_gridelements_columns',
'sys_language_uid',
'colPos',
'l18n_parent'
)
->from('tt_content')
->where(
$queryBuilder->expr()->eq(
'l18n_parent',
$queryBuilder->createNamedParameter((int)$currentValues['uid'], PDO::PARAM_INT)
)
)
->execute();
$translatedElements = [];
while ($translatedElement = $translatedElementQuery->fetch(PDO::FETCH_BOTH)) {
$translatedElements[$translatedElement['uid']] = $translatedElement;
}
if (empty($translatedElements)) {
return;
}
$translatedContainers = [];
if ($currentValues['tx_gridelements_container'] > 0) {
$queryBuilder = $this->getQueryBuilder();
$translatedContainerQuery = $queryBuilder
->select('uid', 'sys_language_uid')
->from('tt_content')
->where(
$queryBuilder->expr()->eq(
'l18n_parent',
$queryBuilder->createNamedParameter(
(int)$currentValues['tx_gridelements_container'],
PDO::PARAM_INT
)
),
$queryBuilder->expr()->eq(
't3ver_oid',
$queryBuilder->createNamedParameter(0, PDO::PARAM_INT)
)
)
->execute();
while ($translatedContainer = $translatedContainerQuery->fetch(PDO::FETCH_BOTH)) {
$translatedContainers[$translatedContainer['sys_language_uid']] = $translatedContainer;
}
}
$containerUpdateArray = [];
foreach ($translatedElements as $translatedUid => $translatedElement) {
$updateArray = [];
if (isset($translatedContainers[$translatedElement['sys_language_uid']])) {
$updateArray['tx_gridelements_container'] = (int)$translatedContainers[$translatedElement['sys_language_uid']]['uid'];
$updateArray['tx_gridelements_columns'] = (int)$currentValues['tx_gridelements_columns'];
} else {
if ($translatedElement['tx_gridelements_container'] == $currentValues['tx_gridelements_container']) {
$updateArray['tx_gridelements_container'] = (int)$currentValues['tx_gridelements_container'];
$updateArray['tx_gridelements_columns'] = (int)$currentValues['tx_gridelements_columns'];
} else {
$updateArray['tx_gridelements_container'] = 0;
$updateArray['tx_gridelements_columns'] = 0;
}
}
$updateArray['colPos'] = (int)$currentValues['colPos'];
$this->getConnection()->update(
'tt_content',
$updateArray,
['uid' => (int)$translatedUid]
);
if ($translatedElement['tx_gridelements_container'] !== $updateArray['tx_gridelements_container']) {
if (!isset($containerUpdateArray[$translatedElement['tx_gridelements_container']])) {
$containerUpdateArray[$translatedElement['tx_gridelements_container']] = -1;
} else {
$containerUpdateArray[$translatedElement['tx_gridelements_container']] -= 1;
}
if (!isset($containerUpdateArray[$updateArray['tx_gridelements_container']])) {
$containerUpdateArray[$updateArray['tx_gridelements_container']] = 1;
} else {
$containerUpdateArray[$updateArray['tx_gridelements_container']] += 1;
}
$this->getTceMain()->updateRefIndex('tt_content', (int)$translatedElement['tx_gridelements_container']);
$this->getTceMain()->updateRefIndex('tt_content', $updateArray['tx_gridelements_container']);
}
}
if (!empty($containerUpdateArray)) {
$this->doGridContainerUpdate($containerUpdateArray);
}
}
/**
* setter for Connection object
*
* @return Connection
*/
public function getConnection(): Connection
{
return GeneralUtility::makeInstance(ConnectionPool::class)
->getConnectionForTable('tt_content');
}
/**
* getter for dataHandler
*
* @return DataHandler dataHandler
*/
public function getTceMain(): DataHandler
{
return $this->dataHandler;
}
/**
* Function to handle record actions between different grid containers
*
* @param array $containerUpdateArray
* @throws \Doctrine\DBAL\DBALException
*/
public function doGridContainerUpdate(array $containerUpdateArray = [])
{
if (is_array($containerUpdateArray) && !empty($containerUpdateArray)) {
$queryBuilder = $this->getQueryBuilder();
$currentContainers = $queryBuilder
->select('uid', 'tx_gridelements_children')
->from('tt_content')
->where(
$queryBuilder->expr()->in('uid', implode(',', array_keys($containerUpdateArray)))
)
->execute()
->fetchAll();
if (!empty($currentContainers)) {
foreach ($currentContainers as $fieldArray) {
$fieldArray['tx_gridelements_children'] = (int)$fieldArray['tx_gridelements_children'] + (int)$containerUpdateArray[$fieldArray['uid']];
$updateArray = $fieldArray;
unset($updateArray['uid']);
$this->getConnection()->update(
'tt_content',
$updateArray,
['uid' => (int)$fieldArray['uid']]
);
$this->getTceMain()->updateRefIndex('tt_content', (int)$fieldArray['uid']);
}
}
}
}
/**
* getter for table
*
* @return string table
*/
public function getTable(): string
{
return $this->table;
}
/**
* setter for table
*
* @param string $table
*/
public function setTable(string $table)
{
$this->table = $table;
}
}