Completed
Push — master ( 22e1bb...96416a )
by Torben
04:49
created

CategoryService   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 0
cbo 0
dl 0
loc 51
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getCategoryListWithChilds() 0 4 1
B getChildrenCategoriesRecursive() 0 29 5
1
<?php
2
namespace DERHANSEN\SfEventMgt\Service;
3
4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
 * It is free software; you can redistribute it and/or modify it under
8
 * the terms of the GNU General Public License, either version 2
9
 * of the License, or any later version.
10
 *
11
 * For the full copyright and license information, please read the
12
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
 * The TYPO3 project - inspiring people to share!
15
 */
16
17
/**
18
 * CategoryService
19
 *
20
 * @author Georg Ringer <[email protected]>
21
 * @author Torben Hansen <[email protected]>
22
 */
23
class CategoryService
24
{
25
26
    /**
27
     * Returns the given categories including their subcategories
28
     *
29
     * @param string $categories
30
     * @return string
31
     */
32
    public static function getCategoryListWithChilds($categories)
33
    {
34
        return self::getChildrenCategoriesRecursive($categories);
35
    }
36
37
    /**
38
     * Get child categories
39
     *
40
     * @param string $categoryList list of category ids to start
41
     * @param int $counter
42
     * @return string comma separated list of category ids
43
     */
44
    private function getChildrenCategoriesRecursive($categoryList, $counter = 0)
45
    {
46
        $result = [];
47
48
        // add idlist to the output too
49
        if ($counter === 0) {
50
            $result[] = $GLOBALS['TYPO3_DB']->cleanIntList($categoryList);
51
        }
52
53
        $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
54
            'uid',
55
            'sys_category',
56
            'sys_category.parent IN (' . $GLOBALS['TYPO3_DB']->cleanIntList($categoryList) . ') AND deleted=0'
57
        );
58
59
        while (($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))) {
60
            $counter++;
61
            if ($counter > 10000) {
62
                $GLOBALS['TT']->setTSlogMessage('EXT:sf_event_mgt: one or more recursive categories where found');
63
                return implode(',', $result);
64
            }
65
            $subcategories = self::getChildrenCategoriesRecursive($row['uid'], $counter);
66
            $result[] = $row['uid'] . ($subcategories ? ',' . $subcategories : '');
67
        }
68
        $GLOBALS['TYPO3_DB']->sql_free_result($res);
69
70
        $result = implode(',', $result);
71
        return $result;
72
    }
73
}