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