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