Passed
Push — master ( 2ed7e8...9bc0b7 )
by
unknown
18:09
created

GridDataService::getIntegrityService()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
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\Controller\Remote\RemoteServer;
30
use TYPO3\CMS\Workspaces\Domain\Model\CombinedRecord;
31
use TYPO3\CMS\Workspaces\Event\AfterCompiledCacheableDataForWorkspaceEvent;
32
use TYPO3\CMS\Workspaces\Event\AfterDataGeneratedForWorkspaceEvent;
33
use TYPO3\CMS\Workspaces\Event\GetVersionedDataEvent;
34
use TYPO3\CMS\Workspaces\Event\SortVersionedDataEvent;
35
use TYPO3\CMS\Workspaces\Preview\PreviewUriBuilder;
36
use TYPO3\CMS\Workspaces\Service\Dependency\CollectionService;
37
38
/**
39
 * Grid data service
40
 */
41
class GridDataService implements LoggerAwareInterface
42
{
43
    use LoggerAwareTrait;
44
45
    const GridColumn_Collection = 'Workspaces_Collection';
46
    const GridColumn_CollectionLevel = 'Workspaces_CollectionLevel';
47
    const GridColumn_CollectionParent = 'Workspaces_CollectionParent';
48
    const GridColumn_CollectionCurrent = 'Workspaces_CollectionCurrent';
49
    const GridColumn_CollectionChildren = 'Workspaces_CollectionChildren';
50
51
    /**
52
     * Id of the current active workspace.
53
     *
54
     * @var int
55
     */
56
    protected $currentWorkspace;
57
58
    /**
59
     * Version record information (filtered, sorted and limited)
60
     *
61
     * @var array
62
     */
63
    protected $dataArray = [];
64
65
    /**
66
     * Name of the field used for sorting.
67
     *
68
     * @var string
69
     */
70
    protected $sort = '';
71
72
    /**
73
     * Direction used for sorting (ASC, DESC).
74
     *
75
     * @var string
76
     */
77
    protected $sortDir = '';
78
79
    /**
80
     * @var \TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
81
     */
82
    protected $workspacesCache;
83
84
    /**
85
     * @var IntegrityService
86
     */
87
    protected $integrityService;
88
89
    /**
90
     * @var EventDispatcherInterface
91
     */
92
    protected $eventDispatcher;
93
94
    public function __construct(EventDispatcherInterface $eventDispatcher)
95
    {
96
        $this->eventDispatcher = $eventDispatcher;
97
    }
98
99
    /**
100
     * Generates grid list array from given versions.
101
     *
102
     * @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
103
     * @param \stdClass $parameter Parameters as submitted by JavaScript component
104
     * @param int $currentWorkspace The current workspace
105
     * @return array Version record information (filtered, sorted and limited)
106
     * @throws \InvalidArgumentException
107
     */
108
    public function generateGridListFromVersions($versions, $parameter, $currentWorkspace)
109
    {
110
        // Read the given parameters from grid. If the parameter is not set use default values.
111
        $filterTxt = $parameter->filterTxt ?? '';
112
        $start = isset($parameter->start) ? (int)$parameter->start : 0;
113
        $limit = isset($parameter->limit) ? (int)$parameter->limit : 30;
114
        $this->sort = $parameter->sort ?? 't3ver_oid';
115
        $this->sortDir = $parameter->dir ?? 'ASC';
116
        if (is_int($currentWorkspace)) {
0 ignored issues
show
introduced by
The condition is_int($currentWorkspace) is always true.
Loading history...
117
            $this->currentWorkspace = $currentWorkspace;
118
        } else {
119
            throw new \InvalidArgumentException('No such workspace defined', 1476048304);
120
        }
121
        $data = [];
122
        $data['data'] = [];
123
        $this->generateDataArray($versions, $filterTxt);
124
        $data['total'] = count($this->dataArray);
125
        $data['data'] = $this->getDataArray($start, $limit);
126
        return $data;
127
    }
128
129
    /**
130
     * Generates grid list array from given versions.
131
     *
132
     * @param array $versions All available version records
133
     * @param string $filterTxt Text to be used to filter record result
134
     */
135
    protected function generateDataArray(array $versions, $filterTxt)
136
    {
137
        $backendUser = $this->getBackendUser();
138
        $workspaceAccess = $backendUser->checkWorkspace($backendUser->workspace);
139
        $swapStage = $workspaceAccess['publish_access'] & 1 ? StagesService::STAGE_PUBLISH_ID : 0;
140
        $swapAccess = $backendUser->workspacePublishAccess($backendUser->workspace);
141
        $this->initializeWorkspacesCachingFramework();
142
        $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
143
        // check for dataArray in cache
144
        if ($this->getDataArrayFromCache($versions, $filterTxt) === false) {
145
            $stagesObj = GeneralUtility::makeInstance(StagesService::class);
146
            $defaultGridColumns = [
147
                self::GridColumn_Collection => 0,
148
                self::GridColumn_CollectionLevel => 0,
149
                self::GridColumn_CollectionParent => '',
150
                self::GridColumn_CollectionCurrent => '',
151
                self::GridColumn_CollectionChildren => 0,
152
            ];
153
            foreach ($versions as $table => $records) {
154
                $table = (string)$table;
155
                $hiddenField = $this->getTcaEnableColumnsFieldName($table, 'disabled');
156
                $isRecordTypeAllowedToModify = $backendUser->check('tables_modify', $table);
157
158
                foreach ($records as $record) {
159
                    $origRecord = (array)BackendUtility::getRecord($table, $record['t3ver_oid']);
160
                    $versionRecord = (array)BackendUtility::getRecord($table, $record['uid']);
161
                    $combinedRecord = CombinedRecord::createFromArrays($table, $origRecord, $versionRecord);
162
                    $hasDiff = $this->versionIsModified($combinedRecord);
163
                    $this->getIntegrityService()->checkElement($combinedRecord);
164
165
                    if ($hiddenField !== null) {
166
                        $recordState = $this->workspaceState($versionRecord['t3ver_state'], $origRecord[$hiddenField], $versionRecord[$hiddenField], $hasDiff);
167
                    } else {
168
                        $recordState = $this->workspaceState($versionRecord['t3ver_state'], $hasDiff);
169
                    }
170
171
                    $isDeletedPage = $table === 'pages' && $recordState === 'deleted';
172
                    $pageId = $table === 'pages' ? $record['uid'] : $record['pid'];
173
                    $viewUrl = GeneralUtility::makeInstance(PreviewUriBuilder::class)->buildUriForElement($table, $record['uid'], $origRecord, $versionRecord);
174
                    $workspaceRecordLabel = BackendUtility::getRecordTitle($table, $versionRecord);
175
                    $liveRecordLabel = BackendUtility::getRecordTitle($table, $origRecord);
176
                    [$pathWorkspaceCropped, $pathWorkspace] = BackendUtility::getRecordPath((int)$record['wspid'], '', 15, 1000);
177
                    $calculatedT3verOid = $record['t3ver_oid'];
178
                    if ((int)$record['t3ver_state'] === VersionState::NEW_PLACEHOLDER) {
179
                        // If we're dealing with a 'new' record, this one has no t3ver_oid. On publish, there is no
180
                        // live counterpart, but the publish methods later need a live uid to publish to. We thus
181
                        // use the uid as t3ver_oid here to be transparent on javascript side.
182
                        $calculatedT3verOid = $record['uid'];
183
                    }
184
185
                    $versionArray = [];
186
                    $versionArray['table'] = $table;
187
                    $versionArray['id'] = $table . ':' . $record['uid'];
188
                    $versionArray['uid'] = $record['uid'];
189
                    $versionArray = array_merge($versionArray, $defaultGridColumns);
190
                    $versionArray['label_Workspace'] = htmlspecialchars($workspaceRecordLabel);
191
                    $versionArray['label_Workspace_crop'] = htmlspecialchars(GeneralUtility::fixed_lgd_cs($workspaceRecordLabel, $backendUser->uc['titleLen']));
192
                    $versionArray['label_Live'] = htmlspecialchars($liveRecordLabel);
193
                    $versionArray['label_Live_crop'] = htmlspecialchars(GeneralUtility::fixed_lgd_cs($liveRecordLabel, $backendUser->uc['titleLen']));
194
                    $versionArray['label_Stage'] = htmlspecialchars($stagesObj->getStageTitle($versionRecord['t3ver_stage']));
195
                    $tempStage = $stagesObj->getNextStage($versionRecord['t3ver_stage']);
196
                    $versionArray['label_nextStage'] = htmlspecialchars($stagesObj->getStageTitle($tempStage['uid']));
197
                    $versionArray['value_nextStage'] = (int)$tempStage['uid'];
198
                    $tempStage = $stagesObj->getPrevStage($versionRecord['t3ver_stage']);
199
                    $versionArray['label_prevStage'] = htmlspecialchars($stagesObj->getStageTitle($tempStage['uid']));
200
                    $versionArray['value_prevStage'] = (int)$tempStage['uid'];
201
                    $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

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