Add `type` to `wcf1_user_ignore`
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / data / user / ignore / UserIgnoreAction.class.php
CommitLineData
320f4a6d 1<?php
a9229942 2
320f4a6d 3namespace wcf\data\user\ignore;
a9229942
TD
4
5use wcf\data\AbstractDatabaseObjectAction;
013abaff
AE
6use wcf\data\user\follow\UserFollow;
7use wcf\data\user\follow\UserFollowEditor;
10bc76ec 8use wcf\data\user\User;
a8c5936e 9use wcf\system\cache\runtime\UserProfileRuntimeCache;
320f4a6d
MW
10use wcf\system\exception\IllegalLinkException;
11use wcf\system\exception\PermissionDeniedException;
12use wcf\system\exception\UserInputException;
10bc76ec
TD
13use wcf\system\form\builder\data\processor\CustomFormDataProcessor;
14use wcf\system\form\builder\DialogFormDocument;
15use wcf\system\form\builder\field\RadioButtonFormField;
16use wcf\system\form\builder\field\validation\FormFieldValidationError;
17use wcf\system\form\builder\field\validation\FormFieldValidator;
18use wcf\system\form\builder\IFormDocument;
320f4a6d
MW
19use wcf\system\user\storage\UserStorageHandler;
20use wcf\system\WCF;
21
22/**
23 * Executes ignored user-related actions.
a9229942
TD
24 *
25 * @author Alexander Ebert
26 * @copyright 2001-2019 WoltLab GmbH
27 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
28 * @package WoltLabSuite\Core\Data\User\Ignore
29 *
30 * @method UserIgnore create()
31 * @method UserIgnoreEditor[] getObjects()
32 * @method UserIgnoreEditor getSingleObject()
320f4a6d 33 */
a9229942
TD
34class UserIgnoreAction extends AbstractDatabaseObjectAction
35{
10bc76ec
TD
36 protected $form;
37
a9229942
TD
38 /**
39 * Validates the 'ignore' action.
40 */
41 public function validateIgnore()
42 {
43 $this->readInteger('userID', false, 'data');
44
45 $userProfile = UserProfileRuntimeCache::getInstance()->getObject($this->parameters['data']['userID']);
46 if ($userProfile === null || $userProfile->userID == WCF::getUser()->userID) {
47 throw new IllegalLinkException();
48 }
49
50 // check permissions
51 if ($userProfile->getPermission('user.profile.cannotBeIgnored')) {
52 throw new PermissionDeniedException();
53 }
10bc76ec
TD
54
55 $this->readInteger('type', true, 'data');
56
57 if (
58 $this->parameters['data']['type']
59 && !\in_array($this->parameters['data']['type'], [
60 UserIgnore::TYPE_BLOCK_DIRECT_CONTACT,
61 UserIgnore::TYPE_HIDE_MESSAGES,
62 ])
63 ) {
64 throw new UserInputException('type', 'invalid');
65 }
a9229942
TD
66 }
67
68 /**
69 * Ignores a user.
70 *
71 * @return array
72 */
73 public function ignore()
74 {
10bc76ec
TD
75 $ignore = new UserIgnoreEditor(UserIgnore::getIgnore($this->parameters['data']['userID']));
76 $type = $this->parameters['data']['type'] ?? UserIgnore::TYPE_BLOCK_DIRECT_CONTACT;
77
78 if ($ignore->ignoreID) {
79 $ignore->update([
80 'type' => $type,
81 'time' => TIME_NOW,
82 ]);
83 } else {
84 $ignore = UserIgnoreEditor::createOrIgnore([
85 'ignoreUserID' => $this->parameters['data']['userID'],
86 'type' => $type,
87 'time' => TIME_NOW,
88 'userID' => WCF::getUser()->userID,
89 ]);
90 }
a9229942
TD
91
92 if ($ignore !== null) {
93 UserStorageHandler::getInstance()->reset([WCF::getUser()->userID], 'ignoredUserIDs');
94 UserStorageHandler::getInstance()->reset([$this->parameters['data']['userID']], 'ignoredByUserIDs');
95
96 // check if target user is following the current user
97 $sql = "SELECT *
98 FROM wcf" . WCF_N . "_user_follow
99 WHERE userID = ?
100 AND followUserID = ?";
101 $statement = WCF::getDB()->prepareStatement($sql);
102 $statement->execute([
103 $this->parameters['data']['userID'],
104 WCF::getUser()->userID,
105 ]);
106
107 $follow = $statement->fetchObject(UserFollow::class);
108
109 // remove follower
110 if ($follow !== null) {
111 $followEditor = new UserFollowEditor($follow);
112 $followEditor->delete();
113
114 // reset storage
115 UserStorageHandler::getInstance()->reset([WCF::getUser()->userID], 'followerUserIDs');
116 UserStorageHandler::getInstance()->reset([$this->parameters['data']['userID']], 'followingUserIDs');
117 }
118 }
119
120 return ['isIgnoredUser' => 1];
121 }
122
123 /**
124 * Validates the 'unignore' action.
125 */
126 public function validateUnignore()
127 {
128 $this->readInteger('userID', false, 'data');
129
130 $userProfile = UserProfileRuntimeCache::getInstance()->getObject($this->parameters['data']['userID']);
131 if ($userProfile === null) {
132 throw new IllegalLinkException();
133 }
134 }
135
136 /**
137 * Unignores a user.
138 *
139 * @return array
140 */
141 public function unignore()
142 {
143 $ignore = UserIgnore::getIgnore($this->parameters['data']['userID']);
144
145 if ($ignore->ignoreID) {
146 $ignoreEditor = new UserIgnoreEditor($ignore);
147 $ignoreEditor->delete();
148
149 UserStorageHandler::getInstance()->reset([WCF::getUser()->userID], 'ignoredUserIDs');
150 }
151
152 return ['isIgnoredUser' => 0];
153 }
154
10bc76ec
TD
155 public function validateGetDialog()
156 {
157 $this->readInteger('userID');
158
159 $userProfile = UserProfileRuntimeCache::getInstance()->getObject($this->parameters['userID']);
160 if ($userProfile === null || $userProfile->userID == WCF::getUser()->userID) {
161 throw new IllegalLinkException();
162 }
163
164 $ignore = UserIgnore::getIgnore($this->parameters['userID']);
165
166 // Check if the user is not yet ignored and cannot be ignored.
167 if (!$ignore && $userProfile->getPermission('user.profile.cannotBeIgnored')) {
168 throw new PermissionDeniedException();
169 }
170 }
171
172 public function getDialog()
173 {
174 $form = $this->getForm();
175
176 return [
177 'dialog' => $form->getHtml(),
178 'formId' => $form->getId(),
179 ];
180 }
181
182 public function validateSubmitDialog()
183 {
184 $this->validateGetDialog();
185
186 $this->readString('formId');
187
188 $this->getForm()->requestData($this->parameters['data'] ?? []);
189 $this->getForm()->readValues();
190 }
191
192 public function submitDialog()
193 {
194 $this->getForm()->validate();
195
196 if ($this->getForm()->hasValidationErrors()) {
197 return [
198 'dialog' => $this->getForm()->getHtml(),
199 'formId' => $this->getForm()->getId(),
200 ];
201 }
202
203 $formData = $this->getForm()->getData();
204
205 if ($formData['data']['type'] === UserIgnore::TYPE_NO_IGNORE) {
206 return (new self([], 'unignore', [
207 'data' => [
208 'userID' => $this->parameters['userID'],
209 ],
210 ]))->executeAction()['returnValues'];
211 } else {
212 return (new self([], 'ignore', [
213 'data' => [
214 'userID' => $this->parameters['userID'],
215 'type' => $formData['data']['type'],
216 ],
217 ]))->executeAction()['returnValues'];
218 }
219 }
220
221 protected function getForm(): IFormDocument
222 {
223 if ($this->form === null) {
224 $id = 'userIgnore';
225 $this->form = DialogFormDocument::create($id)
226 ->ajax()
227 ->prefix($id);
228
229 $ignore = UserIgnore::getIgnore($this->parameters['userID']);
230
231 $this->form->appendChildren([
232 RadioButtonFormField::create('type')
233 ->label(WCF::getLanguage()->get('wcf.user.ignore.type'))
234 ->options([
235 UserIgnore::TYPE_NO_IGNORE => WCF::getLanguage()
236 ->get('wcf.user.ignore.type.noIgnore'),
237 UserIgnore::TYPE_BLOCK_DIRECT_CONTACT => WCF::getLanguage()
238 ->get('wcf.user.ignore.type.blockDirectContact'),
239 UserIgnore::TYPE_HIDE_MESSAGES => WCF::getLanguage()
240 ->get('wcf.user.ignore.type.hideMessages'),
241 ])
242 ->value($ignore->type ?: 0)
243 ->addValidator(new FormFieldValidator('type', function (RadioButtonFormField $formField) {
244 $userProfile = UserProfileRuntimeCache::getInstance()->getObject($this->parameters['userID']);
245 if ($userProfile->getPermission('user.profile.cannotBeIgnored')) {
246 if ($formField->getValue() != UserIgnore::TYPE_NO_IGNORE) {
247 $formField->addValidationError(
248 new FormFieldValidationError(
249 'cannotBeIgnored',
250 'wcf.user.ignore.error.cannotBeIgnored'
251 )
252 );
253 }
254 }
255 })),
256 ]);
257
258 $this->form->getDataHandler()->addProcessor(
259 new CustomFormDataProcessor(
260 'type',
261 static function (IFormDocument $document, array $parameters) {
262 $parameters['data']['type'] = \intval($parameters['data']['type']);
263
264 return $parameters;
265 }
266 )
267 );
268
269 $this->form->build();
270 }
271
272 return $this->form;
273 }
274
a9229942
TD
275 /**
276 * @inheritDoc
277 */
278 public function validateDelete()
279 {
280 // read objects
281 if (empty($this->objects)) {
282 $this->readObjects();
283
284 if (empty($this->objects)) {
285 throw new UserInputException('objectIDs');
286 }
287 }
288
289 // validate ownership
290 foreach ($this->getObjects() as $ignore) {
291 if ($ignore->userID != WCF::getUser()->userID) {
292 throw new PermissionDeniedException();
293 }
294 }
295 }
296
297 /**
298 * @inheritDoc
299 */
300 public function delete()
301 {
302 $returnValues = parent::delete();
303
304 // reset storage
305 UserStorageHandler::getInstance()->reset([WCF::getUser()->userID], 'ignoredUserIDs');
306
307 return $returnValues;
308 }
320f4a6d 309}