Fixed time zone calculation issue
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / form / AvatarEditForm.class.php
1 <?php
2 namespace wcf\form;
3 use wcf\data\user\avatar\Gravatar;
4 use wcf\data\user\avatar\UserAvatarAction;
5 use wcf\data\user\UserAction;
6 use wcf\system\exception\PermissionDeniedException;
7 use wcf\system\exception\UserInputException;
8 use wcf\system\menu\user\UserMenu;
9 use wcf\system\WCF;
10
11 /**
12 * Shows the avatar edit form.
13 *
14 * @author Marcel Werk
15 * @copyright 2001-2014 WoltLab GmbH
16 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
17 * @package com.woltlab.wcf
18 * @subpackage form
19 * @category Community Framework
20 */
21 class AvatarEditForm extends AbstractForm {
22 /**
23 * @see \wcf\page\AbstractPage::$enableTracking
24 */
25 public $enableTracking = true;
26
27 /**
28 * @see \wcf\page\AbstractPage::$loginRequired
29 */
30 public $loginRequired = true;
31
32 /**
33 * @see \wcf\page\AbstractPage::$templateName
34 */
35 public $templateName = 'avatarEdit';
36
37 /**
38 * avatar type
39 * @var string
40 */
41 public $avatarType = 'none';
42
43 /**
44 * @see \wcf\form\IForm::readFormParameters()
45 */
46 public function readFormParameters() {
47 parent::readFormParameters();
48
49 if (isset($_POST['avatarType'])) $this->avatarType = $_POST['avatarType'];
50 }
51
52 /**
53 * @see \wcf\form\IForm::validate()
54 */
55 public function validate() {
56 parent::validate();
57
58 if (WCF::getUser()->disableAvatar) throw new PermissionDeniedException();
59
60 if ($this->avatarType != 'custom' && $this->avatarType != 'gravatar') $this->avatarType = 'none';
61
62 switch ($this->avatarType) {
63 case 'custom':
64 if (!WCF::getUser()->avatarID) {
65 throw new UserInputException('custom');
66 }
67 break;
68
69 case 'gravatar':
70 if (!MODULE_GRAVATAR) {
71 $this->avatarType = 'none';
72 break;
73 }
74
75 // test gravatar
76 if (!Gravatar::test(WCF::getUser()->email)) {
77 throw new UserInputException('gravatar', 'notFound');
78 }
79 break;
80 }
81 }
82
83 /**
84 * @see \wcf\form\IForm::save()
85 */
86 public function save() {
87 parent::save();
88
89 if ($this->avatarType != 'custom') {
90 // delete custom avatar
91 if (WCF::getUser()->avatarID) {
92 $action = new UserAvatarAction(array(WCF::getUser()->avatarID), 'delete');
93 $action->executeAction();
94 }
95 }
96
97 // update user
98 switch ($this->avatarType) {
99 case 'none':
100 $data = array(
101 'avatarID' => null,
102 'enableGravatar' => 0
103 );
104 break;
105
106 case 'custom':
107 $data = array(
108 'enableGravatar' => 0
109 );
110 break;
111
112 case 'gravatar':
113 $data = array(
114 'avatarID' => null,
115 'enableGravatar' => 1
116 );
117 break;
118 }
119 $this->objectAction = new UserAction(array(WCF::getUser()), 'update', array(
120 'data' => array_merge($this->additionalFields, $data)
121 ));
122 $this->objectAction->executeAction();
123
124 $this->saved();
125 WCF::getTPL()->assign('success', true);
126 }
127
128 /**
129 * @see \wcf\page\IPage::readData()
130 */
131 public function readData() {
132 parent::readData();
133
134 if (empty($_POST)) {
135 if (WCF::getUser()->avatarID) $this->avatarType = 'custom';
136 else if (MODULE_GRAVATAR && WCF::getUser()->enableGravatar) $this->avatarType = 'gravatar';
137 }
138 }
139
140 /**
141 * @see \wcf\page\IPage::assignVariables()
142 */
143 public function assignVariables() {
144 parent::assignVariables();
145
146 WCF::getTPL()->assign(array(
147 'avatarType' => $this->avatarType
148 ));
149 }
150
151 /**
152 * @see \wcf\page\IPage::show()
153 */
154 public function show() {
155 // set active tab
156 UserMenu::getInstance()->setActiveMenuItem('wcf.user.menu.profile.avatar');
157
158 parent::show();
159 }
160 }