Passed
Push — master ( 337e8f...9738f3 )
by
unknown
16:20
created

GridDataService::generateDataArray()   D

Complexity

Conditions 23
Paths 4

Size

Total Lines 127
Code Lines 99

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 23
eloc 99
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 127
rs 4.1666

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of the TYPO3 CMS project.
5
 *
6
 * It is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License, either version 2
8
 * of the License, or any later version.
9
 *
10
 * For the full copyright and license information, please read the
11
 * LICENSE.txt file that was distributed with this source code.
12
 *
13
 * The TYPO3 project - inspiring people to share!
14
 */
15
16
namespace TYPO3\CMS\Workspaces\Service;
17
18
use Psr\EventDispatcher\EventDispatcherInterface;
19
use Psr\Log\LoggerAwareInterface;
20
use Psr\Log\LoggerAwareTrait;
21
use TYPO3\CMS\Backend\Configuration\TranslationConfigurationProvider;
22
use TYPO3\CMS\Backend\Utility\BackendUtility;
23
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
24
use TYPO3\CMS\Core\Cache\CacheManager;
25
use TYPO3\CMS\Core\Imaging\Icon;
26
use TYPO3\CMS\Core\Imaging\IconFactory;
27
use TYPO3\CMS\Core\Utility\GeneralUtility;
28
use TYPO3\CMS\Core\Versioning\VersionState;
29
use TYPO3\CMS\Workspaces\Domain\Model\CombinedRecord;
30
use TYPO3\CMS\Workspaces\Event\AfterCompiledCacheableDataForWorkspaceEvent;
31
use TYPO3\CMS\Workspaces\Event\AfterDataGeneratedForWorkspaceEvent;
32
use TYPO3\CMS\Workspaces\Event\GetVersionedDataEvent;
33
use TYPO3\CMS\Workspaces\Event\SortVersionedDataEvent;
34
use TYPO3\CMS\Workspaces\Preview\PreviewUriBuilder;
35
use TYPO3\CMS\Workspaces\Service\Dependency\CollectionService;
36
37
/**
38
 * Grid data service
39
 */
