Fix code formatting
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / data / category / Category.class.php
CommitLineData
13d8b49b
MS
1<?php
2namespace wcf\data\category;
2b59736b 3use wcf\data\user\User;
796fdcf2 4use wcf\data\IPermissionObject;
39d99924 5use wcf\data\ProcessibleDatabaseObject;
13d8b49b 6use wcf\system\category\CategoryHandler;
796fdcf2
MS
7use wcf\system\category\CategoryPermissionHandler;
8use wcf\system\exception\PermissionDeniedException;
13d8b49b
MS
9use wcf\system\request\IRouteController;
10use wcf\system\WCF;
11
12/**
13 * Represents a category.
14 *
15 * @author Matthias Schmidt
ca4ba303 16 * @copyright 2001-2014 WoltLab GmbH
13d8b49b
MS
17 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
18 * @package com.woltlab.wcf
19 * @subpackage data.category
9f959ced 20 * @category Community Framework
13d8b49b 21 */
796fdcf2
MS
22class Category extends ProcessibleDatabaseObject implements IPermissionObject, IRouteController {
23 /**
24 * list of all child categories of this category
0ad90fc3 25 * @var array<\wcf\data\category\Category>
796fdcf2
MS
26 */
27 protected $childCategories = null;
28
13d8b49b
MS
29 /**
30 * list of all parent category generations of this category
0ad90fc3 31 * @var array<\wcf\data\category\Category>
13d8b49b
MS
32 */
33 protected $parentCategories = null;
34
35 /**
36 * parent category of this category
0ad90fc3 37 * @var \wcf\data\category\Category
13d8b49b
MS
38 */
39 protected $parentCategory = null;
40
796fdcf2 41 /**
2b59736b
MS
42 * acl permissions of this category for the active user
43 * @deprecated
796fdcf2
MS
44 * @var array<boolean>
45 */
46 protected $permissions = null;
47
2b59736b
MS
48 /**
49 * acl permissions of this category grouped by the id of the user they
50 * belong to
51 * @var array
52 */
53 protected $userPermissions = array();
54
99c00b13
MW
55 /**
56 * fallback return value used in Category::getPermission()
1a6e8c52 57 * @var boolean
99c00b13
MW
58 */
59 protected $defaultPermission = false;
60
13d8b49b 61 /**
0ad90fc3 62 * @see \wcf\data\DatabaseObject::$databaseTableIndexName
13d8b49b
MS
63 */
64 protected static $databaseTableIndexName = 'categoryID';
65
66 /**
0ad90fc3 67 * @see \wcf\data\DatabaseObject::$databaseTableName
13d8b49b
MS
68 */
69 protected static $databaseTableName = 'category';
70
39d99924 71 /**
0ad90fc3 72 * @see \wcf\data\ProcessibleDatabaseObject::$processorInterface
39d99924
MS
73 */
74 protected static $processorInterface = 'wcf\system\category\ICategoryType';
75
13d8b49b 76 /**
0ad90fc3 77 * @see \wcf\data\IStorableObject::__get()
13d8b49b
MS
78 */
79 public function __get($name) {
39d99924
MS
80 // forward 'className' property requests to object type
81 if ($name == 'className') {
82 return $this->getObjectType()->className;
83 }
84
13d8b49b
MS
85 $value = parent::__get($name);
86
87 // check additional data
88 if ($value === null) {
89 if (isset($this->data['additionalData'][$name])) {
90 $value = $this->data['additionalData'][$name];
91 }
92 }
93
94 return $value;
95 }
96
796fdcf2 97 /**
0ad90fc3 98 * @see \wcf\data\IPermissionObject::checkPermissions()
796fdcf2
MS
99 */
100 public function checkPermissions(array $permissions) {
101 foreach ($permissions as $permission) {
102 if (!$this->getPermission($permission)) {
103 throw new PermissionDeniedException();
104 }
105 }
106 }
107
13d8b49b 108 /**
39d99924
MS
109 * Returns the category object type of the category.
110 *
0ad90fc3 111 * @return \wcf\data\category\Category
13d8b49b 112 */
39d99924
MS
113 public function getObjectType() {
114 return CategoryHandler::getInstance()->getObjectType($this->objectTypeID);
13d8b49b
MS
115 }
116
796fdcf2
MS
117 /**
118 * Returns the child categories of this category.
119 *
0ad90fc3 120 * @return array<\wcf\data\category\Category>
796fdcf2
MS
121 */
122 public function getChildCategories() {
123 if ($this->childCategories === null) {
124 $this->childCategories = CategoryHandler::getInstance()->getChildCategories($this->categoryID);
125 }
126
127 return $this->childCategories;
128 }
129
13d8b49b
MS
130 /**
131 * Returns the parent category of this category.
132 *
0ad90fc3 133 * @return \wcf\data\category\Category
13d8b49b
MS
134 */
135 public function getParentCategory() {
136 if ($this->parentCategoryID && $this->parentCategory === null) {
045f075b 137 $this->parentCategory = CategoryHandler::getInstance()->getCategory($this->parentCategoryID);
13d8b49b
MS
138 }
139
140 return $this->parentCategory;
141 }
142
143 /**
144 * Returns the parent categories of this category.
145 *
0ad90fc3 146 * @return array<\wcf\data\category\Category>
13d8b49b
MS
147 */
148 public function getParentCategories() {
149 if ($this->parentCategories === null) {
150 $this->parentCategories = array();
151 $parentCaregory = $this;
152 while ($parentCaregory = $parentCaregory->getParentCategory()) {
153 $this->parentCategories[] = $parentCaregory;
154 }
9f959ced 155
13d8b49b
MS
156 $this->parentCategories = array_reverse($this->parentCategories);
157 }
158
159 return $this->parentCategories;
160 }
161
796fdcf2 162 /**
0ad90fc3 163 * @see \wcf\data\IPermissionObject::getPermission()
796fdcf2 164 */
2b59736b
MS
165 public function getPermission($permission, User $user = null) {
166 if ($user === null) {
167 $user = WCF::getUser();
168 }
169
170 if (!isset($this->userPermissions[$user->userID])) {
171 $this->userPermissions[$user->userID] = CategoryPermissionHandler::getInstance()->getPermissions($this, $user);
172
173 if ($user->userID == WCF::getUser()->userID) {
174 $this->permissions = $this->userPermissions[$user->userID];
175 }
796fdcf2
MS
176 }
177
2b59736b
MS
178 if (isset($this->userPermissions[$user->userID][$permission])) {
179 return $this->userPermissions[$user->userID][$permission];
796fdcf2
MS
180 }
181
df387094 182 if ($this->getParentCategory()) {
2b59736b 183 return $this->getParentCategory()->getPermission($permission, $user);
df387094
MS
184 }
185
4c6a3ef4
MS
186 if ($this->getObjectType()->defaultpermission !== null) {
187 return $this->getObjectType()->defaultpermission ? true : false;
188 }
189
99c00b13 190 return $this->defaultPermission;
796fdcf2
MS
191 }
192
13d8b49b 193 /**
0ad90fc3 194 * @see \wcf\data\ITitledObject::getTitle()
13d8b49b
MS
195 */
196 public function getTitle() {
197 return WCF::getLanguage()->get($this->title);
198 }
199
04db9363
MW
200 /**
201 * Returns the description of this category.
202 *
203 * @return string
204 */
205 public function getDescription() {
206 if ($this->description) return WCF::getLanguage()->get($this->description);
207 return '';
208 }
209
13d8b49b 210 /**
0ad90fc3 211 * @see \wcf\data\DatabaseObject::handleData()
13d8b49b
MS
212 */
213 protected function handleData($data) {
214 // handle additional data
215 $data['additionalData'] = @unserialize($data['additionalData']);
216 if (!is_array($data['additionalData'])) {
217 $data['additionalData'] = array();
218 }
219
220 parent::handleData($data);
221 }
22fe8f7a
MW
222
223 /**
224 * @see \wcf\data\ITitledObject::getTitle()
225 */
226 public function __toString() {
227 return $this->getTitle();
228 }
13d8b49b 229}