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