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