2 namespace wcf\system\category
;
3 use wcf\data\category\Category
;
4 use wcf\data\
object\type\ObjectType
;
5 use wcf\data\
object\type\ObjectTypeCache
;
6 use wcf\system\cache\builder\CategoryCacheBuilder
;
7 use wcf\system\exception\SystemException
;
8 use wcf\system\SingletonFactory
;
11 * Handles the categories.
13 * @author Matthias Schmidt
14 * @copyright 2001-2018 WoltLab GmbH
15 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
16 * @package WoltLabSuite\Core\System\Category
18 class CategoryHandler
extends SingletonFactory
{
23 protected $categories = [];
26 * category ids grouped by the object type they belong to
29 protected $objectTypeCategoryIDs = [];
32 * maps the names of the category object types to the object type ids
35 protected $objectTypeIDs = [];
38 * list of category object types
41 protected $objectTypes = [];
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.
47 * @param string $objectType
48 * @return Category[]|Category[][]
50 public function getCategories($objectType = null) {
52 if ($objectType === null) {
53 foreach ($this->objectTypes
as $objectType) {
54 $categories[$objectType->objectType
] = $this->getCategories($objectType->objectType
);
57 else if (isset($this->objectTypeCategoryIDs
[$objectType])) {
58 foreach ($this->objectTypeCategoryIDs
[$objectType] as $categoryID) {
59 $categories[$categoryID] = $this->getCategory($categoryID);
67 * Returns the category with the given id or `null` if no such category exists.
69 * @param integer $categoryID
70 * @return Category|null
72 public function getCategory($categoryID) {
73 if (isset($this->categories
[$categoryID])) {
74 return $this->categories
[$categoryID];
81 * Returns the child categories of the category with the given id.
83 * The second parameter is only needed if $categoryID is 0.
85 * @param integer $categoryID
86 * @param integer $objectTypeID
88 * @throws SystemException
90 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) {
97 if ($category->parentCategoryID
== $categoryID && ($categoryID ||
$category->objectTypeID
== $objectTypeID)) {
98 $categories[$category->categoryID
] = $category;
106 * Returns the category object type with the given id or `null` if no such object type exists.
108 * @param integer $objectTypeID
109 * @return ObjectType|null
111 public function getObjectType($objectTypeID) {
112 if (isset($this->objectTypeIDs
[$objectTypeID])) {
113 return $this->getObjectTypeByName($this->objectTypeIDs
[$objectTypeID]);
120 * Returns the category object type with the given name or `null` if no such object type exists.
122 * @param string $objectType
123 * @return ObjectType|null
125 public function getObjectTypeByName($objectType) {
126 if (isset($this->objectTypes
[$objectType])) {
127 return $this->objectTypes
[$objectType];
134 * Returns all category object types.
136 * @return ObjectType[]
138 public function getObjectTypes() {
139 return $this->objectTypes
;
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
;
151 $this->categories
= CategoryCacheBuilder
::getInstance()->getData([], 'categories');
152 $this->objectTypeCategoryIDs
= CategoryCacheBuilder
::getInstance()->getData([], 'objectTypeCategoryIDs');
156 * Reloads the category cache.
158 public function reloadCache() {
159 CategoryCacheBuilder
::getInstance()->reset();