40
class GridDataService implements LoggerAwareInterface
41
{
42
    use LoggerAwareTrait;
43
44
    const GridColumn_Collection = 'Workspaces_Collection';
45
    const GridColumn_CollectionLevel = 'Workspaces_CollectionLevel';
46
    const GridColumn_CollectionParent = 'Workspaces_CollectionParent';
47
    const GridColumn_CollectionCurrent = 'Workspaces_CollectionCurrent';
48
    const GridColumn_CollectionChildren = 'Workspaces_CollectionChildren';
49
50
    /**
51
     * Id of the current active workspace.
52
     *
53
     * @var int
54
     */
55
    protected $currentWorkspace;
56
57
    /**
58
     * Version record information (filtered, sorted and limited)
59
     *
60
     * @var array
61
     */
62
    protected $dataArray = [];
63
64
    /**
65
     * Name of the field used for sorting.
66
     *
67
     * @var string
68
     */
69
    protected $sort = '';
70
71
    /**
72
     * Direction used for sorting (ASC, DESC).
73
     *
74
     * @var string
75
     */
76
    protected $sortDir = '';
77
78
    /**
79
     * @var \TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
80
     */
81
    protected $workspacesCache;
82
83
    /**
84
     * @var IntegrityService
85
     */
86
    protected $integrityService;
87
88
    /**
89
     * @var EventDispatcherInterface
90
     */
91
    protected $eventDispatcher;
92
93
    public function __construct(EventDispatcherInterface $eventDispatcher)
94
    {
95
        $this->eventDispatcher = $eventDispatcher;
96
    }
97
98
    /**
99
     * Generates grid list array from given versions.
100
     *
101
     * @param array $versions All records uids etc. First key is table name, second key incremental integer. Records are associative arrays with uid and t3ver_oid fields. The pid of the online record is found as "livepid" the pid of the offline record is found in "wspid
102
     * @param \stdClass $parameter Parameters as submitted by JavaScript component
103
     * @param int $currentWorkspace The current workspace
104
     * @return array Version record information (filtered, sorted and limited)
105
     * @throws \InvalidArgumentException
106
     */
107
    public function generateGridListFromVersions($versions, $parameter, $currentWorkspace)
108
    {
109
        // Read the given parameters from grid. If the parameter is not set use default values.
110
        $filterTxt = $parameter->filterTxt ?? '';
111
        $start = isset($parameter->start) ? (int)$parameter->start : 0;
112
        $limit = isset($parameter->limit) ? (int)$parameter->limit : 30;
113
        $this->sort = $parameter->sort ?? 't3ver_oid';
114
        $this->sortDir = $parameter->dir ?? 'ASC';
115
        if (is_int($currentWorkspace)) {
0 ignored issues
show
introduced by
The condition is_int($currentWorkspace) is always true.
Loading history...
116
            $this->currentWorkspace = $currentWorkspace;
117
        } else {
118
            throw new \InvalidArgumentException('No such workspace defined', 1476048304);
119
        }
120
        $data = [];
121
        $data['data'] = [];
122
        $this->generateDataArray($versions, $filterTxt);
123
        $data['total'] = count($this->dataArray);
124
        $data['data'] = $this->getDataArray($start, $limit);
125
        return $data;
126
    }
127
128
    /**
129
     * Generates grid list array from given versions.
130
     *
131
     * @param array $versions All available version records
132
     * @param string $filterTxt Text to be used to filter record result
133
     */
134
    protected function generateDataArray(array $versions, $filterTxt)
135
    {
136
        $backendUser = $this->getBackendUser();
137
        $workspaceAccess = $backendUser->checkWorkspace($backendUser->workspace);
138
        $swapStage = $workspaceAccess['publish_access'] & 1 ? StagesService::STAGE_PUBLISH_ID : 0;
139
        $swapAccess = $backendUser->workspacePublishAccess($backendUser->workspace);
140
        $this->initializeWorkspacesCachingFramework();
141
        $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
142
        // check for dataArray in cache
143
        if ($this->getDataArrayFromCache($versions, $filterTxt) === false) {
144
            $stagesObj = GeneralUtility::makeInstance(StagesService::class);
145
            $defaultGridColumns = [
146
                self::GridColumn_Collection => 0,
147
                self::GridColumn_CollectionLevel => 0,
148
                self::GridColumn_CollectionParent => '',
149
                self::GridColumn_CollectionCurrent => '',
150
                self::GridColumn_CollectionChildren => 0,
151
            ];
152
            foreach ($versions as $table => $records) {
153
                $table = (string)$table;
154
                $hiddenField = $this->getTcaEnableColumnsFieldName($table, 'disabled');
155
                $isRecordTypeAllowedToModify = $backendUser->check('tables_modify', $table);
156
157
                foreach ($records as $record) {
158
                    $origRecord = (array)BackendUtility::getRecord($table, $record['t3ver_oid']);
159
                    $versionRecord = (array)BackendUtility::getRecord($table, $record['uid']);
160
                    $combinedRecord = CombinedRecord::createFromArrays($table, $origRecord, $versionRecord);
161
                    $this->getIntegrityService()->checkElement($combinedRecord);
162
163
                    if ($hiddenField !== null) {
164
                        $recordState = $this->workspaceState($versionRecord['t3ver_state'], $origRecord[$hiddenField], $versionRecord[$hiddenField]);
165
                    } else {
166
                        $recordState = $this->workspaceState($versionRecord['t3ver_state']);
167
                    }
168
169
                    $isDeletedPage = $table === 'pages' && $recordState === 'deleted';
170
                    $pageId = $table === 'pages' ? $record['uid'] : $record['pid'];
171
                    $viewUrl = GeneralUtility::makeInstance(PreviewUriBuilder::class)->buildUriForElement($table, $record['uid'], $origRecord, $versionRecord);
172
                    $workspaceRecordLabel = BackendUtility::getRecordTitle($table, $versionRecord);
173
                    $liveRecordLabel = BackendUtility::getRecordTitle($table, $origRecord);
174
                    [$pathWorkspaceCropped, $pathWorkspace] = BackendUtility::getRecordPath((int)$record['wspid'], '', 15, 1000);
175
                    $calculatedT3verOid = $record['t3ver_oid'];
176
                    if ((int)$record['t3ver_state'] === VersionState::NEW_PLACEHOLDER) {
177
                        // If we're dealing with a 'new' record, this one has no t3ver_oid. On publish, there is no
178
                        // live counterpart, but the publish methods later need a live uid to publish to. We thus
179
                        // use the uid as t3ver_oid here to be transparent on javascript side.
180
                        $calculatedT3verOid = $record['uid'];
181
                    }
182
183
                    $versionArray = [];
184
                    $versionArray['table'] = $table;
185
                    $versionArray['id'] = $table . ':' . $record['uid'];
186
                    $versionArray['uid'] = $record['uid'];
187
                    $versionArray = array_merge($versionArray, $defaultGridColumns);
188
                    $versionArray['label_Workspace'] = htmlspecialchars($workspaceRecordLabel);
189
                    $versionArray['label_Workspace_crop'] = htmlspecialchars(GeneralUtility::fixed_lgd_cs($workspaceRecordLabel, $backendUser->uc['titleLen']));
190
                    $versionArray['label_Live'] = htmlspecialchars($liveRecordLabel);
191
                    $versionArray['label_Live_crop'] = htmlspecialchars(GeneralUtility::fixed_lgd_cs($liveRecordLabel, $backendUser->uc['titleLen']));
192
                    $versionArray['label_Stage'] = htmlspecialchars($stagesObj->getStageTitle($versionRecord['t3ver_stage']));
193
                    $tempStage = $stagesObj->getNextStage($versionRecord['t3ver_stage']);
194
                    $versionArray['label_nextStage'] = htmlspecialchars($stagesObj->getStageTitle($tempStage['uid']));
195
                    $versionArray['value_nextStage'] = (int)$tempStage['uid'];
196
                    $tempStage = $stagesObj->getPrevStage($versionRecord['t3ver_stage']);
197
                    $versionArray['label_prevStage'] = htmlspecialchars($stagesObj->getStageTitle($tempStage['uid']));
198
                    $versionArray['value_prevStage'] = (int)$tempStage['uid'];
199
                    $versionArray['path_Live'] = htmlspecialchars(BackendUtility::getRecordPath($record['livepid'], '', 999));
0 ignored issues
show
Bug introduced by
It seems like TYPO3\CMS\Backend\Utilit...rd['livepid'], '', 999) can also be of type array<integer,string>; however, parameter $string of htmlspecialchars() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

199
                    $versionArray['path_Live'] = htmlspecialchars(/** @scrutinizer ignore-type */ BackendUtility::getRecordPath($record['livepid'], '', 999));
Loading history...
200
                    $versionArray['path_Workspace'] = htmlspecialchars($pathWorkspace);
201
                    $versionArray['path_Workspace_crop'] = htmlspecialchars($pathWorkspaceCropped);
202
                    $versionArray['workspace_Title'] = htmlspecialchars(WorkspaceService::getWorkspaceTitle($versionRecord['t3ver_wsid']));
203
                    $versionArray['workspace_Tstamp'] = $versionRecord['tstamp'];
204
                    $versionArray['workspace_Formated_Tstamp'] = BackendUtility::datetime($versionRecord['tstamp']);
205
                    $versionArray['t3ver_wsid'] = $versionRecord['t3ver_wsid'];
206
                    $versionArray['t3ver_oid'] = $calculatedT3verOid;
207
                    $versionArray['livepid'] = $record['livepid'];
208
                    $versionArray['stage'] = $versionRecord['t3ver_stage'];
209
                    $versionArray['icon_Live'] = $iconFactory->getIconForRecord($table, $origRecord, Icon::SIZE_SMALL)->render();
210
                    $versionArray['icon_Workspace'] = $iconFactory->getIconForRecord($table, $versionRecord, Icon::SIZE_SMALL)->render();
211
                    $languageValue = $this->getLanguageValue($table, $versionRecord);
212
                    $versionArray['languageValue'] = $languageValue;
213
                    $versionArray['language'] = [
214
                        'icon' => $iconFactory->getIcon($this->getSystemLanguageValue($languageValue, $pageId, 'flagIcon'), Icon::SIZE_SMALL)->render()
215
                    ];
216
                    $versionArray['allowedAction_nextStage'] = $isRecordTypeAllowedToModify && $stagesObj->isNextStageAllowedForUser($versionRecord['t3ver_stage']);
217
                    $versionArray['allowedAction_prevStage'] = $isRecordTypeAllowedToModify && $stagesObj->isPrevStageAllowedForUser($versionRecord['t3ver_stage']);
218
                    if ($swapAccess && $swapStage != 0 && $versionRecord['t3ver_stage'] == $swapStage) {
219
                        $versionArray['allowedAction_publish'] = $isRecordTypeAllowedToModify && $stagesObj->isNextStageAllowedForUser($swapStage);
220
                    } elseif ($swapAccess && $swapStage == 0) {
221
                        $versionArray['allowedAction_publish'] = $isRecordTypeAllowedToModify;
222
                    } else {
223
                        $versionArray['allowedAction_publish'] = false;
224
                    }
225
                    $versionArray['allowedAction_delete'] = $isRecordTypeAllowedToModify;
226
                    // preview and editing of a deleted page won't work ;)
227
                    $versionArray['allowedAction_view'] = !$isDeletedPage && $viewUrl;
228
                    $versionArray['allowedAction_edit'] = $isRecordTypeAllowedToModify && !$isDeletedPage;
229
                    $versionArray['allowedAction_editVersionedPage'] = $isRecordTypeAllowedToModify && !$isDeletedPage;
230
                    $versionArray['state_Workspace'] = $recordState;
231
232
                    if ($filterTxt == '' || $this->isFilterTextInVisibleColumns($filterTxt, $versionArray)) {
233
                        $versionIdentifier = $versionArray['id'];
234
                        $this->dataArray[$versionIdentifier] = $versionArray;
235
                    }
236
                }
237
            }
238
239
            // Trigger a PSR-14 event
240
            $event = new AfterCompiledCacheableDataForWorkspaceEvent($this, $this->dataArray, $versions);
241
            $this->eventDispatcher->dispatch($event);
242
            $this->dataArray = $event->getData();
243
            $versions = $event->getVersions();
244
            // Enrich elements after everything has been processed:
245
            foreach ($this->dataArray as &$element) {
246
                $identifier = $element['table'] . ':' . $element['t3ver_oid'];
247
                $element['integrity'] = [
248
                    'status' => $this->getIntegrityService()->getStatusRepresentation($identifier),
249
                    'messages' => htmlspecialchars((string)$this->getIntegrityService()->getIssueMessages($identifier, true))
250
                ];
251
            }
252
            $this->setDataArrayIntoCache($versions, $filterTxt);
253
        }
254
255
        // Trigger a PSR-14 event
256
        $event = new AfterDataGeneratedForWorkspaceEvent($this, $this->dataArray, $versions);
257
        $this->eventDispatcher->dispatch($event);
258
        $this->dataArray = $event->getData();
259
        $this->sortDataArray();
260
        $this->resolveDataArrayDependencies();
261
    }
