3 namespace wcf\data\category
;
5 use wcf\data\IPermissionObject
;
6 use wcf\data\
object\type\ObjectType
;
7 use wcf\data\ProcessibleDatabaseObject
;
8 use wcf\data\user\User
;
9 use wcf\system\category\CategoryHandler
;
10 use wcf\system\category\CategoryPermissionHandler
;
11 use wcf\system\category\ICategoryType
;
12 use wcf\system\exception\PermissionDeniedException
;
13 use wcf\system\request\IRouteController
;
17 * Represents a category.
19 * @author Matthias Schmidt
20 * @copyright 2001-2019 WoltLab GmbH
21 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
22 * @package WoltLabSuite\Core\Data\Category
24 * @property-read int $categoryID unique id of the category
25 * @property-read int $objectTypeID id of the `com.woltlab.wcf.category` object type
26 * @property-read int $parentCategoryID id of the category's parent category or `0` if it has no parent category
27 * @property-read string $title title of the category or name of language item which contains the title
28 * @property-read string $description description of the category or name of language item which contains the description
29 * @property-read int $descriptionUseHtml is `1` if html is enabled in the description, otherwise `0`
30 * @property-read int $showOrder position of the category in relation to its siblings
31 * @property-read int $time timestamp at which the comment has been created
32 * @property-read int $isDisabled is `1` if the category is disabled and thus neither accessible nor selectable, otherwise `0`
33 * @property-read array $additionalData array with additional data of the category
35 class Category
extends ProcessibleDatabaseObject
implements IPermissionObject
, IRouteController
38 * list of child categories of this category
41 protected $childCategories;
44 * list of all child categories of this category
47 protected $allChildCategories;
50 * list of all parent category generations of this category
53 protected $parentCategories;
56 * parent category of this category
59 protected $parentCategory;
62 * acl permissions of this category for the active user
66 protected $permissions;
69 * acl permissions of this category grouped by the id of the user they
73 protected $userPermissions = [];
76 * fallback return value used in Category::getPermission()
79 protected $defaultPermission = false;
84 protected static $processorInterface = ICategoryType
::class;
89 public function __get($name)
91 // forward 'className' property requests to object type
92 if ($name == 'className') {
93 return $this->getObjectType()->className
;
96 $value = parent
::__get($name);
98 // check additional data
99 if ($value === null) {
100 if (isset($this->data
['additionalData'][$name])) {
101 $value = $this->data
['additionalData'][$name];
111 public function checkPermissions(array $permissions)
113 foreach ($permissions as $permission) {
114 if (!$this->getPermission($permission)) {
115 throw new PermissionDeniedException();
121 * Returns the category object type of the category.
125 public function getObjectType()
127 return CategoryHandler
::getInstance()->getObjectType($this->objectTypeID
);
131 * Returns the direct child categories of this category.
135 public function getChildCategories()
137 if ($this->childCategories
=== null) {
138 $this->childCategories
= CategoryHandler
::getInstance()->getChildCategories($this->categoryID
);
141 return $this->childCategories
;
145 * Returns the child categories of this category recursively.
149 public function getAllChildCategories()
151 if ($this->allChildCategories
=== null) {
152 $directChildCategories = CategoryHandler
::getInstance()->getChildCategories($this->categoryID
);
153 $childCategories = [];
154 foreach ($directChildCategories as $childCategory) {
155 $childCategories = \array_replace
($childCategories, $childCategory->getAllChildCategories());
158 $this->allChildCategories
= \array_replace
($directChildCategories, $childCategories);
161 return $this->allChildCategories
;
165 * Returns the parent category of the category or `null` if the category has no parent category.
167 * @return Category|null
169 public function getParentCategory()
171 if ($this->parentCategoryID
&& $this->parentCategory
=== null) {
172 $this->parentCategory
= CategoryHandler
::getInstance()->getCategory($this->parentCategoryID
);
175 return $this->parentCategory
;
179 * Returns the parent categories of this category.
183 public function getParentCategories()
185 if ($this->parentCategories
=== null) {
186 $this->parentCategories
= [];
187 $parentCategory = $this;
188 while ($parentCategory = $parentCategory->getParentCategory()) {
189 $this->parentCategories
[] = $parentCategory;
192 $this->parentCategories
= \array_reverse
($this->parentCategories
);
195 return $this->parentCategories
;
199 * Returns true if given category is a parent category of this category.
201 * @param Category $category
204 public function isParentCategory(self
$category)
206 return \
in_array($category, $this->getParentCategories());
212 public function getPermission($permission, ?User
$user = null)
214 if ($user === null) {
215 $user = WCF
::getUser();
218 if (!isset($this->userPermissions
[$user->userID
])) {
219 $this->userPermissions
[$user->userID
] = CategoryPermissionHandler
::getInstance()
220 ->getPermissions($this, $user);
222 if ($user->userID
== WCF
::getUser()->userID
) {
223 $this->permissions
= $this->userPermissions
[$user->userID
];
227 if (isset($this->userPermissions
[$user->userID
][$permission])) {
228 return $this->userPermissions
[$user->userID
][$permission];
231 if ($this->getParentCategory()) {
232 return $this->getParentCategory()->getPermission($permission, $user);
235 if ($this->getObjectType()->defaultpermission
!== null) {
236 return $this->getObjectType()->defaultpermission ?
true : false;
239 return $this->defaultPermission
;
245 public function getTitle()
247 return WCF
::getLanguage()->get($this->title
);
251 * Returns the description of this category.
255 public function getDescription()
257 if ($this->description
) {
258 return WCF
::getLanguage()->get($this->description
);
267 protected function handleData($data)
269 // handle additional data
270 if (isset($data['additionalData'])) {
271 $data['additionalData'] = @\
unserialize($data['additionalData']);
272 if (!\
is_array($data['additionalData'])) {
273 $data['additionalData'] = [];
276 $data['additionalData'] = [];
279 parent
::handleData($data);
285 public function __toString()
287 return $this->getTitle();