Merge branch '3.0'
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / system / category / CategoryHandler.class.php
1 <?php
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;
9
10 /**
11 * Handles the categories.
12 *
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
17 */
18 class CategoryHandler extends SingletonFactory {
19 /**
20 * cached categories
21 * @var Category[]
22 */
23 protected $categories = [];
24
25 /**
26 * category ids grouped by the object type they belong to
27 * @var integer[][]
28 */
29 protected $objectTypeCategoryIDs = [];
30
31 /**
32 * maps the names of the category object types to the object type ids
33 * @var integer[]
34 */
35 protected $objectTypeIDs = [];
36
37 /**
38 * list of category object types
39 * @var ObjectType[]
40 */
41 protected $objectTypes = [];
42
43 /**
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.
46 *
47 * @param string $objectType
48 * @return Category[]|Category[][]
49 */
50 public function getCategories($objectType = null) {
51 $categories = [];
52 if ($objectType === null) {
53 foreach ($this->objectTypes as $objectType) {
54 $categories[$objectType->objectType] = $this->getCategories($objectType->objectType);
55 }
56 }
57 else if (isset($this->objectTypeCategoryIDs[$objectType])) {
58 foreach ($this->objectTypeCategoryIDs[$objectType] as $categoryID) {
59 $categories[$categoryID] = $this->getCategory($categoryID);
60 }
61 }
62
63 return $categories;
64 }
65
66 /**
67 * Returns the category with the given id or `null` if no such category exists.
68 *
69 * @param integer $categoryID
70 * @return Category|null
71 */
72 public function getCategory($categoryID) {
73 if (isset($this->categories[$categoryID])) {
74 return $this->categories[$categoryID];
75 }
76
77 return null;
78 }
79
80 /**
81 * Returns the child categories of the category with the given id.
82 *
83 * The second parameter is only needed if $categoryID is 0.
84 *
85 * @param integer $categoryID
86 * @param integer $objectTypeID
87 * @return Category[]
88 * @throws SystemException
89 */
90 public function getChildCategories($categoryID, $objectTypeID = null) {
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 ($category->parentCategoryID == $categoryID && ($categoryID || $category->objectTypeID == $objectTypeID)) {
98 $categories[$category->categoryID] = $category;
99 }
100 }
101
102 return $categories;
103 }
104
105 /**
106 * Returns the category object type with the given id or `null` if no such object type exists.
107 *
108 * @param integer $objectTypeID
109 * @return ObjectType|null
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 /**
120 * Returns the category object type with the given name or `null` if no such object type exists.
121 *
122 * @param string $objectType
123 * @return ObjectType|null
124 */
125 public function getObjectTypeByName($objectType) {
126 if (isset($this->objectTypes[$objectType])) {
127 return $this->objectTypes[$objectType];
128 }
129
130 return null;
131 }
132
133 /**
134 * Returns all category object types.
135 *
136 * @return ObjectType[]
137 */
138 public function getObjectTypes() {
139 return $this->objectTypes;
140 }
141
142 /**
143 * @inheritDoc
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
151 $this->categories = CategoryCacheBuilder::getInstance()->getData([], 'categories');
152 $this->objectTypeCategoryIDs = CategoryCacheBuilder::getInstance()->getData([], 'objectTypeCategoryIDs');
153 }
154
155 /**
156 * Reloads the category cache.
157 */
158 public function reloadCache() {
159 CategoryCacheBuilder::getInstance()->reset();
160
161 $this->init();
162 }
163 }