Change `notValid` to `invalid`
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / acp / form / UserRankAddForm.class.php
CommitLineData
320f4a6d
MW
1<?php
2namespace wcf\acp\form;
320f4a6d
MW
3use wcf\data\user\group\UserGroup;
4use wcf\data\user\rank\UserRankAction;
5use wcf\data\user\rank\UserRankEditor;
6use wcf\form\AbstractForm;
7use wcf\system\exception\UserInputException;
8use wcf\system\language\I18nHandler;
9use wcf\system\Regex;
10use wcf\system\WCF;
11use wcf\util\StringUtil;
12
13/**
14 * Shows the user rank add form.
15 *
16 * @author Marcel Werk
7d739af0 17 * @copyright 2001-2016 WoltLab GmbH
320f4a6d 18 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
e71525e4 19 * @package WoltLabSuite\Core\Acp\Form
320f4a6d
MW
20 */
21class UserRankAddForm extends AbstractForm {
22 /**
0fcfe5f6 23 * @inheritDoc
320f4a6d
MW
24 */
25 public $activeMenuItem = 'wcf.acp.menu.link.user.rank.add';
26
27 /**
0fcfe5f6 28 * @inheritDoc
320f4a6d 29 */
058cbd6a 30 public $neededPermissions = ['admin.user.rank.canManageRank'];
320f4a6d 31
f5f0b11c 32 /**
0fcfe5f6 33 * @inheritDoc
f5f0b11c 34 */
058cbd6a 35 public $neededModules = ['MODULE_USER_RANK'];
f5f0b11c 36
320f4a6d
MW
37 /**
38 * rank group id
39 * @var integer
40 */
41 public $groupID = 0;
42
43 /**
44 * rank title
45 * @var string
46 */
47 public $rankTitle = '';
48
49 /**
50 * CSS class name
51 * @var string
52 */
53 public $cssClassName = '';
54
55 /**
56 * custom CSS class name
57 * @var string
58 */
59 public $customCssClassName = '';
60
61 /**
62 * required activity points to acquire the rank
63 * @var integer
64 */
65 public $requiredPoints = 0;
66
67 /**
68 * path to user rank image
69 * @var string
70 */
71 public $rankImage = '';
72
73 /**
74 * number of image repeats
75 * @var integer
76 */
77 public $repeatImage = 1;
78
79 /**
80 * required gender setting (1=male; 2=female)
81 * @var integer
82 */
83 public $requiredGender = 0;
84
85 /**
86 * list of pre-defined css class names
7a23a706 87 * @var string[]
320f4a6d 88 */
058cbd6a 89 public $availableCssClassNames = [
320f4a6d
MW
90 'yellow',
91 'orange',
92 'brown',
93 'red',
94 'pink',
95 'purple',
96 'blue',
97 'green',
98 'black',
99
100 'none', /* not a real value */
101 'custom' /* not a real value */
058cbd6a 102 ];
320f4a6d
MW
103
104 /**
0fcfe5f6 105 * @inheritDoc
320f4a6d
MW
106 */
107 public function readParameters() {
108 parent::readParameters();
109
110 I18nHandler::getInstance()->register('rankTitle');
111 }
112
113 /**
0fcfe5f6 114 * @inheritDoc
320f4a6d
MW
115 */
116 public function readFormParameters() {
117 parent::readFormParameters();
118
119 I18nHandler::getInstance()->readValues();
120
121 if (I18nHandler::getInstance()->isPlainValue('rankTitle')) $this->rankTitle = I18nHandler::getInstance()->getValue('rankTitle');
122 if (isset($_POST['cssClassName'])) $this->cssClassName = StringUtil::trim($_POST['cssClassName']);
123 if (isset($_POST['customCssClassName'])) $this->customCssClassName = StringUtil::trim($_POST['customCssClassName']);
124 if (isset($_POST['groupID'])) $this->groupID = intval($_POST['groupID']);
125 if (isset($_POST['requiredPoints'])) $this->requiredPoints = intval($_POST['requiredPoints']);
126 if (isset($_POST['rankImage'])) $this->rankImage = StringUtil::trim($_POST['rankImage']);
127 if (isset($_POST['repeatImage'])) $this->repeatImage = intval($_POST['repeatImage']);
128 if (isset($_POST['requiredGender'])) $this->requiredGender = intval($_POST['requiredGender']);
129 }
130
131 /**
0fcfe5f6 132 * @inheritDoc
320f4a6d
MW
133 */
134 public function validate() {
135 parent::validate();
136
137 // validate label
138 if (!I18nHandler::getInstance()->validateValue('rankTitle')) {
139 if (I18nHandler::getInstance()->isPlainValue('rankTitle')) {
140 throw new UserInputException('rankTitle');
141 }
142 else {
143 throw new UserInputException('rankTitle', 'multilingual');
144 }
145 }
146
147 // validate group
148 if (!$this->groupID) {
149 throw new UserInputException('groupID');
150 }
151 $userGroup = UserGroup::getGroupByID($this->groupID);
152 if ($userGroup === null || $userGroup->groupType == UserGroup::GUESTS || $userGroup->groupType == UserGroup::EVERYONE) {
063bbf46 153 throw new UserInputException('groupID', 'invalid');
320f4a6d
MW
154 }
155
156 // css class name
157 if (empty($this->cssClassName)) {
158 throw new UserInputException('cssClassName', 'empty');
159 }
160 else if (!in_array($this->cssClassName, $this->availableCssClassNames)) {
063bbf46 161 throw new UserInputException('cssClassName', 'invalid');
320f4a6d
MW
162 }
163 else if ($this->cssClassName == 'custom') {
164 if (!empty($this->customCssClassName) && !Regex::compile('^-?[_a-zA-Z]+[_a-zA-Z0-9-]+$')->match($this->customCssClassName)) {
063bbf46 165 throw new UserInputException('cssClassName', 'invalid');
320f4a6d
MW
166 }
167 }
168
169 // required gender
170 if ($this->requiredGender < 0 || $this->requiredGender > 2) {
171 $this->requiredGender = 0;
172 }
173 }
174
175 /**
0fcfe5f6 176 * @inheritDoc
320f4a6d
MW
177 */
178 public function save() {
179 parent::save();
180
181 // save label
058cbd6a 182 $this->objectAction = new UserRankAction([], 'create', ['data' => array_merge($this->additionalFields, [
320f4a6d 183 'rankTitle' => $this->rankTitle,
63b9817b 184 'cssClassName' => $this->cssClassName == 'custom' ? $this->customCssClassName : $this->cssClassName,
320f4a6d
MW
185 'groupID' => $this->groupID,
186 'requiredPoints' => $this->requiredPoints,
187 'rankImage' => $this->rankImage,
188 'repeatImage' => $this->repeatImage,
189 'requiredGender' => $this->requiredGender
058cbd6a 190 ])]);
320f4a6d
MW
191 $this->objectAction->executeAction();
192
193 if (!I18nHandler::getInstance()->isPlainValue('rankTitle')) {
194 $returnValues = $this->objectAction->getReturnValues();
195 $rankID = $returnValues['returnValues']->rankID;
a37b39b6 196 I18nHandler::getInstance()->save('rankTitle', 'wcf.user.rank.userRank'.$rankID, 'wcf.user', 1);
320f4a6d
MW
197
198 // update name
199 $rankEditor = new UserRankEditor($returnValues['returnValues']);
058cbd6a 200 $rankEditor->update([
320f4a6d 201 'rankTitle' => 'wcf.user.rank.userRank'.$rankID
058cbd6a 202 ]);
320f4a6d
MW
203 }
204 $this->saved();
205
206 // reset values
207 $this->rankTitle = $this->cssClassName = $this->customCssClassName = $this->rankImage = '';
066c0080
MK
208 $this->groupID = $this->requiredPoints = $this->requiredGender = 0;
209 $this->repeatImage = 1;
320f4a6d
MW
210
211 I18nHandler::getInstance()->reset();
212
47b90344
MS
213 // show success message
214 WCF::getTPL()->assign('success', true);
320f4a6d
MW
215 }
216
217 /**
0fcfe5f6 218 * @inheritDoc
320f4a6d
MW
219 */
220 public function assignVariables() {
221 parent::assignVariables();
222
223 I18nHandler::getInstance()->assignVariables();
224
058cbd6a 225 WCF::getTPL()->assign([
320f4a6d
MW
226 'action' => 'add',
227 'availableCssClassNames' => $this->availableCssClassNames,
228 'cssClassName' => $this->cssClassName,
229 'customCssClassName' => $this->customCssClassName,
230 'groupID' => $this->groupID,
231 'rankTitle' => $this->rankTitle,
058cbd6a 232 'availableGroups' => UserGroup::getGroupsByType([], [UserGroup::GUESTS, UserGroup::EVERYONE]),
320f4a6d
MW
233 'requiredPoints' => $this->requiredPoints,
234 'rankImage' => $this->rankImage,
235 'repeatImage' => $this->repeatImage,
236 'requiredGender' => $this->requiredGender
058cbd6a 237 ]);
320f4a6d
MW
238 }
239}