Commit | Line | Data |
---|---|---|
158bd3ca TD |
1 | <?php |
2 | namespace wcf\acp\form; | |
320f4a6d | 3 | use wcf\data\user\avatar\Gravatar; |
320f4a6d | 4 | use wcf\data\user\avatar\UserAvatar; |
fc69b61d | 5 | use wcf\data\user\avatar\UserAvatarAction; |
2bc9f31d | 6 | use wcf\data\user\group\UserGroup; |
158bd3ca TD |
7 | use wcf\data\user\User; |
8 | use wcf\data\user\UserAction; | |
9 | use wcf\data\user\UserEditor; | |
320f4a6d | 10 | use wcf\data\user\UserProfileAction; |
158bd3ca TD |
11 | use wcf\form\AbstractForm; |
12 | use wcf\system\exception\IllegalLinkException; | |
13 | use wcf\system\exception\PermissionDeniedException; | |
fc69b61d MW |
14 | use wcf\system\exception\UserInputException; |
15 | use wcf\system\moderation\queue\ModerationQueueManager; | |
2bc9f31d | 16 | use wcf\system\WCF; |
158bd3ca TD |
17 | use wcf\util\StringUtil; |
18 | ||
19 | /** | |
20 | * Shows the user edit form. | |
6e048dca | 21 | * |
158bd3ca | 22 | * @author Marcel Werk |
ca4ba303 | 23 | * @copyright 2001-2014 WoltLab GmbH |
158bd3ca TD |
24 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> |
25 | * @package com.woltlab.wcf | |
26 | * @subpackage acp.form | |
9f959ced | 27 | * @category Community Framework |
158bd3ca TD |
28 | */ |
29 | class UserEditForm extends UserAddForm { | |
30 | /** | |
0ad90fc3 | 31 | * @see \wcf\page\AbstractPage::$activeMenuItem |
158bd3ca | 32 | */ |
906091f5 | 33 | public $activeMenuItem = 'wcf.acp.menu.link.user.management'; |
158bd3ca TD |
34 | |
35 | /** | |
0ad90fc3 | 36 | * @see \wcf\page\AbstractPage::$neededPermissions |
158bd3ca TD |
37 | */ |
38 | public $neededPermissions = array('admin.user.canEditUser'); | |
39 | ||
40 | /** | |
41 | * user id | |
9f959ced | 42 | * @var integer |
158bd3ca TD |
43 | */ |
44 | public $userID = 0; | |
45 | ||
46 | /** | |
47 | * user editor object | |
0ad90fc3 | 48 | * @var \wcf\data\user\UserEditor |
158bd3ca TD |
49 | */ |
50 | public $user = null; | |
51 | ||
baa1ddc3 MW |
52 | /** |
53 | * ban status | |
06355ec3 | 54 | * @var boolean |
baa1ddc3 MW |
55 | */ |
56 | public $banned = 0; | |
57 | ||
58 | /** | |
59 | * ban reason | |
06355ec3 | 60 | * @var string |
baa1ddc3 MW |
61 | */ |
62 | public $banReason = ''; | |
63 | ||
320f4a6d MW |
64 | /** |
65 | * user avatar object | |
0ad90fc3 | 66 | * @var \wcf\data\user\avatar\UserAvatar |
320f4a6d MW |
67 | */ |
68 | public $userAvatar = null; | |
69 | ||
70 | /** | |
71 | * avatar type | |
72 | * @var string | |
73 | */ | |
74 | public $avatarType = 'none'; | |
75 | ||
76 | /** | |
77 | * true to disable this avatar | |
06355ec3 | 78 | * @var boolean |
320f4a6d MW |
79 | */ |
80 | public $disableAvatar = 0; | |
81 | ||
82 | /** | |
83 | * reason | |
06355ec3 | 84 | * @var string |
320f4a6d MW |
85 | */ |
86 | public $disableAvatarReason = ''; | |
87 | ||
158bd3ca | 88 | /** |
0ad90fc3 | 89 | * @see \wcf\page\IPage::readParameters() |
158bd3ca TD |
90 | */ |
91 | public function readParameters() { | |
08d76680 | 92 | if (isset($_REQUEST['id'])) $this->userID = intval($_REQUEST['id']); |
08d76680 AE |
93 | $user = new User($this->userID); |
94 | if (!$user->userID) { | |
95 | throw new IllegalLinkException(); | |
96 | } | |
97 | ||
98 | $this->user = new UserEditor($user); | |
99 | if (!UserGroup::isAccessibleGroup($this->user->getGroupIDs())) { | |
100 | throw new PermissionDeniedException(); | |
158bd3ca | 101 | } |
c897c9b4 | 102 | |
cf251c63 MW |
103 | parent::readParameters(); |
104 | } | |
105 | ||
106 | /** | |
107 | * wcf\acp\form\AbstractOptionListForm::initOptionHandler() | |
108 | */ | |
109 | protected function initOptionHandler() { | |
c897c9b4 | 110 | $this->optionHandler->setUser($this->user->getDecoratedObject()); |
158bd3ca TD |
111 | } |
112 | ||
113 | /** | |
0ad90fc3 | 114 | * @see \wcf\page\IPage::readFormParameters() |
158bd3ca TD |
115 | */ |
116 | public function readFormParameters() { | |
117 | parent::readFormParameters(); | |
118 | ||
119 | if (!WCF::getSession()->getPermission('admin.user.canEditPassword')) $this->password = $this->confirmPassword = ''; | |
120 | if (!WCF::getSession()->getPermission('admin.user.canEditMailAddress')) $this->email = $this->confirmEmail = $this->user->email; | |
baa1ddc3 MW |
121 | |
122 | if (!empty($_POST['banned'])) $this->banned = 1; | |
123 | if (isset($_POST['banReason'])) $this->banReason = StringUtil::trim($_POST['banReason']); | |
320f4a6d | 124 | if (isset($_POST['avatarType'])) $this->avatarType = $_POST['avatarType']; |
57f097e8 MS |
125 | |
126 | if (WCF::getSession()->getPermission('admin.user.canDisableAvatar')) { | |
127 | if (!empty($_POST['disableAvatar'])) $this->disableAvatar = 1; | |
128 | if (isset($_POST['disableAvatarReason'])) $this->disableAvatarReason = StringUtil::trim($_POST['disableAvatarReason']); | |
129 | } | |
158bd3ca TD |
130 | } |
131 | ||
132 | /** | |
0ad90fc3 | 133 | * @see \wcf\page\IPage::readData() |
158bd3ca TD |
134 | */ |
135 | public function readData() { | |
15fa2802 | 136 | if (empty($_POST)) { |
158bd3ca TD |
137 | // get visible languages |
138 | $this->readVisibleLanguages(); | |
139 | ||
140 | // default values | |
141 | $this->readDefaultValues(); | |
142 | } | |
143 | ||
504c8f9d AE |
144 | parent::readData(); |
145 | ||
320f4a6d MW |
146 | // get avatar object |
147 | if ($this->avatarType == 'custom') { | |
148 | $this->userAvatar = new UserAvatar($this->user->avatarID); | |
149 | } | |
158bd3ca TD |
150 | } |
151 | ||
152 | /** | |
153 | * Gets the selected languages. | |
154 | */ | |
155 | protected function readVisibleLanguages() { | |
156 | $this->visibleLanguages = $this->user->getLanguageIDs(); | |
157 | } | |
158 | ||
159 | /** | |
160 | * Gets the default values. | |
161 | */ | |
162 | protected function readDefaultValues() { | |
163 | $this->username = $this->user->username; | |
164 | $this->email = $this->confirmEmail = $this->user->email; | |
d3db5e74 | 165 | $this->groupIDs = $this->user->getGroupIDs(true); |
158bd3ca | 166 | $this->languageID = $this->user->languageID; |
baa1ddc3 MW |
167 | $this->banned = $this->user->banned; |
168 | $this->banReason = $this->user->banReason; | |
320f4a6d MW |
169 | $this->userTitle = $this->user->userTitle; |
170 | ||
171 | $this->signature = $this->user->signature; | |
172 | $this->signatureEnableBBCodes = $this->user->signatureEnableBBCodes; | |
173 | $this->signatureEnableSmilies = $this->user->signatureEnableSmilies; | |
174 | $this->signatureEnableHtml = $this->user->signatureEnableHtml; | |
175 | $this->disableSignature = $this->user->disableSignature; | |
176 | $this->disableSignatureReason = $this->user->disableSignatureReason; | |
177 | $this->disableAvatar = $this->user->disableAvatar; | |
178 | $this->disableAvatarReason = $this->user->disableAvatarReason; | |
57f097e8 | 179 | |
320f4a6d MW |
180 | if ($this->user->avatarID) $this->avatarType = 'custom'; |
181 | else if (MODULE_GRAVATAR && $this->user->enableGravatar) $this->avatarType = 'gravatar'; | |
158bd3ca TD |
182 | } |
183 | ||
184 | /** | |
0ad90fc3 | 185 | * @see \wcf\page\IPage::assignVariables() |
158bd3ca TD |
186 | */ |
187 | public function assignVariables() { | |
188 | parent::assignVariables(); | |
189 | ||
190 | WCF::getTPL()->assign(array( | |
191 | 'userID' => $this->user->userID, | |
192 | 'action' => 'edit', | |
193 | 'url' => '', | |
194 | 'markedUsers' => 0, | |
baa1ddc3 MW |
195 | 'user' => $this->user, |
196 | 'banned' => $this->banned, | |
320f4a6d MW |
197 | 'banReason' => $this->banReason, |
198 | 'avatarType' => $this->avatarType, | |
199 | 'disableAvatar' => $this->disableAvatar, | |
200 | 'disableAvatarReason' => $this->disableAvatarReason, | |
201 | 'userAvatar' => $this->userAvatar | |
158bd3ca TD |
202 | )); |
203 | } | |
204 | ||
205 | /** | |
0ad90fc3 | 206 | * @see \wcf\form\IForm::save() |
158bd3ca TD |
207 | */ |
208 | public function save() { | |
209 | AbstractForm::save(); | |
210 | ||
320f4a6d MW |
211 | // handle avatar |
212 | if ($this->avatarType != 'custom') { | |
213 | // delete custom avatar | |
214 | if ($this->user->avatarID) { | |
215 | $action = new UserAvatarAction(array($this->user->avatarID), 'delete'); | |
216 | $action->executeAction(); | |
217 | } | |
218 | } | |
219 | switch ($this->avatarType) { | |
220 | case 'none': | |
221 | $avatarData = array( | |
222 | 'avatarID' => null, | |
223 | 'enableGravatar' => 0 | |
224 | ); | |
2d63c13c MS |
225 | break; |
226 | ||
320f4a6d MW |
227 | case 'custom': |
228 | $avatarData = array( | |
320f4a6d MW |
229 | 'enableGravatar' => 0 |
230 | ); | |
2d63c13c MS |
231 | break; |
232 | ||
320f4a6d MW |
233 | case 'gravatar': |
234 | $avatarData = array( | |
235 | 'avatarID' => null, | |
236 | 'enableGravatar' => 1 | |
237 | ); | |
2d63c13c | 238 | break; |
320f4a6d | 239 | } |
57f097e8 MS |
240 | |
241 | if (WCF::getSession()->getPermission('admin.user.canDisableAvatar')) { | |
242 | $avatarData['disableAvatar'] = $this->disableAvatar; | |
243 | $avatarData['disableAvatarReason'] = $this->disableAvatarReason; | |
244 | } | |
245 | ||
320f4a6d MW |
246 | $this->additionalFields = array_merge($this->additionalFields, $avatarData); |
247 | ||
158bd3ca TD |
248 | // add default groups |
249 | $defaultGroups = UserGroup::getAccessibleGroups(array(UserGroup::GUESTS, UserGroup::EVERYONE, UserGroup::USERS)); | |
250 | $oldGroupIDs = $this->user->getGroupIDs(); | |
251 | foreach ($oldGroupIDs as $oldGroupID) { | |
252 | if (isset($defaultGroups[$oldGroupID])) { | |
253 | $this->groupIDs[] = $oldGroupID; | |
254 | } | |
255 | } | |
256 | $this->groupIDs = array_unique($this->groupIDs); | |
257 | ||
258 | // save user | |
f277d540 | 259 | $saveOptions = $this->optionHandler->save(); |
158bd3ca | 260 | $this->additionalFields['languageID'] = $this->languageID; |
fd7969b5 MW |
261 | if (WCF::getSession()->getPermission('admin.user.canBanUser')) { |
262 | $this->additionalFields['banned'] = $this->banned; | |
263 | $this->additionalFields['banReason'] = $this->banReason; | |
264 | } | |
158bd3ca TD |
265 | $data = array( |
266 | 'data' => array_merge($this->additionalFields, array( | |
267 | 'username' => $this->username, | |
268 | 'email' => $this->email, | |
269 | 'password' => $this->password, | |
baa1ddc3 | 270 | 'banned' => $this->banned, |
320f4a6d MW |
271 | 'banReason' => $this->banReason, |
272 | 'userTitle' => $this->userTitle, | |
273 | 'signature' => $this->signature, | |
274 | 'signatureEnableBBCodes' => $this->signatureEnableBBCodes, | |
275 | 'signatureEnableSmilies' => $this->signatureEnableSmilies, | |
57f097e8 | 276 | 'signatureEnableHtml' => $this->signatureEnableHtml |
158bd3ca TD |
277 | )), |
278 | 'groups' => $this->groupIDs, | |
7e849270 | 279 | 'languageIDs' => $this->visibleLanguages, |
158bd3ca TD |
280 | 'options' => $saveOptions |
281 | ); | |
57f097e8 MS |
282 | |
283 | if (WCF::getSession()->getPermission('admin.user.canDisableSignature')) { | |
284 | $data['data']['disableSignature'] = $this->disableSignature; | |
285 | $data['data']['disableSignatureReason'] = $this->disableSignatureReason; | |
286 | } | |
287 | ||
635dd8c4 AE |
288 | $this->objectAction = new UserAction(array($this->userID), 'update', $data); |
289 | $this->objectAction->executeAction(); | |
158bd3ca | 290 | |
320f4a6d MW |
291 | // update user rank |
292 | $editor = new UserEditor(new User($this->userID)); | |
293 | if (MODULE_USER_RANK) { | |
294 | $action = new UserProfileAction(array($editor), 'updateUserRank'); | |
295 | $action->executeAction(); | |
296 | } | |
297 | if (MODULE_USERS_ONLINE) { | |
298 | $action = new UserProfileAction(array($editor), 'updateUserOnlineMarking'); | |
299 | $action->executeAction(); | |
300 | } | |
fc69b61d MW |
301 | |
302 | // remove assignments | |
303 | $sql = "DELETE FROM wcf".WCF_N."_moderation_queue_to_user | |
304 | WHERE userID = ?"; | |
305 | $statement = WCF::getDB()->prepareStatement($sql); | |
306 | $statement->execute(array($this->user->userID)); | |
307 | ||
308 | // reset moderation count | |
309 | ModerationQueueManager::getInstance()->resetModerationCount($this->user->userID); | |
158bd3ca TD |
310 | $this->saved(); |
311 | ||
312 | // reset password | |
313 | $this->password = $this->confirmPassword = ''; | |
9f959ced | 314 | |
158bd3ca TD |
315 | // show success message |
316 | WCF::getTPL()->assign('success', true); | |
317 | } | |
318 | ||
319 | /** | |
0ad90fc3 | 320 | * @see \wcf\acp\form\UserAddForm::validateUsername() |
158bd3ca TD |
321 | */ |
322 | protected function validateUsername($username) { | |
838e315b | 323 | if (mb_strtolower($this->user->username) != mb_strtolower($username)) { |
158bd3ca TD |
324 | parent::validateUsername($username); |
325 | } | |
326 | } | |
327 | ||
328 | /** | |
0ad90fc3 | 329 | * @see \wcf\acp\form\UserAddForm::validateEmail() |
158bd3ca TD |
330 | */ |
331 | protected function validateEmail($email, $confirmEmail) { | |
838e315b | 332 | if (mb_strtolower($this->user->email) != mb_strtolower($email)) { |
f4683ba3 | 333 | parent::validateEmail($email, $this->confirmEmail); |
158bd3ca TD |
334 | } |
335 | } | |
336 | ||
337 | /** | |
0ad90fc3 | 338 | * @see \wcf\acp\form\UserAddForm::validatePassword() |
158bd3ca TD |
339 | */ |
340 | protected function validatePassword($password, $confirmPassword) { | |
341 | if (!empty($password) || !empty($confirmPassword)) { | |
342 | parent::validatePassword($password, $confirmPassword); | |
343 | } | |
344 | } | |
320f4a6d MW |
345 | |
346 | /** | |
347 | * Validates the user avatar. | |
348 | */ | |
349 | protected function validateAvatar() { | |
350 | if ($this->avatarType != 'custom' && $this->avatarType != 'gravatar') $this->avatarType = 'none'; | |
e3369fd2 | 351 | |
320f4a6d MW |
352 | try { |
353 | switch ($this->avatarType) { | |
354 | case 'custom': | |
355 | if (!$this->user->avatarID) { | |
356 | throw new UserInputException('customAvatar'); | |
357 | } | |
358 | break; | |
e3369fd2 | 359 | |
320f4a6d MW |
360 | case 'gravatar': |
361 | if (!MODULE_GRAVATAR) { | |
362 | $this->avatarType = 'none'; | |
363 | break; | |
364 | } | |
e3369fd2 | 365 | |
320f4a6d MW |
366 | // test gravatar |
367 | if (!Gravatar::test($this->user->email)) { | |
368 | throw new UserInputException('gravatar', 'notFound'); | |
369 | } | |
370 | } | |
371 | } | |
372 | catch (UserInputException $e) { | |
373 | $this->errorType[$e->getField()] = $e->getType(); | |
374 | } | |
375 | } | |
376 | ||
377 | /** | |
0ad90fc3 | 378 | * @see \wcf\form\IForm::validate() |
320f4a6d MW |
379 | */ |
380 | public function validate() { | |
381 | $this->validateAvatar(); | |
382 | ||
383 | parent::validate(); | |
384 | } | |
158bd3ca | 385 | } |