Merge pull request #5989 from WoltLab/wsc-rpc-api-const
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / system / category / AbstractCategoryType.class.php
CommitLineData
13d8b49b 1<?php
a9229942 2
13d8b49b 3namespace wcf\system\category;
a9229942 4
13d8b49b 5use wcf\data\category\CategoryEditor;
1a5d2dcf 6use wcf\system\database\util\PreparedStatementConditionBuilder;
13d8b49b 7use wcf\system\SingletonFactory;
a7272346 8use wcf\system\user\object\watch\UserObjectWatchHandler;
13d8b49b
MS
9use wcf\system\WCF;
10
11/**
12 * Abstract implementation of a category type.
a9229942
TD
13 *
14 * @author Matthias Schmidt
15 * @copyright 2001-2019 WoltLab GmbH
16 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
13d8b49b 17 */
a9229942
TD
18abstract class AbstractCategoryType extends SingletonFactory implements ICategoryType
19{
20 /**
21 * indicates if categories of this type may have no empty description
22 * @var bool
23 */
24 protected $forceDescription = true;
25
26 /**
27 * indicates if categories of this type have descriptions
28 * @var bool
29 */
30 protected $hasDescription = true;
31
32 /**
33 * language category which contains the language variables of i18n values
34 * @var string
35 */
36 protected $i18nLangVarCategory = 'wcf.category';
37
38 /**
39 * prefix used for language variables in templates
40 * @var string
41 */
42 protected $langVarPrefix = '';
43
44 /**
45 * permission prefix for the add/delete/edit permissions
46 * @var string
47 */
48 protected $permissionPrefix = '';
49
50 /**
51 * maximum category nesting label
52 * @var int
53 */
54 protected $maximumNestingLevel = -1;
55
56 /**
57 * name of the object types associated with categories of this type (the
58 * key is the definition name and value the object type name)
59 * @var string[]
60 */
61 protected $objectTypes = [];
62
63 /**
64 * @inheritDoc
65 */
66 public function afterDeletion(CategoryEditor $categoryEditor)
67 {
68 $categoryIDs = \array_keys(CategoryHandler::getInstance()->getChildCategories($categoryEditor->categoryID));
69
70 if (!empty($categoryIDs)) {
71 // move child categories to parent category
72 $conditionBuilder = new PreparedStatementConditionBuilder();
73 $conditionBuilder->add("categoryID IN (?)", [$categoryIDs]);
74 $sql = "UPDATE wcf" . WCF_N . "_category
75 SET parentCategoryID = ?
76 " . $conditionBuilder;
77 $statement = WCF::getDB()->prepareStatement($sql);
78 $statement->execute(\array_merge([$categoryEditor->parentCategoryID], $conditionBuilder->getParameters()));
79 }
a7272346
MW
80
81 if ($this->getObjectTypeName('com.woltlab.wcf.user.objectWatch')) {
82 UserObjectWatchHandler::getInstance()
83 ->deleteObjects($this->getObjectTypeName('com.woltlab.wcf.user.objectWatch'), [$categoryEditor->categoryID]);
84 }
a9229942
TD
85 }
86
87 /**
88 * @inheritDoc
89 */
90 public function beforeDeletion(CategoryEditor $categoryEditor)
91 {
92 // does nothing
93 }
94
95 /**
96 * @inheritDoc
97 */
98 public function canAddCategory()
99 {
100 return WCF::getSession()->getPermission($this->permissionPrefix . '.canAddCategory');
101 }
102
103 /**
104 * @inheritDoc
105 */
106 public function canDeleteCategory()
107 {
108 return WCF::getSession()->getPermission($this->permissionPrefix . '.canDeleteCategory');
109 }
110
111 /**
112 * @inheritDoc
113 */
114 public function canEditCategory()
115 {
116 return WCF::getSession()->getPermission($this->permissionPrefix . '.canEditCategory');
117 }
118
119 /**
120 * @inheritDoc
121 */
122 public function changedParentCategories(array $categoryData)
123 {
124 // does nothing
125 }
126
127 /**
128 * @inheritDoc
129 */
130 public function forceDescription()
131 {
132 return $this->hasDescription() && $this->forceDescription;
133 }
134
135 /**
136 * @inheritDoc
137 */
138 public function getApplication()
139 {
0befcb4a 140 $classParts = \explode('\\', static::class);
a9229942
TD
141
142 return $classParts[0];
143 }
144
145 /**
146 * @inheritDoc
147 */
148 public function getObjectTypeName($definitionName)
149 {
813c41ce 150 return $this->objectTypes[$definitionName] ?? null;
a9229942
TD
151 }
152
153 /**
154 * @inheritDoc
155 */
156 public function getDescriptionLangVarCategory()
157 {
158 return $this->i18nLangVarCategory;
159 }
160
161 /**
162 * @inheritDoc
163 */
164 public function getI18nLangVarPrefix()
165 {
166 return $this->i18nLangVarCategory . '.category';
167 }
168
169 /**
170 * @inheritDoc
171 */
172 public function getLanguageVariable($name, $optional = false)
173 {
174 if ($this->langVarPrefix) {
175 $value = WCF::getLanguage()->getDynamicVariable($this->langVarPrefix . '.' . $name, [], true);
176 if ($value) {
177 return $value;
178 }
179 }
180
181 return WCF::getLanguage()->getDynamicVariable('wcf.category.' . $name, [], $optional);
182 }
183
184 /**
185 * @inheritDoc
186 */
187 public function getMaximumNestingLevel()
188 {
189 return $this->maximumNestingLevel;
190 }
191
192 /**
193 * @inheritDoc
194 */
195 public function getTitleLangVarCategory()
196 {
197 return $this->i18nLangVarCategory;
198 }
199
200 /**
201 * @inheritDoc
202 */
203 public function hasDescription()
204 {
205 return $this->hasDescription;
206 }
207
208 /**
209 * @inheritDoc
210 * @since 5.2
211 */
212 public function supportsHtmlDescription()
213 {
214 return false;
215 }
13d8b49b 216}