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