PageUtility   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 37
dl 0
loc 67
rs 10
c 1
b 0
f 0
wmc 16

2 Methods

Rating   Name   Duplication   Size   Complexity  
A extendPidListByChildren() 0 16 4
C getTreeList() 0 41 12
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Extension "sf_event_mgt" for TYPO3 CMS.
7
 *
8
 * For the full copyright and license information, please read the
9
 * LICENSE.txt file that was distributed with this source code.
10
 */
11
12
namespace DERHANSEN\SfEventMgt\Utility;
13
14
use TYPO3\CMS\Core\Database\Connection;
15
use TYPO3\CMS\Core\Database\ConnectionPool;
16
use TYPO3\CMS\Core\Database\Query\QueryHelper;
17
use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
18
use TYPO3\CMS\Core\Utility\GeneralUtility;
19
20
class PageUtility
21
{
22
    /**
23
     * Find all ids from given comma separated list of PIDs and level
24
     */
25
    public static function extendPidListByChildren(string $pidList = '', int $recursive = 0): string
26
    {
27
        if ($recursive <= 0) {
28
            return $pidList;
29
        }
30
31
        $recursiveStoragePids = $pidList;
32
        $storagePids = GeneralUtility::intExplode(',', $pidList);
33
        foreach ($storagePids as $startPid) {
34
            $pids = self::getTreeList($startPid, $recursive);
35
            if (strlen($pids) > 0) {
36
                $recursiveStoragePids .= ',' . $pids;
37
            }
38
        }
39
40
        return $recursiveStoragePids;
41
    }
42
43
    /**
44
     * Recursively fetch all descendants of a given page
45
     */
46
    protected static function getTreeList(int $id, int $depth, int $begin = 0, string $permClause = ''): string
47
    {
48
        if ($id < 0) {
49
            $id = (int)abs($id);
50
        }
51
52
        if ($begin === 0) {
53
            $theList = $id;
54
        } else {
55
            $theList = '';
56
        }
57
58
        if ($id && $depth > 0) {
59
            $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
60
            $queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
61
            $queryBuilder->select('uid')
62
                ->from('pages')
63
                ->where(
64
                    $queryBuilder->expr()->eq('pid', $queryBuilder->createNamedParameter($id, Connection::PARAM_INT)),
65
                    $queryBuilder->expr()->eq('sys_language_uid', 0)
66
                )
67
                ->orderBy('uid');
68
            if ($permClause !== '') {
69
                $queryBuilder->andWhere(QueryHelper::stripLogicalOperatorPrefix($permClause));
70
            }
71
            $statement = $queryBuilder->executeQuery();
72
            while ($row = $statement->fetchAssociative()) {
73
                if ($begin <= 0) {
74
                    $theList .= ',' . $row['uid'];
75
                }
76
                if ($depth > 1) {
77
                    $theSubList = self::getTreeList((int)$row['uid'], $depth - 1, $begin - 1, $permClause);
78
                    if (!empty($theList) && !empty($theSubList) && ($theSubList[0] !== ',')) {
79
                        $theList .= ',';
80
                    }
81
                    $theList .= $theSubList;
82
                }
83
            }
84
        }
85
86
        return (string)$theList;
87
    }
88
}
89