3 namespace wcf\system\category
;
5 use wcf\data\category\Category
;
6 use wcf\data\
object\type\ObjectType
;
7 use wcf\data\
object\type\ObjectTypeCache
;
8 use wcf\system\cache\builder\CategoryCacheBuilder
;
9 use wcf\system\exception\SystemException
;
10 use wcf\system\SingletonFactory
;
13 * Handles the categories.
15 * @author Matthias Schmidt
16 * @copyright 2001-2019 WoltLab GmbH
17 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
19 class CategoryHandler
extends SingletonFactory
25 protected $categories = [];
28 * category ids grouped by the object type they belong to
31 protected $objectTypeCategoryIDs = [];
34 * maps the names of the category object types to the object type ids
37 protected $objectTypeIDs = [];
40 * list of category object types
43 protected $objectTypes = [];
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.
49 * @param string $objectType
50 * @return Category[]|Category[][]
52 public function getCategories($objectType = null)
55 if ($objectType === null) {
56 foreach ($this->objectTypes
as $objectType) {
57 $categories[$objectType->objectType
] = $this->getCategories($objectType->objectType
);
59 } elseif (isset($this->objectTypeCategoryIDs
[$objectType])) {
60 foreach ($this->objectTypeCategoryIDs
[$objectType] as $categoryID) {
61 $categories[$categoryID] = $this->getCategory($categoryID);
69 * Returns the category with the given id or `null` if no such category exists.
71 * @param int $categoryID
72 * @return Category|null
74 public function getCategory($categoryID)
76 return $this->categories
[$categoryID] ??
null;
80 * Returns the child categories of the category with the given id.
82 * The second parameter is only needed if $categoryID is 0.
84 * @param int $categoryID
85 * @param int $objectTypeID
87 * @throws SystemException
89 public function getChildCategories($categoryID, $objectTypeID = null)
91 if (!$categoryID && $objectTypeID === null) {
92 throw new SystemException("Missing object type id");
96 foreach ($this->categories
as $category) {
98 $category->parentCategoryID
== $categoryID
99 && ($categoryID ||
$category->objectTypeID
== $objectTypeID)
101 $categories[$category->categoryID
] = $category;
109 * Returns the category object type with the given id or `null` if no such object type exists.
111 * @param int $objectTypeID
112 * @return ObjectType|null
114 public function getObjectType($objectTypeID)
116 if (isset($this->objectTypeIDs
[$objectTypeID])) {
117 return $this->getObjectTypeByName($this->objectTypeIDs
[$objectTypeID]);
124 * Returns the category object type with the given name or `null` if no such object type exists.
126 * @param string $objectType
127 * @return ObjectType|null
129 public function getObjectTypeByName($objectType)
131 return $this->objectTypes
[$objectType] ??
null;
135 * Returns all category object types.
137 * @return ObjectType[]
139 public function getObjectTypes()
141 return $this->objectTypes
;
147 protected function init()
149 $this->objectTypes
= ObjectTypeCache
::getInstance()->getObjectTypes('com.woltlab.wcf.category');
150 foreach ($this->objectTypes
as $objectType) {
151 $this->objectTypeIDs
[$objectType->objectTypeID
] = $objectType->objectType
;
154 $this->categories
= CategoryCacheBuilder
::getInstance()->getData([], 'categories');
155 $this->objectTypeCategoryIDs
= CategoryCacheBuilder
::getInstance()->getData([], 'objectTypeCategoryIDs');
159 * Reloads the category cache.
161 public function reloadCache()
163 CategoryCacheBuilder
::getInstance()->reset();