262
263
    /**
264
     * Resolves dependencies of nested structures
265
     * and sort data elements considering these dependencies.
266
     */
267
    protected function resolveDataArrayDependencies()
268
    {
269
        $collectionService = $this->getDependencyCollectionService();
270
        $dependencyResolver = $collectionService->getDependencyResolver();
271
272
        foreach ($this->dataArray as $dataElement) {
273
            $dependencyResolver->addElement($dataElement['table'], $dataElement['uid']);
274
        }
275
276
        $this->dataArray = $collectionService->process($this->dataArray);
277
    }
278
279
    /**
280
     * Gets the data array by considering the page to be shown in the grid view.
281
     *
282
     * @param int $start
283
     * @param int $limit
284
     * @return array
285
     */
286
    protected function getDataArray($start, $limit)
287
    {
288
        $dataArrayPart = [];
289
        $dataArrayCount = count($this->dataArray);
290
        $end = ($start + $limit < $dataArrayCount ? $start + $limit : $dataArrayCount);
291
292
        // Ensure that there are numerical indexes
293
        $this->dataArray = array_values($this->dataArray);
294
        for ($i = $start; $i < $end; $i++) {
295
            $dataArrayPart[] = $this->dataArray[$i];
296
        }
297
298
        // Ensure that collections are not cut for the pagination
299
        if (!empty($this->dataArray[$i][self::GridColumn_Collection])) {
300
            $collectionIdentifier = $this->dataArray[$i][self::GridColumn_Collection];
301
            for ($i = $i + 1; $i < $dataArrayCount && $collectionIdentifier === $this->dataArray[$i][self::GridColumn_Collection]; $i++) {
302
                $dataArrayPart[] = $this->dataArray[$i];
303
            }
304
        }
305
306
        // Trigger a PSR-14 event
307
        $event = new GetVersionedDataEvent($this, $this->dataArray, $start, $limit, $dataArrayPart);
308
        $this->eventDispatcher->dispatch($event);
309
        $this->dataArray = $event->getData();
310
        return $event->getDataArrayPart();
311
    }
