Apply PSR-12 code style (#3886)
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / system / user / notification / object / type / TMultiRecipientModerationQueueCommentUserNotificationObjectType.class.php
1 <?php
2
3 namespace wcf\system\user\notification\object\type;
4
5 use wcf\data\comment\Comment;
6 use wcf\data\user\UserProfile;
7 use wcf\system\cache\runtime\UserProfileRuntimeCache;
8 use wcf\system\comment\CommentHandler;
9 use wcf\system\database\util\PreparedStatementConditionBuilder;
10 use wcf\system\user\storage\UserStorageHandler;
11 use wcf\system\WCF;
12
13 /**
14 * Implements IMultiRecipientCommentUserNotificationObjectType::getRecipientIDs()
15 * for moderation queue comment user notification object types.
16 *
17 * @author Mathias Schmidt
18 * @copyright 2001-2019 WoltLab GmbH
19 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
20 * @package WoltLabSuite\Core\System\User\Notification\Object\Type
21 * @since 3.0
22 */
23 trait TMultiRecipientModerationQueueCommentUserNotificationObjectType
24 {
25 /**
26 * @inheritDoc
27 */
28 public function getRecipientIDs(Comment $comment)
29 {
30 $objectTypeID = CommentHandler::getInstance()->getObjectTypeID('com.woltlab.wcf.moderation.queue');
31 if ($comment->objectTypeID != $objectTypeID) {
32 return [];
33 }
34
35 // 1. fetch assigned user
36 // 2. fetch users who commented on the moderation queue entry
37 // 3. fetch users who responded to a comment on the moderation queue entry
38 $sql = "(
39 SELECT assignedUserID
40 FROM wcf" . WCF_N . "_moderation_queue
41 WHERE queueID = ?
42 AND assignedUserID IS NOT NULL
43 ) UNION (
44 SELECT DISTINCT userID
45 FROM wcf" . WCF_N . "_comment
46 WHERE objectID = ?
47 AND objectTypeID = ?
48 ) UNION (
49 SELECT DISTINCT comment_response.userID
50 FROM wcf" . WCF_N . "_comment_response comment_response
51 INNER JOIN wcf" . WCF_N . "_comment comment
52 ON (comment.commentID = comment_response.commentID)
53 WHERE comment.objectID = ?
54 AND comment.objectTypeID = ?
55 )";
56 $statement = WCF::getDB()->prepareStatement($sql);
57 $statement->execute([
58 $comment->objectID,
59 $comment->objectID,
60 $objectTypeID,
61 $comment->objectID,
62 $objectTypeID,
63 ]);
64 $recipientIDs = $statement->fetchAll(\PDO::FETCH_COLUMN);
65
66 // make sure that all users can (still) access the moderation queue entry
67 if (!empty($recipientIDs)) {
68 $conditionBuilder = new PreparedStatementConditionBuilder();
69 $conditionBuilder->add('userID IN (?)', [$recipientIDs]);
70 $conditionBuilder->add('queueID = ?', [$comment->objectID]);
71 $conditionBuilder->add('isAffected = ?', [1]);
72 $sql = "SELECT userID
73 FROM wcf" . WCF_N . "_moderation_queue_to_user
74 " . $conditionBuilder;
75 $statement = WCF::getDB()->prepareStatement($sql);
76 $statement->execute($conditionBuilder->getParameters());
77 $recipientIDs = $statement->fetchAll(\PDO::FETCH_COLUMN);
78
79 // make sure that all users (still) have permission to access moderation
80 if (!$recipientIDs) {
81 UserStorageHandler::getInstance()->loadStorage($recipientIDs);
82 $userProfiles = UserProfileRuntimeCache::getInstance()->getObjects($recipientIDs);
83 $recipientIDs = \array_keys(\array_filter($userProfiles, static function (UserProfile $userProfile) {
84 return $userProfile->getPermission('mod.general.canUseModeration');
85 }));
86 }
87 }
88
89 return $recipientIDs;
90 }
91 }