Merge branch '2.1' into 3.0
[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-2017 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 lebel
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 canAddCategory() {
82 return WCF::getSession()->getPermission($this->permissionPrefix.'.canAddCategory');
83 }
84
85 /**
86 * @inheritDoc
87 */
88 public function canDeleteCategory() {
89 return WCF::getSession()->getPermission($this->permissionPrefix.'.canDeleteCategory');
90 }
91
92 /**
93 * @inheritDoc
94 */
95 public function canEditCategory() {
96 return WCF::getSession()->getPermission($this->permissionPrefix.'.canEditCategory');
97 }
98
99 /**
100 * @inheritDoc
101 */
102 public function changedParentCategories(array $categoryData) {
103 // does nothing
104 }
105
106 /**
107 * @inheritDoc
108 */
109 public function forceDescription() {
110 return $this->hasDescription() && $this->forceDescription;
111 }
112
113 /**
114 * @inheritDoc
115 */
116 public function getApplication() {
117 $classParts = explode('\\', get_called_class());
118 return $classParts[0];
119 }
120
121 /**
122 * @inheritDoc
123 */
124 public function getObjectTypeName($definitionName) {
125 if (isset($this->objectTypes[$definitionName])) {
126 return $this->objectTypes[$definitionName];
127 }
128
129 return null;
130 }
131
132 /**
133 * @inheritDoc
134 */
135 public function getDescriptionLangVarCategory() {
136 return $this->i18nLangVarCategory;
137 }
138
139 /**
140 * @inheritDoc
141 */
142 public function getI18nLangVarPrefix() {
143 return $this->i18nLangVarCategory.'.category';
144 }
145
146 /**
147 * @inheritDoc
148 */
149 public function getLanguageVariable($name, $optional = false) {
150 if ($this->langVarPrefix) {
151 $value = WCF::getLanguage()->getDynamicVariable($this->langVarPrefix.'.'.$name, [], true);
152 if ($value) {
153 return $value;
154 }
155 }
156
157 return WCF::getLanguage()->getDynamicVariable('wcf.category.'.$name, [], $optional);
158 }
159
160 /**
161 * @inheritDoc
162 */
163 public function getMaximumNestingLevel() {
164 return $this->maximumNestingLevel;
165 }
166
167 /**
168 * @inheritDoc
169 */
170 public function getTitleLangVarCategory() {
171 return $this->i18nLangVarCategory;
172 }
173
174 /**
175 * @inheritDoc
176 */
177 public function hasDescription() {
178 return $this->hasDescription;
179 }
180 }