Merge branch '2.0'
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / form / AvatarEditForm.class.php
CommitLineData
320f4a6d
MW
1<?php
2namespace wcf\form;
3use wcf\data\user\avatar\Gravatar;
4use wcf\data\user\avatar\UserAvatarAction;
e94d7556 5use wcf\data\user\UserAction;
320f4a6d
MW
6use wcf\system\exception\PermissionDeniedException;
7use wcf\system\exception\UserInputException;
8use wcf\system\menu\user\UserMenu;
9use wcf\system\WCF;
10
11/**
12 * Shows the avatar edit form.
13 *
14 * @author Marcel Werk
ca4ba303 15 * @copyright 2001-2014 WoltLab GmbH
320f4a6d 16 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
f4f05aa5 17 * @package com.woltlab.wcf
320f4a6d
MW
18 * @subpackage form
19 * @category Community Framework
20 */
77329f86 21class AvatarEditForm extends AbstractForm {
320f4a6d 22 /**
0ad90fc3 23 * @see \wcf\page\AbstractPage::$enableTracking
320f4a6d
MW
24 */
25 public $enableTracking = true;
26
27 /**
0ad90fc3 28 * @see \wcf\page\AbstractPage::$loginRequired
320f4a6d
MW
29 */
30 public $loginRequired = true;
31
32 /**
0ad90fc3 33 * @see \wcf\page\AbstractPage::$templateName
320f4a6d
MW
34 */
35 public $templateName = 'avatarEdit';
36
37 /**
38 * avatar type
39 * @var string
40 */
41 public $avatarType = 'none';
42
43 /**
0ad90fc3 44 * @see \wcf\form\IForm::readFormParameters()
320f4a6d
MW
45 */
46 public function readFormParameters() {
47 parent::readFormParameters();
48
49 if (isset($_POST['avatarType'])) $this->avatarType = $_POST['avatarType'];
50 }
51
52 /**
0ad90fc3 53 * @see \wcf\form\IForm::validate()
320f4a6d
MW
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 /**
0ad90fc3 84 * @see \wcf\form\IForm::save()
320f4a6d
MW
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':
e94d7556 100 $data = array(
320f4a6d
MW
101 'avatarID' => null,
102 'enableGravatar' => 0
e94d7556 103 );
320f4a6d
MW
104 break;
105
106 case 'custom':
e94d7556 107 $data = array(
320f4a6d 108 'enableGravatar' => 0
e94d7556 109 );
320f4a6d
MW
110 break;
111
112 case 'gravatar':
e94d7556 113 $data = array(
320f4a6d
MW
114 'avatarID' => null,
115 'enableGravatar' => 1
e94d7556 116 );
320f4a6d
MW
117 break;
118 }
e94d7556
TD
119 $this->objectAction = new UserAction(array(WCF::getUser()), 'update', array(
120 'data' => array_merge($this->additionalFields, $data)
121 ));
122 $this->objectAction->executeAction();
320f4a6d 123
5242f6d5
MW
124 // reset gravatar cache
125 if ($this->avatarType == 'gravatar') {
126 $pattern = WCF_DIR . sprintf(Gravatar::GRAVATAR_CACHE_LOCATION, md5(mb_strtolower(WCF::getUser()->email)), '*');
127 $files = glob($pattern);
128 if (!empty($files)) {
129 foreach ($files as $file) {
130 @unlink($file);
131 }
132 }
133 }
134
320f4a6d
MW
135 $this->saved();
136 WCF::getTPL()->assign('success', true);
137 }
138
139 /**
0ad90fc3 140 * @see \wcf\page\IPage::readData()
320f4a6d
MW
141 */
142 public function readData() {
143 parent::readData();
144
145 if (empty($_POST)) {
146 if (WCF::getUser()->avatarID) $this->avatarType = 'custom';
147 else if (MODULE_GRAVATAR && WCF::getUser()->enableGravatar) $this->avatarType = 'gravatar';
148 }
149 }
150
151 /**
0ad90fc3 152 * @see \wcf\page\IPage::assignVariables()
320f4a6d
MW
153 */
154 public function assignVariables() {
155 parent::assignVariables();
156
157 WCF::getTPL()->assign(array(
158 'avatarType' => $this->avatarType
159 ));
160 }
161
162 /**
0ad90fc3 163 * @see \wcf\page\IPage::show()
320f4a6d
MW
164 */
165 public function show() {
166 // set active tab
167 UserMenu::getInstance()->setActiveMenuItem('wcf.user.menu.profile.avatar');
168
169 parent::show();
170 }
171}