312
313
    /**
314
     * Initializes the workspace cache
315
     */
316
    protected function initializeWorkspacesCachingFramework()
317
    {
318
        $this->workspacesCache = GeneralUtility::makeInstance(CacheManager::class)->getCache('workspaces_cache');
319
    }
320
321
    /**
322
     * Puts the generated dataArray into the workspace cache.
323
     *
324
     * @param array $versions All records uids etc. First key is table name, second key incremental integer. Records are associative arrays with uid and t3ver_oid fields. The pid of the online record is found as "livepid" the pid of the offline record is found in "wspid
325
     * @param string $filterTxt The given filter text from the grid.
326
     */
327
    protected function setDataArrayIntoCache(array $versions, $filterTxt)
328
    {
329
        $hash = $this->calculateHash($versions, $filterTxt);
330
        $this->workspacesCache->set(
331
            $hash,
332
            $this->dataArray,
333
            [
334
                (string)$this->currentWorkspace,
335
                'user_' . $this->getBackendUser()->user['uid']
336
            ]
337
        );
338
    }
339
340
    /**
341
     * Checks if a cache entry is given for given versions and filter text and tries to load the data array from cache.
342
     *
343
     * @param array $versions All records uids etc. First key is table name, second key incremental integer. Records are associative arrays with uid and t3ver_oid fields. The pid of the online record is found as "livepid" the pid of the offline record is found in "wspid
344
     * @param string $filterTxt The given filter text from the grid.
345
     * @return bool TRUE if cache entry was successfully fetched from cache and content put to $this->dataArray
346
     */
