Commit | Line | Data |
---|---|---|
3b75466f | 1 | <?php |
a9229942 | 2 | |
3b75466f | 3 | namespace wcf\acp\form; |
a9229942 | 4 | |
3b75466f | 5 | use wcf\data\label\group\LabelGroupAction; |
4cae3515 | 6 | use wcf\data\label\group\LabelGroupEditor; |
3b75466f MW |
7 | use wcf\data\object\type\ObjectTypeCache; |
8 | use wcf\form\AbstractForm; | |
9 | use wcf\system\acl\ACLHandler; | |
10 | use wcf\system\exception\UserInputException; | |
7a23a706 MS |
11 | use wcf\system\label\object\type\ILabelObjectTypeHandler; |
12 | use wcf\system\label\object\type\LabelObjectTypeContainer; | |
e76c454b | 13 | use wcf\system\language\I18nHandler; |
3fb859c9 | 14 | use wcf\system\request\LinkHandler; |
3b75466f | 15 | use wcf\system\WCF; |
f3db386c | 16 | use wcf\util\StringUtil; |
3b75466f MW |
17 | |
18 | /** | |
19 | * Shows the label group add form. | |
a9229942 TD |
20 | * |
21 | * @author Alexander Ebert | |
22 | * @copyright 2001-2019 WoltLab GmbH | |
23 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> | |
24 | * @package WoltLabSuite\Core\Acp\Form | |
3b75466f | 25 | */ |
a9229942 TD |
26 | class LabelGroupAddForm extends AbstractForm |
27 | { | |
28 | /** | |
29 | * @inheritDoc | |
30 | */ | |
31 | public $activeMenuItem = 'wcf.acp.menu.link.label.group.add'; | |
32 | ||
33 | /** | |
34 | * @inheritDoc | |
35 | */ | |
36 | public $neededPermissions = ['admin.content.label.canManageLabel']; | |
37 | ||
38 | /** | |
39 | * force users to select a label | |
40 | * @var bool | |
41 | */ | |
42 | public $forceSelection = false; | |
43 | ||
44 | /** | |
45 | * group name | |
46 | * @var string | |
47 | */ | |
48 | public $groupName = ''; | |
49 | ||
50 | /** | |
51 | * group description | |
52 | * @var string | |
53 | */ | |
54 | public $groupDescription = ''; | |
55 | ||
56 | /** | |
57 | * list of label object type handlers | |
58 | * @var ILabelObjectTypeHandler[] | |
59 | */ | |
60 | public $labelObjectTypes = []; | |
61 | ||
62 | /** | |
63 | * list of label object type containers | |
64 | * @var LabelObjectTypeContainer[] | |
65 | */ | |
66 | public $labelObjectTypeContainers = []; | |
67 | ||
68 | /** | |
69 | * list of label group to object type relations | |
70 | * @var array<array> | |
71 | */ | |
72 | public $objectTypes = []; | |
73 | ||
74 | /** | |
75 | * object type id | |
76 | * @var int | |
77 | */ | |
78 | public $objectTypeID = 0; | |
79 | ||
80 | /** | |
81 | * show order | |
82 | * @var int | |
83 | */ | |
84 | public $showOrder = 0; | |
85 | ||
86 | /** | |
87 | * @inheritDoc | |
88 | */ | |
89 | public function readParameters() | |
90 | { | |
91 | parent::readParameters(); | |
92 | ||
93 | $this->objectTypeID = ACLHandler::getInstance()->getObjectTypeID('com.woltlab.wcf.label'); | |
94 | ||
95 | I18nHandler::getInstance()->register('groupName'); | |
96 | } | |
97 | ||
98 | /** | |
99 | * @inheritDoc | |
100 | */ | |
101 | public function readFormParameters() | |
102 | { | |
103 | parent::readFormParameters(); | |
104 | ||
105 | I18nHandler::getInstance()->readValues(); | |
106 | ||
107 | if (I18nHandler::getInstance()->isPlainValue('groupName')) { | |
108 | $this->groupName = I18nHandler::getInstance()->getValue('groupName'); | |
109 | } | |
110 | ||
111 | if (isset($_POST['groupDescription'])) { | |
112 | $this->groupDescription = StringUtil::trim($_POST['groupDescription']); | |
113 | } | |
114 | if (isset($_POST['forceSelection'])) { | |
115 | $this->forceSelection = true; | |
116 | } | |
117 | if (isset($_POST['objectTypes']) && \is_array($_POST['objectTypes'])) { | |
118 | $this->objectTypes = $_POST['objectTypes']; | |
119 | } | |
120 | if (isset($_POST['showOrder'])) { | |
121 | $this->showOrder = \intval($_POST['showOrder']); | |
122 | } | |
123 | } | |
124 | ||
125 | /** | |
126 | * @inheritDoc | |
127 | */ | |
128 | public function readData() | |
129 | { | |
130 | // get label object type handlers | |
131 | $objectTypes = ObjectTypeCache::getInstance()->getObjectTypes('com.woltlab.wcf.label.objectType'); | |
132 | foreach ($objectTypes as $objectType) { | |
133 | $this->labelObjectTypes[$objectType->objectTypeID] = $objectType->getProcessor(); | |
134 | $this->labelObjectTypes[$objectType->objectTypeID]->setObjectTypeID($objectType->objectTypeID); | |
135 | } | |
136 | ||
137 | foreach ($this->labelObjectTypes as $objectTypeID => $labelObjectType) { | |
138 | $this->labelObjectTypeContainers[$objectTypeID] = $labelObjectType->getContainer(); | |
139 | } | |
140 | ||
141 | parent::readData(); | |
142 | ||
143 | // assign new values for object relations | |
144 | $this->setObjectTypeRelations(); | |
145 | } | |
146 | ||
147 | /** | |
148 | * @inheritDoc | |
149 | */ | |
150 | public function validate() | |
151 | { | |
152 | parent::validate(); | |
153 | ||
154 | // validate group name | |
155 | if (!I18nHandler::getInstance()->validateValue('groupName')) { | |
156 | if (I18nHandler::getInstance()->isPlainValue('groupName')) { | |
157 | throw new UserInputException('groupName'); | |
158 | } else { | |
159 | throw new UserInputException('groupName', 'multilingual'); | |
160 | } | |
161 | } | |
162 | ||
163 | // validate object type relations | |
164 | foreach ($this->objectTypes as $objectTypeID => $data) { | |
165 | if (!isset($this->labelObjectTypes[$objectTypeID])) { | |
166 | unset($this->objectTypes[$objectTypeID]); | |
167 | } | |
168 | } | |
169 | } | |
170 | ||
171 | /** | |
172 | * @inheritDoc | |
173 | */ | |
174 | public function save() | |
175 | { | |
176 | parent::save(); | |
177 | ||
178 | // save label | |
179 | $this->objectAction = new LabelGroupAction([], 'create', [ | |
180 | 'data' => \array_merge($this->additionalFields, [ | |
181 | 'forceSelection' => $this->forceSelection ? 1 : 0, | |
182 | 'groupName' => $this->groupName, | |
183 | 'groupDescription' => $this->groupDescription, | |
184 | 'showOrder' => $this->showOrder, | |
185 | ]), | |
186 | ]); | |
187 | $returnValues = $this->objectAction->executeAction(); | |
188 | ||
189 | if (!I18nHandler::getInstance()->isPlainValue('groupName')) { | |
190 | I18nHandler::getInstance()->save( | |
191 | 'groupName', | |
192 | 'wcf.acp.label.group' . $returnValues['returnValues']->groupID, | |
193 | 'wcf.acp.label', | |
194 | 1 | |
195 | ); | |
196 | ||
197 | // update group name | |
198 | $groupEditor = new LabelGroupEditor($returnValues['returnValues']); | |
199 | $groupEditor->update([ | |
200 | 'groupName' => 'wcf.acp.label.group' . $returnValues['returnValues']->groupID, | |
201 | ]); | |
202 | } | |
203 | ||
204 | // save acl | |
205 | ACLHandler::getInstance()->save($returnValues['returnValues']->groupID, $this->objectTypeID); | |
206 | ACLHandler::getInstance()->disableAssignVariables(); | |
207 | ||
208 | // save object type relations | |
209 | $this->saveObjectTypeRelations($returnValues['returnValues']->groupID); | |
210 | ||
13b11e4c | 211 | foreach ($this->labelObjectTypes as $labelObjectType) { |
a9229942 TD |
212 | $labelObjectType->save(); |
213 | } | |
214 | ||
215 | $this->saved(); | |
216 | ||
217 | // reset values | |
218 | $this->forceSelection = false; | |
219 | $this->groupName = $this->groupDescription = ''; | |
220 | $this->objectTypes = []; | |
221 | $this->showOrder = 0; | |
222 | $this->setObjectTypeRelations(); | |
223 | ||
224 | // show success message | |
225 | WCF::getTPL()->assign([ | |
226 | 'success' => true, | |
227 | 'objectEditLink' => LinkHandler::getInstance()->getControllerLink( | |
228 | LabelGroupEditForm::class, | |
229 | ['id' => $returnValues['returnValues']->groupID] | |
230 | ), | |
231 | ]); | |
232 | ||
233 | I18nHandler::getInstance()->reset(); | |
234 | } | |
235 | ||
236 | /** | |
237 | * @inheritDoc | |
238 | */ | |
239 | public function assignVariables() | |
240 | { | |
241 | parent::assignVariables(); | |
242 | ||
243 | ACLHandler::getInstance()->assignVariables($this->objectTypeID); | |
244 | I18nHandler::getInstance()->assignVariables(); | |
245 | ||
246 | WCF::getTPL()->assign([ | |
247 | 'action' => 'add', | |
248 | 'forceSelection' => $this->forceSelection, | |
249 | 'groupName' => $this->groupName, | |
250 | 'groupDescription' => $this->groupDescription, | |
251 | 'labelObjectTypeContainers' => $this->labelObjectTypeContainers, | |
252 | 'objectTypeID' => $this->objectTypeID, | |
253 | 'showOrder' => $this->showOrder, | |
254 | ]); | |
255 | } | |
256 | ||
257 | /** | |
258 | * Saves label group to object relations. | |
259 | * | |
260 | * @param int $groupID | |
261 | */ | |
262 | protected function saveObjectTypeRelations($groupID) | |
263 | { | |
264 | WCF::getDB()->beginTransaction(); | |
265 | ||
266 | // remove old relations | |
267 | if ($groupID !== null) { | |
268 | $sql = "DELETE FROM wcf" . WCF_N . "_label_group_to_object | |
269 | WHERE groupID = ?"; | |
270 | $statement = WCF::getDB()->prepareStatement($sql); | |
271 | $statement->execute([$groupID]); | |
272 | } | |
273 | ||
274 | // insert new relations | |
275 | if (!empty($this->objectTypes)) { | |
276 | $sql = "INSERT INTO wcf" . WCF_N . "_label_group_to_object | |
277 | (groupID, objectTypeID, objectID) | |
278 | VALUES (?, ?, ?)"; | |
279 | $statement = WCF::getDB()->prepareStatement($sql); | |
280 | ||
281 | foreach ($this->objectTypes as $objectTypeID => $data) { | |
282 | foreach ($data as $objectID) { | |
283 | // use "0" (stored as NULL) for simple true/false states | |
284 | if (!$objectID) { | |
285 | $objectID = null; | |
286 | } | |
287 | ||
288 | $statement->execute([ | |
289 | $groupID, | |
290 | $objectTypeID, | |
291 | $objectID, | |
292 | ]); | |
293 | } | |
294 | } | |
295 | } | |
296 | ||
297 | WCF::getDB()->commitTransaction(); | |
298 | } | |
299 | ||
300 | /** | |
301 | * Sets object type relations. | |
302 | * | |
303 | * @param array|null $data | |
304 | */ | |
305 | protected function setObjectTypeRelations($data = null) | |
306 | { | |
307 | if (!empty($_POST)) { | |
308 | // use POST data | |
309 | $data = &$this->objectTypes; | |
310 | } | |
311 | ||
312 | foreach ($this->labelObjectTypeContainers as $objectTypeID => $container) { | |
313 | $hasData = isset($data[$objectTypeID]); | |
314 | foreach ($container as $object) { | |
315 | if (!$hasData) { | |
316 | $object->setOptionValue(0); | |
317 | } else { | |
318 | $optionValue = \in_array($object->getObjectID(), $data[$objectTypeID]) ? 1 : 0; | |
319 | $object->setOptionValue($optionValue); | |
320 | } | |
321 | } | |
322 | } | |
323 | } | |
3b75466f | 324 | } |