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