347
    protected function getDataArrayFromCache(array $versions, $filterTxt)
348
    {
349
        $cacheEntry = false;
350
        $hash = $this->calculateHash($versions, $filterTxt);
351
        $content = $this->workspacesCache->get($hash);
352
        if ($content !== false) {
353
            $this->dataArray = $content;
354
            $cacheEntry = true;
355
        }
356
        return $cacheEntry;
357
    }
358
359
    /**
360
     * Calculates the hash value of the used workspace, the user id, the versions array, the filter text, the sorting attribute, the workspace selected in grid and the sorting direction.
361
     *
362
     * @param array $versions All records uids etc. First key is table name, second key incremental integer. Records are associative arrays with uid and t3ver_oid fields. The pid of the online record is found as "livepid" the pid of the offline record is found in "wspid
363
     * @param string $filterTxt The given filter text from the grid.
364
     * @return string
365
     */
366
    protected function calculateHash(array $versions, $filterTxt)
367
    {
368
        $backendUser = $this->getBackendUser();
369
        $hashArray = [
370
            $backendUser->workspace,
371
            $backendUser->user['uid'],
372
            $versions,
373
            $filterTxt,
374
            $this->sort,
375
            $this->sortDir,
376
            $this->currentWorkspace
377
        ];
378
        $hash = md5(serialize($hashArray));
379
        return $hash;
380
    }
