Merge pull request #6006 from WoltLab/file-processor-can-adopt
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / acp / form / UserRankAddForm.class.php
CommitLineData
320f4a6d 1<?php
a9229942 2
320f4a6d 3namespace wcf\acp\form;
a9229942 4
320f4a6d
MW
5use wcf\data\user\group\UserGroup;
6use wcf\data\user\rank\UserRankAction;
7use wcf\data\user\rank\UserRankEditor;
fe0457a9 8use wcf\data\user\UserProfile;
320f4a6d
MW
9use wcf\form\AbstractForm;
10use wcf\system\exception\UserInputException;
9517b7a0 11use wcf\system\file\upload\UploadField;
12use wcf\system\file\upload\UploadFile;
13use wcf\system\file\upload\UploadHandler;
320f4a6d
MW
14use wcf\system\language\I18nHandler;
15use wcf\system\Regex;
3fb859c9 16use wcf\system\request\LinkHandler;
320f4a6d
MW
17use wcf\system\WCF;
18use wcf\util\StringUtil;
19
20/**
21 * Shows the user rank add form.
a9229942
TD
22 *
23 * @author Marcel Werk
24 * @copyright 2001-2019 WoltLab GmbH
25 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
320f4a6d 26 */
a9229942
TD
27class UserRankAddForm extends AbstractForm
28{
29 /**
30 * @inheritDoc
31 */
32 public $activeMenuItem = 'wcf.acp.menu.link.user.rank.add';
33
34 /**
35 * @inheritDoc
36 */
37 public $neededPermissions = ['admin.user.rank.canManageRank'];
38
39 /**
40 * @inheritDoc
41 */
42 public $neededModules = ['MODULE_USER_RANK'];
43
44 /**
45 * rank group id
46 * @var int
47 */
48 public $groupID = 0;
49
50 /**
51 * rank title
52 * @var string
53 */
54 public $rankTitle = '';
55
56 /**
57 * CSS class name
58 * @var string
59 */
60 public $cssClassName = '';
61
62 /**
63 * custom CSS class name
64 * @var string
65 */
66 public $customCssClassName = '';
67
68 /**
69 * required activity points to acquire the rank
70 * @var int
71 */
72 public $requiredPoints = 0;
73
74 /**
75 * @deprecated since 5.4
76 */
77 public $rankImage = '';
78
79 /**
80 * number of image repeats
81 * @var int
82 */
83 public $repeatImage = 1;
84
85 /**
86 * required gender setting (1=male; 2=female)
87 * @var int
88 */
89 public $requiredGender = 0;
90
91 /**
92 * hide generic user title
93 * @var int
94 */
95 public $hideTitle = 0;
96
97 /**
98 * list of pre-defined css class names
99 * @var string[]
100 */
101 public $availableCssClassNames = [
102 'yellow',
103 'orange',
104 'brown',
105 'red',
106 'pink',
107 'purple',
108 'blue',
109 'green',
110 'black',
111
112 'none', /* not a real value */
113 'custom', /* not a real value */
114 ];
115
116 /**
117 * @var UploadFile[]
118 */
119 public $removedRankImages;
120
121 /**
122 * @var UploadFile|bool
123 */
124 public $rankImageFile;
125
126 /**
127 * @inheritDoc
128 */
129 public function readParameters()
130 {
131 parent::readParameters();
132
133 I18nHandler::getInstance()->register('rankTitle');
134
135 $this->rebuildUploadField();
136 }
137
138 protected function rebuildUploadField(): void
139 {
140 if (UploadHandler::getInstance()->isRegisteredFieldId('rankImage')) {
141 UploadHandler::getInstance()->unregisterUploadField('rankImage');
142 }
143 $field = new UploadField('rankImage');
144 $field->setImageOnly(true);
145 $field->setAllowSvgImage(true);
146 $field->maxFiles = 1;
147 UploadHandler::getInstance()->registerUploadField($field);
148 }
149
150 /**
151 * @inheritDoc
152 */
153 public function readFormParameters()
154 {
155 parent::readFormParameters();
156
157 I18nHandler::getInstance()->readValues();
158
159 if (I18nHandler::getInstance()->isPlainValue('rankTitle')) {
160 $this->rankTitle = I18nHandler::getInstance()->getValue('rankTitle');
161 }
162 if (isset($_POST['cssClassName'])) {
163 $this->cssClassName = StringUtil::trim($_POST['cssClassName']);
164 }
165 if (isset($_POST['customCssClassName'])) {
166 $this->customCssClassName = StringUtil::trim($_POST['customCssClassName']);
167 }
168 if (isset($_POST['groupID'])) {
169 $this->groupID = \intval($_POST['groupID']);
170 }
171 if (isset($_POST['requiredPoints'])) {
172 $this->requiredPoints = \intval($_POST['requiredPoints']);
173 }
174 if (isset($_POST['repeatImage'])) {
175 $this->repeatImage = \intval($_POST['repeatImage']);
176 }
177 if (isset($_POST['requiredGender'])) {
178 $this->requiredGender = \intval($_POST['requiredGender']);
179 }
180 if (isset($_POST['hideTitle'])) {
181 $this->hideTitle = \intval($_POST['hideTitle']);
182 }
183
055b3277 184 $this->removedRankImages = UploadHandler::getInstance()->getRemovedFilesByFieldId('rankImage');
a9229942
TD
185 $rankImageFiles = UploadHandler::getInstance()->getFilesByFieldId('rankImage');
186 $this->rankImageFile = \reset($rankImageFiles);
187 }
188
189 /**
190 * @inheritDoc
191 */
192 public function validate()
193 {
194 parent::validate();
195
196 // validate label
197 if (!I18nHandler::getInstance()->validateValue('rankTitle')) {
198 if (I18nHandler::getInstance()->isPlainValue('rankTitle')) {
199 throw new UserInputException('rankTitle');
200 } else {
201 throw new UserInputException('rankTitle', 'multilingual');
202 }
203 }
204
205 // validate group
206 if (!$this->groupID) {
207 throw new UserInputException('groupID');
208 }
209 $userGroup = UserGroup::getGroupByID($this->groupID);
210 if ($userGroup === null || $userGroup->groupType == UserGroup::GUESTS || $userGroup->groupType == UserGroup::EVERYONE) {
211 throw new UserInputException('groupID', 'invalid');
212 }
213
214 // css class name
215 if (empty($this->cssClassName)) {
216 throw new UserInputException('cssClassName', 'empty');
217 } elseif (!\in_array($this->cssClassName, $this->availableCssClassNames)) {
218 throw new UserInputException('cssClassName', 'invalid');
219 } elseif ($this->cssClassName == 'custom') {
220 if (!empty($this->customCssClassName) && !Regex::compile('^-?[_a-zA-Z]+[_a-zA-Z0-9-]+$')->match($this->customCssClassName)) {
221 throw new UserInputException('cssClassName', 'invalid');
222 }
223 }
224
225 // required gender
226 if ($this->requiredGender < 0 || $this->requiredGender > UserProfile::GENDER_OTHER) {
227 $this->requiredGender = 0;
228 }
229
230 if ($this->hideTitle && !$this->rankImageFile) {
231 throw new UserInputException('hideTitle', 'rankImage');
232 }
233 }
234
235 /**
236 * @inheritDoc
237 */
238 public function save()
239 {
240 parent::save();
241
242 // save label
243 $this->objectAction = new UserRankAction([], 'create', [
244 'data' => \array_merge($this->additionalFields, [
245 'rankTitle' => $this->rankTitle,
246 'cssClassName' => $this->cssClassName == 'custom' ? $this->customCssClassName : $this->cssClassName,
247 'groupID' => $this->groupID,
248 'requiredPoints' => $this->requiredPoints,
249 'repeatImage' => $this->repeatImage,
250 'requiredGender' => $this->requiredGender,
251 'hideTitle' => ($this->hideTitle ? 1 : 0),
252 ]),
253 'rankImageFile' => $this->rankImageFile,
254 ]);
255 $returnValues = $this->objectAction->executeAction();
256 $rankID = $returnValues['returnValues']->rankID;
257
258 if (!I18nHandler::getInstance()->isPlainValue('rankTitle')) {
259 I18nHandler::getInstance()->save('rankTitle', 'wcf.user.rank.userRank' . $rankID, 'wcf.user', 1);
260
261 // update name
262 $rankEditor = new UserRankEditor($returnValues['returnValues']);
263 $rankEditor->update([
264 'rankTitle' => 'wcf.user.rank.userRank' . $rankID,
265 ]);
266 }
267 $this->saved();
268
269 // reset values
270 $this->rankTitle = $this->cssClassName = $this->customCssClassName = $this->rankImage = '';
271 $this->groupID = $this->requiredPoints = $this->requiredGender = $this->hideTitle = 0;
272 $this->repeatImage = 1;
273
274 I18nHandler::getInstance()->reset();
275 $this->rebuildUploadField();
276
277 // show success message
278 WCF::getTPL()->assign([
279 'success' => true,
280 'objectEditLink' => LinkHandler::getInstance()->getControllerLink(
281 UserRankEditForm::class,
282 ['id' => $rankID]
283 ),
284 ]);
285 }
286
287 /**
288 * @inheritDoc
289 */
290 public function assignVariables()
291 {
292 parent::assignVariables();
293
294 I18nHandler::getInstance()->assignVariables();
295
296 WCF::getTPL()->assign([
297 'action' => 'add',
298 'availableCssClassNames' => $this->availableCssClassNames,
299 'cssClassName' => $this->cssClassName,
300 'customCssClassName' => $this->customCssClassName,
301 'groupID' => $this->groupID,
302 'rankTitle' => $this->rankTitle,
303 'availableGroups' => UserGroup::getSortedGroupsByType([], [UserGroup::GUESTS, UserGroup::EVERYONE]),
304 'requiredPoints' => $this->requiredPoints,
305 'rankImage' => $this->rankImage,
306 'repeatImage' => $this->repeatImage,
307 'requiredGender' => $this->requiredGender,
308 'hideTitle' => $this->hideTitle,
309 ]);
310 }
320f4a6d 311}