Commit | Line | Data |
---|---|---|
60239287 | 1 | <?php |
a9229942 | 2 | |
60239287 | 3 | namespace wcf\acp\form; |
a9229942 | 4 | |
60239287 JR |
5 | use wcf\data\category\Category; |
6 | use wcf\data\object\type\ObjectType; | |
7 | use wcf\data\trophy\category\TrophyCategoryCache; | |
8 | use wcf\data\trophy\Trophy; | |
9 | use wcf\data\trophy\TrophyAction; | |
15062be7 | 10 | use wcf\data\trophy\TrophyEditor; |
60239287 JR |
11 | use wcf\system\condition\ConditionHandler; |
12 | use wcf\system\exception\UserInputException; | |
13 | use wcf\system\language\I18nValue; | |
3fb859c9 | 14 | use wcf\system\request\LinkHandler; |
afd474ba | 15 | use wcf\system\style\FontAwesomeIcon; |
60239287 JR |
16 | use wcf\system\trophy\condition\TrophyConditionHandler; |
17 | use wcf\system\WCF; | |
18 | use wcf\util\StringUtil; | |
19 | ||
20 | /** | |
a9229942 | 21 | * Represents the trophy add form. |
60239287 | 22 | * |
a9229942 TD |
23 | * @author Joshua Ruesweg |
24 | * @copyright 2001-2019 WoltLab GmbH | |
25 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> | |
a9229942 | 26 | * @since 3.1 |
60239287 | 27 | */ |
a9229942 TD |
28 | class TrophyAddForm extends AbstractAcpForm |
29 | { | |
30 | /** | |
31 | * @inheritDoc | |
32 | */ | |
33 | public $activeMenuItem = 'wcf.acp.menu.link.trophy.add'; | |
34 | ||
35 | /** | |
36 | * @inheritDoc | |
37 | */ | |
38 | public $neededPermissions = ['admin.trophy.canManageTrophy']; | |
39 | ||
40 | /** | |
41 | * @inheritDoc | |
42 | */ | |
43 | public $neededModules = ['MODULE_TROPHY']; | |
44 | ||
45 | /** | |
46 | * category id for the trophy. | |
47 | * @var int | |
48 | */ | |
49 | public $categoryID = 0; | |
50 | ||
51 | /** | |
52 | * Category object. | |
53 | * @var Category | |
54 | */ | |
55 | public $category; | |
56 | ||
57 | /** | |
58 | * Trophy description. | |
59 | * @var string | |
60 | */ | |
61 | public $description = ''; | |
62 | ||
63 | /** | |
64 | * Trophy title. | |
65 | * @var string | |
66 | */ | |
67 | public $title = ''; | |
68 | ||
69 | /** | |
70 | * All available trophy types. | |
71 | * @var [] | |
72 | */ | |
73 | public $availableTypes = [ | |
74 | Trophy::TYPE_IMAGE => 'imageUpload', | |
75 | Trophy::TYPE_BADGE => 'badge', | |
76 | ]; | |
77 | ||
78 | /** | |
79 | * Type of the trophy (whether this is an image or not) | |
80 | * @var int | |
81 | */ | |
82 | public $type = Trophy::TYPE_BADGE; | |
83 | ||
84 | /** | |
85 | * temporary hash for image icon | |
86 | * @var string | |
87 | */ | |
88 | public $tmpHash = ''; | |
89 | ||
90 | /** | |
91 | * the url for the uploaded image | |
92 | * @var string | |
93 | */ | |
94 | public $uploadedImageURL = ''; | |
95 | ||
96 | /** | |
97 | * the icon name for CSS icons (FA-Icon) | |
98 | * @var string | |
99 | */ | |
afd474ba | 100 | public $iconName = "trophy;false"; |
a9229942 TD |
101 | |
102 | /** | |
103 | * The icon color (rgba format with rgba prefix) | |
104 | * @var string | |
105 | */ | |
106 | public $iconColor = 'rgba(255, 255, 255, 1)'; | |
107 | ||
108 | /** | |
109 | * The badge color (rgba format with rgba prefix) | |
110 | * @var string | |
111 | */ | |
112 | public $badgeColor = 'rgba(50, 92, 132, 1)'; | |
113 | ||
114 | /** | |
115 | * `1` if the trophy is disabled. | |
116 | * @var int | |
117 | */ | |
118 | public $isDisabled = 0; | |
119 | ||
120 | /** | |
121 | * `1` if the trophy has conditions to reward automatically trophies. | |
122 | * @var int | |
123 | */ | |
124 | public $awardAutomatically = 0; | |
125 | ||
126 | /** | |
127 | * `1` if the trophy should be automatically revoked once the conditions are no longer met. | |
128 | * @var int | |
129 | */ | |
130 | public $revokeAutomatically = 0; | |
131 | ||
132 | /** | |
133 | * `1` if the trophy contains html in the description | |
134 | * @var int | |
135 | */ | |
136 | public $trophyUseHtml = 0; | |
137 | ||
138 | /** | |
139 | * list of grouped user group assignment condition object types | |
140 | * @var ObjectType[][] | |
141 | */ | |
142 | public $conditions = []; | |
143 | ||
144 | /** | |
145 | * the showOrder value of the trophy | |
146 | * @var int | |
147 | */ | |
148 | public $showOrder = 0; | |
149 | ||
150 | /** | |
151 | * @inheritDoc | |
152 | */ | |
153 | public function readData() | |
154 | { | |
155 | $this->conditions = TrophyConditionHandler::getInstance()->getGroupedObjectTypes(); | |
156 | ||
157 | parent::readData(); | |
158 | } | |
159 | ||
160 | /** | |
161 | * @inheritDoc | |
162 | */ | |
163 | public function readParameters() | |
164 | { | |
165 | parent::readParameters(); | |
166 | ||
167 | $titleI18n = new I18nValue('title'); | |
168 | $titleI18n->setLanguageItem('wcf.user.trophy.title', 'wcf.user.trophy', 'com.woltlab.wcf'); | |
169 | $this->registerI18nValue($titleI18n); | |
170 | ||
171 | $descriptionI18n = new I18nValue('description'); | |
172 | $descriptionI18n->setLanguageItem('wcf.user.trophy.description', 'wcf.user.trophy', 'com.woltlab.wcf'); | |
173 | $descriptionI18n->setFlags(I18nValue::ALLOW_EMPTY); | |
174 | $this->registerI18nValue($descriptionI18n); | |
175 | ||
176 | if (isset($_POST['tmpHash'])) { | |
177 | $this->tmpHash = StringUtil::trim($_POST['tmpHash']); | |
178 | } | |
179 | ||
180 | if (empty($this->tmpHash)) { | |
181 | $this->tmpHash = StringUtil::getRandomID(); | |
182 | } | |
183 | } | |
184 | ||
185 | /** | |
186 | * @inheritDoc | |
187 | */ | |
188 | public function readFormParameters() | |
189 | { | |
190 | parent::readFormParameters(); | |
191 | ||
192 | if (isset($_POST['categoryID'])) { | |
193 | $this->categoryID = \intval($_POST['categoryID']); | |
194 | } | |
195 | if (isset($_POST['type'])) { | |
196 | $this->type = \intval($_POST['type']); | |
197 | } | |
198 | if (isset($_POST['isDisabled'])) { | |
199 | $this->isDisabled = \intval($_POST['isDisabled']); | |
200 | } | |
201 | if (isset($_POST['iconName'])) { | |
202 | $this->iconName = StringUtil::trim($_POST['iconName']); | |
203 | } | |
204 | if (isset($_POST['iconColor'])) { | |
205 | $this->iconColor = $_POST['iconColor']; | |
206 | } | |
207 | if (isset($_POST['badgeColor'])) { | |
208 | $this->badgeColor = $_POST['badgeColor']; | |
209 | } | |
210 | if (isset($_POST['awardAutomatically'])) { | |
211 | $this->awardAutomatically = 1; | |
212 | } | |
213 | if (isset($_POST['revokeAutomatically']) && $this->awardAutomatically) { | |
214 | $this->revokeAutomatically = 1; | |
215 | } | |
216 | if (isset($_POST['trophyUseHtml'])) { | |
217 | $this->trophyUseHtml = 1; | |
218 | } | |
219 | if (isset($_POST['showOrder'])) { | |
220 | $this->showOrder = \intval($_POST['showOrder']); | |
221 | } | |
222 | ||
223 | // read file upload | |
224 | $fileExtension = WCF::getSession()->getVar('trophyImage-' . $this->tmpHash); | |
225 | ||
226 | if ($fileExtension !== null && \file_exists(WCF_DIR . 'images/trophy/tmp_' . $this->tmpHash . '.' . $fileExtension)) { | |
227 | $this->uploadedImageURL = WCF::getPath() . 'images/trophy/tmp_' . $this->tmpHash . '.' . $fileExtension; | |
228 | } | |
229 | ||
230 | $this->category = TrophyCategoryCache::getInstance()->getCategoryByID($this->categoryID); | |
231 | ||
232 | foreach ($this->conditions as $conditions) { | |
233 | /** @var ObjectType $condition */ | |
234 | foreach ($conditions as $condition) { | |
235 | $condition->getProcessor()->readFormParameters(); | |
236 | } | |
237 | } | |
238 | } | |
239 | ||
240 | /** | |
241 | * @inheritDoc | |
242 | */ | |
243 | public function validate() | |
244 | { | |
245 | parent::validate(); | |
246 | ||
247 | if (!\in_array($this->type, \array_keys($this->availableTypes))) { | |
248 | throw new UserInputException('type'); | |
249 | } | |
250 | ||
251 | if (!$this->categoryID) { | |
252 | throw new UserInputException('categoryID'); | |
253 | } | |
254 | ||
255 | if (!$this->category->getObjectID()) { | |
256 | throw new UserInputException('categoryID'); | |
257 | } | |
258 | ||
259 | $this->validateType(); | |
260 | ||
261 | if ($this->awardAutomatically) { | |
262 | $hasData = false; | |
263 | foreach ($this->conditions as $conditions) { | |
264 | foreach ($conditions as $condition) { | |
265 | $condition->getProcessor()->validate(); | |
266 | ||
267 | if (!$hasData && $condition->getProcessor()->getData() !== null) { | |
268 | $hasData = true; | |
269 | } | |
270 | } | |
271 | } | |
272 | ||
273 | if (!$hasData) { | |
274 | throw new UserInputException('conditions'); | |
275 | } | |
276 | } | |
277 | } | |
278 | ||
279 | /** | |
280 | * Validates the trophy type. | |
281 | */ | |
282 | protected function validateType() | |
283 | { | |
284 | switch ($this->type) { | |
285 | case Trophy::TYPE_IMAGE: | |
286 | $fileExtension = WCF::getSession()->getVar('trophyImage-' . $this->tmpHash); | |
287 | ||
288 | if ($fileExtension === null) { | |
289 | throw new UserInputException('imageUpload'); | |
290 | } | |
291 | ||
292 | if (!\file_exists(WCF_DIR . 'images/trophy/tmp_' . $this->tmpHash . '.' . $fileExtension)) { | |
293 | throw new UserInputException('imageUpload'); | |
294 | } | |
295 | break; | |
296 | ||
297 | case Trophy::TYPE_BADGE: | |
298 | if (empty($this->iconName)) { | |
299 | throw new UserInputException('iconName'); | |
300 | } | |
301 | ||
afd474ba AE |
302 | if (!FontAwesomeIcon::isValidString($this->iconName)) { |
303 | throw new UserInputException('iconName'); | |
304 | } | |
305 | ||
a9229942 TD |
306 | if (empty($this->iconColor)) { |
307 | throw new UserInputException('iconColor'); | |
308 | } | |
309 | ||
310 | if (empty($this->badgeColor)) { | |
311 | throw new UserInputException('badgeColor'); | |
312 | } | |
313 | break; | |
314 | } | |
315 | } | |
316 | ||
317 | /** | |
318 | * @inheritDoc | |
319 | */ | |
320 | public function save() | |
321 | { | |
322 | parent::save(); | |
323 | ||
324 | $data = []; | |
325 | if ($this->type == Trophy::TYPE_BADGE) { | |
326 | $data['iconName'] = $this->iconName; | |
327 | $data['iconColor'] = $this->iconColor; | |
328 | $data['badgeColor'] = $this->badgeColor; | |
329 | } | |
330 | ||
331 | $this->objectAction = new TrophyAction([], 'create', [ | |
332 | 'data' => \array_merge($this->additionalFields, $data, [ | |
333 | 'title' => $this->title, | |
334 | 'description' => $this->description, | |
335 | 'categoryID' => $this->categoryID, | |
336 | 'type' => $this->type, | |
337 | 'isDisabled' => $this->isDisabled, | |
338 | 'awardAutomatically' => $this->awardAutomatically, | |
339 | 'revokeAutomatically' => $this->revokeAutomatically, | |
340 | 'trophyUseHtml' => $this->trophyUseHtml, | |
341 | 'showOrder' => $this->showOrder, | |
342 | ]), | |
343 | 'tmpHash' => $this->tmpHash, | |
344 | ]); | |
345 | $returnValues = $this->objectAction->executeAction(); | |
346 | ||
347 | $this->saveI18n($returnValues['returnValues'], TrophyEditor::class); | |
348 | ||
349 | // transform conditions array into one-dimensional array | |
350 | $conditions = []; | |
351 | foreach ($this->conditions as $groupedObjectTypes) { | |
352 | foreach ($groupedObjectTypes as $objectTypes) { | |
353 | if (\is_array($objectTypes)) { | |
354 | $conditions = \array_merge($conditions, $objectTypes); | |
355 | } else { | |
356 | $conditions[] = $objectTypes; | |
357 | } | |
358 | } | |
359 | } | |
360 | ||
361 | ConditionHandler::getInstance()->createConditions($returnValues['returnValues']->trophyID, $conditions); | |
362 | ||
363 | $this->reset(); | |
364 | ||
365 | WCF::getTPL()->assign([ | |
366 | 'objectEditLink' => LinkHandler::getInstance()->getControllerLink( | |
367 | TrophyEditForm::class, | |
368 | ['id' => $returnValues['returnValues']->trophyID] | |
369 | ), | |
370 | ]); | |
371 | } | |
372 | ||
373 | /** | |
374 | * @inheritDoc | |
375 | */ | |
376 | public function reset() | |
377 | { | |
378 | parent::reset(); | |
379 | ||
380 | $this->isDisabled = $this->awardAutomatically = $this->categoryID = $this->trophyUseHtml = $this->showOrder = $this->revokeAutomatically = 0; | |
381 | $this->type = Trophy::TYPE_BADGE; | |
382 | $this->iconName = $this->uploadedImageURL = ''; | |
383 | $this->iconColor = 'rgba(255, 255, 255, 1)'; | |
384 | $this->badgeColor = 'rgba(50, 92, 132, 1)'; | |
afd474ba | 385 | $this->iconName = 'trophy;false'; |
a9229942 TD |
386 | $this->tmpHash = StringUtil::getRandomID(); |
387 | ||
388 | foreach ($this->conditions as $conditions) { | |
389 | foreach ($conditions as $condition) { | |
390 | $condition->getProcessor()->reset(); | |
391 | } | |
392 | } | |
393 | } | |
394 | ||
395 | /** | |
396 | * @inheritDoc | |
397 | */ | |
398 | public function assignVariables() | |
399 | { | |
400 | parent::assignVariables(); | |
401 | ||
402 | WCF::getTPL()->assign([ | |
403 | 'categoryID' => $this->categoryID, | |
404 | 'type' => $this->type, | |
405 | 'isDisabled' => $this->isDisabled, | |
406 | 'iconName' => $this->iconName, | |
407 | 'iconColor' => $this->iconColor, | |
408 | 'badgeColor' => $this->badgeColor, | |
afd474ba | 409 | 'icon' => FontAwesomeIcon::fromString($this->iconName), |
a9229942 TD |
410 | 'trophyCategories' => TrophyCategoryCache::getInstance()->getCategories(), |
411 | 'groupedObjectTypes' => $this->conditions, | |
412 | 'awardAutomatically' => $this->awardAutomatically, | |
413 | 'revokeAutomatically' => $this->revokeAutomatically, | |
414 | 'availableTypes' => $this->availableTypes, | |
415 | 'tmpHash' => $this->tmpHash, | |
416 | 'uploadedImageURL' => $this->uploadedImageURL, | |
417 | 'trophyUseHtml' => $this->trophyUseHtml, | |
418 | 'showOrder' => $this->showOrder, | |
419 | ]); | |
420 | } | |
60239287 | 421 | } |