381
382
    /**
383
     * Performs sorting on the data array accordant to the
384
     * selected column in the grid view to be used for sorting.
385
     */
386
    protected function sortDataArray()
387
    {
388
        if (is_array($this->dataArray)) {
0 ignored issues
show
introduced by
The condition is_array($this->dataArray) is always true.
Loading history...
389
            switch ($this->sort) {
390
                case 'uid':
391
                case 'change':
392
                case 'workspace_Tstamp':
393
                case 't3ver_oid':
394
                case 'liveid':
395
                case 'livepid':
396
                case 'languageValue':
397
                    uasort($this->dataArray, [$this, 'intSort']);
398
                    break;
399
                case 'label_Workspace':
400
                case 'label_Live':
401
                case 'label_Stage':
402
                case 'workspace_Title':
403
                case 'path_Live':
404
                    // case 'path_Workspace': This is the first sorting attribute
405
                    uasort($this->dataArray, [$this, 'stringSort']);
406
                    break;
407
                default:
408
                    // Do nothing
409
            }
410
        } else {
411
            $this->logger->critical('Try to sort "' . $this->sort . '" in "\\TYPO3\\CMS\\Workspaces\\Service\\GridDataService::sortDataArray" but $this->dataArray is empty! This might be the bug #26422 which could not be reproduced yet.');
412
        }
413
        // Trigger an event for extensibility
414
        $event = new SortVersionedDataEvent($this, $this->dataArray, $this->sort, $this->sortDir);
415
        $this->eventDispatcher->dispatch($event);
416
        $this->dataArray = $event->getData();
417
        $this->sort = $event->getSortColumn();
418
        $this->sortDir = $event->getSortDirection();
419
    }
420
421
    /**
422
     * Implements individual sorting for columns based on integer comparison.
423
     *
424
     * @param array $a First value
425
     * @param array $b Second value
426
     * @return int
427
     */
428
    protected function intSort(array $a, array $b)
429
    {
430
        if (!$this->isSortable($a, $b)) {
431
            return 0;
432
        }
433
        // First sort by using the page-path in current workspace
434
        $path_cmp = strcasecmp($a['path_Workspace'], $b['path_Workspace']);
435
        if ($path_cmp < 0) {
436
            return $path_cmp;
437
        }
438
        if ($path_cmp == 0) {
439
            if ($a[$this->sort] == $b[$this->sort]) {
440
                return 0;
441
            }
442
            if ($this->sortDir === 'ASC') {
443
                return $a[$this->sort] < $b[$this->sort] ? -1 : 1;
444
            }
445
            if ($this->sortDir === 'DESC') {
446
                return $a[$this->sort] > $b[$this->sort] ? -1 : 1;
447
            }
448
        } elseif ($path_cmp > 0) {
449
            return $path_cmp;
450
        }
451
        return 0;
452
    }
