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