Commit | Line | Data |
---|---|---|
13d8b49b | 1 | <?php |
a9229942 | 2 | |
13d8b49b | 3 | namespace wcf\system\category; |
a9229942 | 4 | |
2b770bdd | 5 | use wcf\data\category\Category; |
7a23a706 | 6 | use wcf\data\object\type\ObjectType; |
bcb9c1bf | 7 | use wcf\data\object\type\ObjectTypeCache; |
b401cd0d | 8 | use wcf\system\cache\builder\CategoryCacheBuilder; |
c6c381eb | 9 | use wcf\system\exception\SystemException; |
bcb9c1bf | 10 | use wcf\system\SingletonFactory; |
13d8b49b MS |
11 | |
12 | /** | |
a17de04e | 13 | * Handles the categories. |
a9229942 TD |
14 | * |
15 | * @author Matthias Schmidt | |
16 | * @copyright 2001-2019 WoltLab GmbH | |
17 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> | |
13d8b49b | 18 | */ |
a9229942 TD |
19 | class CategoryHandler extends SingletonFactory |
20 | { | |
21 | /** | |
22 | * cached categories | |
23 | * @var Category[] | |
24 | */ | |
25 | protected $categories = []; | |
26 | ||
27 | /** | |
28 | * category ids grouped by the object type they belong to | |
29 | * @var int[][] | |
30 | */ | |
31 | protected $objectTypeCategoryIDs = []; | |
32 | ||
33 | /** | |
34 | * maps the names of the category object types to the object type ids | |
35 | * @var int[] | |
36 | */ | |
37 | protected $objectTypeIDs = []; | |
38 | ||
39 | /** | |
40 | * list of category object types | |
41 | * @var ObjectType[] | |
42 | */ | |
43 | protected $objectTypes = []; | |
44 | ||
45 | /** | |
46 | * Returns all category objects with the given object type. If no object | |
47 | * type is given, all categories grouped by object type are returned. | |
48 | * | |
49 | * @param string $objectType | |
50 | * @return Category[]|Category[][] | |
51 | */ | |
52 | public function getCategories($objectType = null) | |
53 | { | |
54 | $categories = []; | |
55 | if ($objectType === null) { | |
56 | foreach ($this->objectTypes as $objectType) { | |
57 | $categories[$objectType->objectType] = $this->getCategories($objectType->objectType); | |
58 | } | |
59 | } elseif (isset($this->objectTypeCategoryIDs[$objectType])) { | |
60 | foreach ($this->objectTypeCategoryIDs[$objectType] as $categoryID) { | |
61 | $categories[$categoryID] = $this->getCategory($categoryID); | |
62 | } | |
63 | } | |
64 | ||
65 | return $categories; | |
66 | } | |
67 | ||
68 | /** | |
69 | * Returns the category with the given id or `null` if no such category exists. | |
70 | * | |
71 | * @param int $categoryID | |
72 | * @return Category|null | |
73 | */ | |
74 | public function getCategory($categoryID) | |
75 | { | |
813c41ce | 76 | return $this->categories[$categoryID] ?? null; |
a9229942 TD |
77 | } |
78 | ||
79 | /** | |
80 | * Returns the child categories of the category with the given id. | |
81 | * | |
82 | * The second parameter is only needed if $categoryID is 0. | |
83 | * | |
84 | * @param int $categoryID | |
85 | * @param int $objectTypeID | |
86 | * @return Category[] | |
87 | * @throws SystemException | |
88 | */ | |
89 | public function getChildCategories($categoryID, $objectTypeID = null) | |
90 | { | |
91 | if (!$categoryID && $objectTypeID === null) { | |
92 | throw new SystemException("Missing object type id"); | |
93 | } | |
94 | ||
95 | $categories = []; | |
96 | foreach ($this->categories as $category) { | |
97 | if ( | |
98 | $category->parentCategoryID == $categoryID | |
99 | && ($categoryID || $category->objectTypeID == $objectTypeID) | |
100 | ) { | |
101 | $categories[$category->categoryID] = $category; | |
102 | } | |
103 | } | |
104 | ||
105 | return $categories; | |
106 | } | |
107 | ||
108 | /** | |
109 | * Returns the category object type with the given id or `null` if no such object type exists. | |
110 | * | |
111 | * @param int $objectTypeID | |
112 | * @return ObjectType|null | |
113 | */ | |
114 | public function getObjectType($objectTypeID) | |
115 | { | |
116 | if (isset($this->objectTypeIDs[$objectTypeID])) { | |
117 | return $this->getObjectTypeByName($this->objectTypeIDs[$objectTypeID]); | |
118 | } | |
5227ebc7 MS |
119 | |
120 | return null; | |
a9229942 TD |
121 | } |
122 | ||
123 | /** | |
124 | * Returns the category object type with the given name or `null` if no such object type exists. | |
125 | * | |
126 | * @param string $objectType | |
127 | * @return ObjectType|null | |
128 | */ | |
129 | public function getObjectTypeByName($objectType) | |
130 | { | |
813c41ce | 131 | return $this->objectTypes[$objectType] ?? null; |
a9229942 TD |
132 | } |
133 | ||
134 | /** | |
135 | * Returns all category object types. | |
136 | * | |
137 | * @return ObjectType[] | |
138 | */ | |
139 | public function getObjectTypes() | |
140 | { | |
141 | return $this->objectTypes; | |
142 | } | |
143 | ||
144 | /** | |
145 | * @inheritDoc | |
146 | */ | |
147 | protected function init() | |
148 | { | |
149 | $this->objectTypes = ObjectTypeCache::getInstance()->getObjectTypes('com.woltlab.wcf.category'); | |
150 | foreach ($this->objectTypes as $objectType) { | |
151 | $this->objectTypeIDs[$objectType->objectTypeID] = $objectType->objectType; | |
152 | } | |
153 | ||
154 | $this->categories = CategoryCacheBuilder::getInstance()->getData([], 'categories'); | |
155 | $this->objectTypeCategoryIDs = CategoryCacheBuilder::getInstance()->getData([], 'objectTypeCategoryIDs'); | |
156 | } | |
157 | ||
158 | /** | |
159 | * Reloads the category cache. | |
160 | */ | |
161 | public function reloadCache() | |
162 | { | |
163 | CategoryCacheBuilder::getInstance()->reset(); | |
164 | ||
165 | $this->init(); | |
166 | } | |
13d8b49b | 167 | } |