453
454
    /**
455
     * Implements individual sorting for columns based on string comparison.
456
     *
457
     * @param array $a First value
458
     * @param array $b Second value
459
     * @return int
460
     */
461
    protected function stringSort($a, $b)
462
    {
463
        if (!$this->isSortable($a, $b)) {
464
            return 0;
465
        }
466
        $path_cmp = strcasecmp($a['path_Workspace'], $b['path_Workspace']);
467
        if ($path_cmp < 0) {
468
            return $path_cmp;
469
        }
470
        if ($path_cmp == 0) {
471
            if ($a[$this->sort] == $b[$this->sort]) {
472
                return 0;
473
            }
474
            if ($this->sortDir === 'ASC') {
475
                return strcasecmp($a[$this->sort], $b[$this->sort]);
476
            }
477
            if ($this->sortDir === 'DESC') {
478
                return strcasecmp($a[$this->sort], $b[$this->sort]) * -1;
479
            }
480
        } elseif ($path_cmp > 0) {
481
            return $path_cmp;
482
        }
483
        return 0;
484
    }
485
486
    /**
487
     * Determines whether dataArray elements are sortable.
488
     * Only elements on the first level (0) or below the same
489
     * parent element are directly sortable.
490
     *
491
     * @param array $a
492
     * @param array $b
493
     * @return bool
494
     */
495
    protected function isSortable(array $a, array $b)
496
    {
497
        return
498
            $a[self::GridColumn_CollectionLevel] === 0 && $b[self::GridColumn_CollectionLevel] === 0
499
            || $a[self::GridColumn_CollectionParent] === $b[self::GridColumn_CollectionParent]
500
        ;
501
    }
502
503
    /**
504
     * Determines whether the text used to filter the results is part of
505
     * a column that is visible in the grid view.
506
     *
507
     * @param string $filterText
508
     * @param array $versionArray
509
     * @return bool
510
     */
511
    protected function isFilterTextInVisibleColumns($filterText, array $versionArray)
512
    {
513
        $backendUser = $this->getBackendUser();
514
        if (is_array($backendUser->uc['moduleData']['Workspaces'][$backendUser->workspace]['columns'])) {
515
            $visibleColumns = $backendUser->uc['moduleData']['Workspaces'][$backendUser->workspace]['columns'];
516
        } else {
517
            $visibleColumns = [
518
                'workspace_Formated_Tstamp' => ['hidden' => 0],
519
                'change' => ['hidden' => 0],
520
                'path_Workspace' => ['hidden' => 0],
521
                'path_Live' => ['hidden' => 0],
522
                'label_Live' => ['hidden' => 0],
523
                'label_Stage' => ['hidden' => 0],
524
                'label_Workspace' => ['hidden' => 0],
525
            ];
526
        }
527
        foreach ($visibleColumns as $column => $value) {
528
            if (isset($value['hidden']) && isset($column) && isset($versionArray[$column])) {
529
                if ($value['hidden'] == 0) {
530
                    switch ($column) {
531
                        case 'workspace_Tstamp':
532
                            if (stripos($versionArray['workspace_Formated_Tstamp'], $filterText) !== false) {
533
                                return true;
534
                            }
535
                            break;
536
                        case 'change':
537
                            if (stripos((string)$versionArray[$column], str_replace('%', '', $filterText)) !== false) {
538
                                return true;
539
                            }
540
                            break;
541
                        default:
542
                            if (stripos((string)$versionArray[$column], $filterText) !== false) {
543
                                return true;
544
                            }
545
                    }
546
                }
547
            }
548
        }
549
        return false;
550
    }
551
552
    /**
553
     * Gets the state of a given state value.
554
     *
555
     * @param int $stateId stateId of offline record
556
     * @param bool $hiddenOnline hidden status of online record
557
     * @param bool $hiddenOffline hidden status of offline record
558
     * @return string
559
     */
560
    protected function workspaceState($stateId, $hiddenOnline = false, $hiddenOffline = false)
