Add recent activity for trophies
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / form / SettingsForm.class.php
CommitLineData
320f4a6d
MW
1<?php
2namespace wcf\form;
7a23a706
MS
3use wcf\data\language\Language;
4use wcf\data\style\Style;
a83d788a
JR
5use wcf\data\trophy\Trophy;
6use wcf\data\trophy\TrophyCache;
320f4a6d 7use wcf\data\user\option\category\UserOptionCategory;
a83d788a 8use wcf\data\user\trophy\UserTrophyList;
320f4a6d 9use wcf\data\user\UserAction;
a83d788a
JR
10use wcf\data\user\UserProfile;
11use wcf\data\user\UserProfileAction;
320f4a6d
MW
12use wcf\system\exception\IllegalLinkException;
13use wcf\system\exception\UserInputException;
14use wcf\system\language\LanguageFactory;
15use wcf\system\menu\user\UserMenu;
16use wcf\system\option\user\UserOptionHandler;
17use wcf\system\user\storage\UserStorageHandler;
18use wcf\system\style\StyleHandler;
19use wcf\system\WCF;
20use wcf\util\ArrayUtil;
21
22/**
23 * Shows the dynamic options edit form.
24 *
25 * @author Alexander Ebert
cea1798f 26 * @copyright 2001-2017 WoltLab GmbH
320f4a6d 27 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
e71525e4 28 * @package WoltLabSuite\Core\Form
320f4a6d
MW
29 */
30class SettingsForm extends AbstractForm {
320f4a6d 31 /**
0fcfe5f6 32 * @inheritDoc
320f4a6d
MW
33 */
34 public $loginRequired = true;
35
36 /**
37 * user option handler
4e25add7 38 * @var UserOptionHandler
320f4a6d
MW
39 */
40 public $optionHandler = null;
41
42 /**
0fcfe5f6 43 * @inheritDoc
320f4a6d 44 */
058cbd6a 45 public $errorType = [];
320f4a6d
MW
46
47 /**
48 * option category
49 * @var string
50 */
51 public $category = 'general';
52
53 /**
54 * list of available content languages
7a23a706 55 * @var Language[]
320f4a6d 56 */
058cbd6a 57 public $availableContentLanguages = [];
320f4a6d
MW
58
59 /**
60 * list of available languages
7a23a706 61 * @var Language[]
320f4a6d 62 */
058cbd6a 63 public $availableLanguages = [];
320f4a6d
MW
64
65 /**
66 * list of available styles
7a23a706 67 * @var Style[]
320f4a6d 68 */
058cbd6a 69 public $availableStyles = [];
320f4a6d 70
a83d788a
JR
71 /**
72 * list of available trophies
73 * @var Trophy[]
74 */
75 public $availableTrophies = [];
76
320f4a6d
MW
77 /**
78 * list of content language ids
7a23a706 79 * @var integer[]
320f4a6d 80 */
058cbd6a 81 public $contentLanguageIDs = [];
320f4a6d
MW
82
83 /**
84 * language id
85 * @var integer
86 */
87 public $languageID = 0;
88
89 /**
90 * style id
91 * @var integer
92 */
93 public $styleID = 0;
94
a83d788a
JR
95 /**
96 * special trophies
97 * @var integer[]
98 */
99 public $specialTrophies = [];
100
320f4a6d 101 /**
0fcfe5f6 102 * @inheritDoc
320f4a6d
MW
103 */
104 public function readParameters() {
105 parent::readParameters();
106
107 if (!empty($_REQUEST['category'])) {
108 $this->category = $_REQUEST['category'];
109
110 // validate category
111 if (UserOptionCategory::getCategoryByName('settings.'.$this->category) === null) {
112 throw new IllegalLinkException();
113 }
114 }
115
116 $this->optionHandler = new UserOptionHandler(false, '', 'settings.'.$this->category);
117 $this->optionHandler->setUser(WCF::getUser());
118
119 if ($this->category == 'general') {
120 $this->availableContentLanguages = LanguageFactory::getInstance()->getContentLanguages();
121 $this->availableLanguages = LanguageFactory::getInstance()->getLanguages();
122 $this->availableStyles = StyleHandler::getInstance()->getAvailableStyles();
a83d788a
JR
123
124 // read available trophies
125 $trophyIDs = array_unique(array_map(function ($userTrophy) {
126 return $userTrophy->trophyID;
127 }, UserTrophyList::getUserTrophies([WCF::getUser()->userID])[WCF::getUser()->userID]));
128
129 $this->availableTrophies = TrophyCache::getInstance()->getTrophiesByID($trophyIDs);
320f4a6d
MW
130 }
131 }
132
133 /**
0fcfe5f6 134 * @inheritDoc
320f4a6d
MW
135 */
136 public function readFormParameters() {
137 parent::readFormParameters();
138
139 $this->optionHandler->readUserInput($_POST);
140
141 // static options
142 if ($this->category == 'general') {
143 if (isset($_POST['contentLanguageIDs']) && is_array($_POST['contentLanguageIDs'])) $this->contentLanguageIDs = ArrayUtil::toIntegerArray($_POST['contentLanguageIDs']);
144 if (isset($_POST['languageID'])) $this->languageID = intval($_POST['languageID']);
145 if (isset($_POST['styleID'])) $this->styleID = intval($_POST['styleID']);
a83d788a 146 if (isset($_POST['specialTrophies'])) $this->specialTrophies = ArrayUtil::toIntegerArray($_POST['specialTrophies']);
320f4a6d
MW
147 }
148 }
149
150 /**
0fcfe5f6 151 * @inheritDoc
320f4a6d
MW
152 */
153 public function validate() {
154 parent::validate();
155
156 // dynamic options
157 $optionErrors = $this->optionHandler->validate();
158 if (!empty($optionErrors)) {
159 $this->errorType = $optionErrors;
160 throw new UserInputException('options', $this->errorType);
161 }
162
163 // static options
164 if ($this->category == 'general') {
165 // validate language id
166 if (!isset($this->availableLanguages[$this->languageID])) {
167 $this->languageID = LanguageFactory::getInstance()->getDefaultLanguageID();
168 }
169
170 // validate content language ids
171 foreach ($this->contentLanguageIDs as $key => $languageID) {
172 if (!isset($this->availableContentLanguages[$languageID])) {
173 unset($this->contentLanguageIDs[$key]);
174 }
175 }
176
177 if (empty($this->contentLanguageIDs) && isset($this->availableContentLanguages[$this->languageID])) {
178 $this->contentLanguageIDs[] = $this->languageID;
179 }
180
181 // validate style id
182 if (!isset($this->availableStyles[$this->styleID])) {
183 $this->styleID = 0;
184 }
a83d788a
JR
185
186 // validate special trophies
187 if (count($this->specialTrophies) > WCF::getSession()->getPermission('user.profile.trophy.maxUserSpecialTrophies')) {
188 throw new UserInputException('specialTrophies', 'tooMuch');
189 }
190
191 foreach ($this->specialTrophies as $trophyID) {
192 if (!in_array($trophyID, array_map(function ($trophy) {
193 return $trophy->trophyID;
194 }, $this->availableTrophies))) {
c5684f00 195 throw new UserInputException('specialTrophies', 'invalid');
a83d788a
JR
196 }
197 }
320f4a6d
MW
198 }
199 }
200
201 /**
0fcfe5f6 202 * @inheritDoc
320f4a6d
MW
203 */
204 public function readData() {
205 parent::readData();
206
207 // default values
208 if (empty($_POST)) {
209 // static options
210 if ($this->category == 'general') {
211 $this->contentLanguageIDs = WCF::getUser()->getLanguageIDs();
6c82fd5c
MW
212 if (isset($this->availableLanguages[WCF::getUser()->languageID])) {
213 $this->languageID = WCF::getUser()->languageID;
214 }
320f4a6d 215 $this->styleID = WCF::getUser()->styleID;
a83d788a
JR
216
217 $this->specialTrophies = array_unique(array_map(function ($trophy) {
218 return $trophy->trophyID;
219 }, (new UserProfile(WCF::getUser()))->getSpecialTrophies()));
320f4a6d
MW
220 }
221 }
222 }
223
224 /**
0fcfe5f6 225 * @inheritDoc
320f4a6d
MW
226 */
227 public function save() {
228 parent::save();
229
230 $saveOptions = $this->optionHandler->save();
058cbd6a 231 $parameters = ['options' => $saveOptions];
320f4a6d
MW
232 // static options
233 if ($this->category == 'general') {
058cbd6a 234 $parameters['data'] = array_merge($this->additionalFields, [
320f4a6d
MW
235 'languageID' => $this->languageID,
236 'styleID' => $this->styleID
058cbd6a 237 ]);
320f4a6d
MW
238 $parameters['languageIDs'] = $this->contentLanguageIDs;
239 }
240
058cbd6a 241 $this->objectAction = new UserAction([WCF::getUser()], 'update', $parameters);
320f4a6d
MW
242 $this->objectAction->executeAction();
243
244 // static options
245 if ($this->category == 'general') {
246 // reset user language ids cache
058cbd6a 247 UserStorageHandler::getInstance()->reset([WCF::getUser()->userID], 'languageIDs');
a83d788a
JR
248
249 $userProfileAction = new UserProfileAction([WCF::getUser()->userID], 'updateSpecialTrophies', [
250 'trophyIDs' => $this->specialTrophies
251 ]);
252 $userProfileAction->executeAction();
320f4a6d
MW
253 }
254 $this->saved();
255
256 WCF::getTPL()->assign('success', true);
257 }
258
259 /**
0fcfe5f6 260 * @inheritDoc
320f4a6d
MW
261 */
262 public function assignVariables() {
263 parent::assignVariables();
264
058cbd6a 265 WCF::getTPL()->assign([
320f4a6d
MW
266 'optionTree' => $this->optionHandler->getOptionTree(),
267 'category' => $this->category
058cbd6a 268 ]);
320f4a6d
MW
269 // static options
270 if ($this->category == 'general') {
058cbd6a 271 WCF::getTPL()->assign([
320f4a6d
MW
272 'availableContentLanguages' => $this->availableContentLanguages,
273 'availableLanguages' => $this->availableLanguages,
274 'availableStyles' => $this->availableStyles,
a83d788a 275 'availableTrophies' => $this->availableTrophies,
320f4a6d
MW
276 'contentLanguageIDs' => $this->contentLanguageIDs,
277 'languageID' => $this->languageID,
a83d788a
JR
278 'styleID' => $this->styleID,
279 'specialTrophies' => $this->specialTrophies
058cbd6a 280 ]);
320f4a6d
MW
281 }
282 }
283
284 /**
0fcfe5f6 285 * @inheritDoc
320f4a6d
MW
286 */
287 public function show() {
288 // set active tab
289 UserMenu::getInstance()->setActiveMenuItem('wcf.user.option.category.settings.'.$this->category);
290
291 parent::show();
292 }
293}