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