561
    {
562
        $hiddenState = null;
563
        if ($hiddenOnline == 0 && $hiddenOffline == 1) {
564
            $hiddenState = 'hidden';
565
        } elseif ($hiddenOnline == 1 && $hiddenOffline == 0) {
566
            $hiddenState = 'unhidden';
567
        }
568
        switch ($stateId) {
569
            case VersionState::NEW_PLACEHOLDER:
570
                $state = 'new';
571
                break;
572
            case VersionState::DELETE_PLACEHOLDER:
573
                $state = 'deleted';
574
                break;
575
            case VersionState::MOVE_POINTER:
576
                $state = 'moved';
577
                break;
578
            default:
579
                $state = ($hiddenState ?: 'modified');
580
        }
581
        return $state;
582
    }
583
584
    /**
585
     * Gets the field name of the enable-columns as defined in $TCA.
586
     *
587
     * @param string $table Name of the table
588
     * @param string $type Type to be fetches (e.g. 'disabled', 'starttime', 'endtime', 'fe_group)
589
     * @return string|null The accordant field name or NULL if not defined
590
     */
591
    protected function getTcaEnableColumnsFieldName($table, $type)
592
    {
593
        $fieldName = null;
594
595
        if (!empty($GLOBALS['TCA'][$table]['ctrl']['enablecolumns'][$type])) {
596
            $fieldName = $GLOBALS['TCA'][$table]['ctrl']['enablecolumns'][$type];
597
        }
598
599
        return $fieldName;
600
    }
601
602
    /**
603
     * Gets the used language value (sys_language.uid) of
604
     * a given database record.
605
     *
606
     * @param string $table Name of the table
607
     * @param array $record Database record
608
     * @return int
609
     */
610
    protected function getLanguageValue($table, array $record)
611
    {
612
        $languageValue = 0;
613
        if (BackendUtility::isTableLocalizable($table)) {
614
            $languageField = $GLOBALS['TCA'][$table]['ctrl']['languageField'];
615
            if (!empty($record[$languageField])) {
616
                $languageValue = $record[$languageField];
617
            }
618
        }
619
        return $languageValue;
620
    }
621
622
    /**
623
     * Gets a named value of the available sys_language elements.
624
     *
625
     * @param int $id sys_language uid
626
     * @param int $pageId page id of a site
627
     * @param string $key Name of the value to be fetched (e.g. title)
628
     * @return string|null
629
     * @see getSystemLanguages
630
     */
631
    protected function getSystemLanguageValue($id, $pageId, $key)
632
    {
633
        $value = null;
634
        $systemLanguages = $this->getSystemLanguages((int)$pageId);
635
        if (!empty($systemLanguages[$id][$key])) {
636
            $value = $systemLanguages[$id][$key];
637
        }
638
        return $value;
639
    }
640
641
    /**
642
     * Gets all available system languages.
643
     *
644
     * @param int $pageId
645
     * @return array
646
     */
647
    public function getSystemLanguages(int $pageId)
648
    {
649
        return GeneralUtility::makeInstance(TranslationConfigurationProvider::class)->getSystemLanguages($pageId);
650
    }
651
652
    /**
653
     * Gets an instance of the integrity service.
654
     *
655
     * @return IntegrityService
656
     */
657
    protected function getIntegrityService()
658
    {
659
        if (!isset($this->integrityService)) {
660
            $this->integrityService = GeneralUtility::makeInstance(IntegrityService::class);
661
        }
662
        return $this->integrityService;
663
    }
664
665
    /**
666
     * @return Dependency\CollectionService
667
     */
668
    protected function getDependencyCollectionService()
669
    {
670
        return GeneralUtility::makeInstance(CollectionService::class);
671
    }
672
673
    /**
674
     * @return BackendUserAuthentication
675
     */
676
    protected function getBackendUser()
677
    {
678
        return $GLOBALS['BE_USER'];
679
    }
680
}
681