Welcome to mirror list, hosted at ThFree Co, Russian Federation.

CategoryList.php « Category « core - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 88b72b733fd9054436d78b26387d1644dcf5ebb2 (plain)
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
<?php
/**
 * Piwik - free/libre analytics platform
 *
 * @link https://matomo.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */
namespace Piwik\Category;

use Piwik\Container\StaticContainer;

/**
 * Base type for category. lets you change the name for a categoryId and specifiy a different order
 * so the category appears eg at a different order in the reporting menu.
 *
 * This class is for now not exposed as public API until needed. Categories of plugins will be automatically
 * displayed in the menu at the very right after all core categories.
 */
class CategoryList
{
    /**
     * @var Category[] indexed by categoryId
     */
    private $categories = array();

    public function addCategory(Category $category)
    {
        $categoryId = $category->getId();

        if ($this->hasCategory($categoryId)) {
            throw new \Exception(sprintf('Category %s already exists', $categoryId));
        }

        $this->categories[$categoryId] = $category;
    }

    public function getCategories()
    {
        return $this->categories;
    }

    public function hasCategory($categoryId)
    {
        return isset($this->categories[$categoryId]);
    }

    /**
     * Get the category having the given id, if possible.
     *
     * @param string $categoryId
     * @return Category|null
     */
    public function getCategory($categoryId)
    {
        if ($this->hasCategory($categoryId)) {
            return $this->categories[$categoryId];
        }
    }

    /**
     * @return CategoryList
     */
    public static function get()
    {
        $list = new CategoryList();

        $categories = StaticContainer::get('Piwik\Plugin\Categories');

        foreach ($categories->getAllCategories() as $category) {
            $list->addCategory($category);
        }

        // move subcategories into categories
        foreach ($categories->getAllSubcategories() as $subcategory) {
            $categoryId = $subcategory->getCategoryId();

            if (!$categoryId) {
                continue;
            }

            if ($list->hasCategory($categoryId)) {
                $category = $list->getCategory($categoryId);
            } else {
                $category = new Category();
                $category->setId($categoryId);
                $list->addCategory($category);
            }

            $category->addSubcategory($subcategory);
        }

        return $list;
    }
}