28c7894cc31344ddd973ef702ac8c384c9dbd5f3
[GitHub/WoltLab/WCF.git] /
1 <?php
2 declare(strict_types=1);
3 namespace wcf\system\user\notification\event;
4 use wcf\data\moderation\queue\ViewableModerationQueue;
5 use wcf\data\object\type\ObjectTypeCache;
6 use wcf\data\user\UserProfile;
7 use wcf\system\cache\runtime\CommentRuntimeCache;
8 use wcf\system\cache\runtime\UserProfileRuntimeCache;
9 use wcf\system\comment\CommentHandler;
10 use wcf\system\email\Email;
11 use wcf\system\moderation\queue\report\IModerationQueueReportHandler;
12 use wcf\system\user\notification\object\CommentResponseUserNotificationObject;
13 use wcf\system\WCF;
14
15 /**
16 * User notification event for moderation queue comments.
17 *
18 * @author Matthias Schmidt
19 * @copyright 2001-2018 WoltLab GmbH
20 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
21 * @package WoltLabSuite\Core\System\User\Notification\Event
22 * @since 3.0
23 *
24 * @method CommentResponseUserNotificationObject getUserNotificationObject()
25 */
26 class ModerationQueueCommentResponseUserNotificationEvent extends AbstractSharedUserNotificationEvent implements ITestableUserNotificationEvent {
27 use TTestableCommentResponseUserNotificationEvent;
28 use TTestableModerationQueueUserNotificationEvent;
29
30 /**
31 * language item prefix for the notification texts
32 * @var string
33 */
34 protected $languageItemPrefix = null;
35
36 /**
37 * moderation queue object the notifications (indirectly) belong to
38 * @var ViewableModerationQueue
39 */
40 protected $moderationQueue = null;
41
42 /**
43 * true if the moderation queue is already loaded
44 * @var boolean
45 */
46 protected $moderationQueueLoaded = false;
47
48 /**
49 * @inheritDoc
50 */
51 protected $stackable = true;
52
53 /**
54 * @inheritDoc
55 */
56 public function checkAccess() {
57 if (!WCF::getSession()->getPermission('mod.general.canUseModeration') || $this->getModerationQueue() === null) {
58 return false;
59 }
60
61 return $this->getModerationQueue()->canEdit();
62 }
63
64 /**
65 * @inheritDoc
66 */
67 public function getEmailMessage($notificationType = 'instant') {
68 $comment = CommentRuntimeCache::getInstance()->getObject($this->getUserNotificationObject()->commentID);
69 if ($comment->userID) {
70 $commentAuthor = UserProfileRuntimeCache::getInstance()->getObject($comment->userID);
71 }
72 else {
73 $commentAuthor = UserProfile::getGuestUserProfile($comment->username);
74 }
75
76 $messageID = '<com.woltlab.wcf.moderation.queue.notification/'.$comment->commentID.'@'.Email::getHost().'>';
77
78 return [
79 'template' => 'email_notification_moderationQueueCommentResponse',
80 'application' => 'wcf',
81 'in-reply-to' => [$messageID],
82 'references' => [
83 '<com.woltlab.wcf.moderation.queue/'.$this->getModerationQueue()->queueID.'@'.Email::getHost().'>',
84 $messageID
85 ],
86 'variables' => [
87 'moderationQueue' => $this->getModerationQueue(),
88 'commentAuthor' => $commentAuthor,
89 'languageItemPrefix' => $this->getLanguageItemPrefix()
90 ]
91 ];
92 }
93
94 /**
95 * @inheritDoc
96 */
97 public function getEventHash() {
98 return sha1($this->eventID . '-' . $this->getModerationQueue()->queueID);
99 }
100
101 /**
102 * @inheritDoc
103 */
104 public function getLink() {
105 return $this->getModerationQueue()->getLink() . '#comment' . $this->getUserNotificationObject()->commentID;
106 }
107
108 /**
109 * @inheritDoc
110 */
111 public function getMessage() {
112 $authors = $this->getAuthors();
113 if (count($authors) > 1) {
114 if (isset($authors[0])) {
115 unset($authors[0]);
116 }
117 $count = count($authors);
118
119 return $this->getLanguage()->getDynamicVariable($this->getLanguageItemPrefix().'.commentResponse.message.stacked', [
120 'authors' => array_values($authors),
121 'commentID' => $this->getUserNotificationObject()->commentID,
122 'count' => $count,
123 'others' => $count - 1,
124 'moderationQueue' => $this->getModerationQueue()
125 ]);
126 }
127
128 $comment = CommentRuntimeCache::getInstance()->getObject($this->getUserNotificationObject()->commentID);
129 if ($comment->userID) {
130 $commentAuthor = UserProfileRuntimeCache::getInstance()->getObject($comment->userID);
131 }
132 else {
133 $commentAuthor = UserProfile::getGuestUserProfile($comment->username);
134 }
135
136 return $this->getLanguage()->getDynamicVariable($this->getLanguageItemPrefix().'.commentResponse.message', [
137 'author' => $this->author,
138 'commentAuthor' => $commentAuthor,
139 'commentID' => $this->getUserNotificationObject()->commentID,
140 'responseID' => $this->getUserNotificationObject()->responseID,
141 'moderationQueue' => $this->getModerationQueue()
142 ]);
143 }
144
145 /**
146 * Returns the moderation queue object the responded to comment belongs to.
147 * Returns null if the active user has no access to the moderation queue.
148 *
149 * @return ViewableModerationQueue
150 */
151 public function getModerationQueue() {
152 if (!$this->moderationQueueLoaded) {
153 $comment = CommentRuntimeCache::getInstance()->getObject($this->getUserNotificationObject()->commentID);
154
155 $this->moderationQueue = ViewableModerationQueue::getViewableModerationQueue($comment->objectID);
156 $this->moderationQueueLoaded = true;
157 }
158
159 return $this->moderationQueue;
160 }
161
162 /**
163 * Returns the language item prefix for the notification texts.
164 *
165 * @return string
166 */
167 public function getLanguageItemPrefix() {
168 if ($this->languageItemPrefix === null) {
169 /** @var IModerationQueueReportHandler $moderationHandler */
170 $moderationHandler = ObjectTypeCache::getInstance()->getObjectType($this->getModerationQueue()->objectTypeID)->getProcessor();
171 $this->languageItemPrefix = $moderationHandler->getCommentNotificationLanguageItemPrefix();
172 }
173
174 return $this->languageItemPrefix;
175 }
176
177 /**
178 * @inheritDoc
179 */
180 public function getTitle() {
181 $count = count($this->getAuthors());
182 if ($count > 1) {
183 return $this->getLanguage()->getDynamicVariable($this->getLanguageItemPrefix().'.commentResponse.title.stacked', [
184 'count' => $count,
185 'timesTriggered' => $this->notification->timesTriggered
186 ]);
187 }
188
189 return $this->getLanguage()->get($this->getLanguageItemPrefix().'.commentResponse.title');
190 }
191
192 /**
193 * @inheritDoc
194 */
195 protected function prepare() {
196 CommentRuntimeCache::getInstance()->cacheObjectID($this->getUserNotificationObject()->commentID);
197 UserProfileRuntimeCache::getInstance()->cacheObjectID($this->additionalData['userID']);
198 }
199
200 /**
201 * @inheritDoc
202 * @since 3.1
203 */
204 public static function canBeTriggeredByGuests() {
205 return false;
206 }
207
208 /**
209 * @inheritDoc
210 * @since 3.1
211 */
212 protected static function getTestCommentObjectData(UserProfile $recipient, UserProfile $author) {
213 return [
214 'objectID' => self::getTestUserModerationQueueEntry($author, $recipient)->queueID,
215 'objectTypeID' => CommentHandler::getInstance()->getObjectTypeID('com.woltlab.wcf.moderation.queue')
216 ];
217 }
218 }