8684dee622a655bf075ff3aada716c54e7c5c444
[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-2017 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() . '#comments';
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 'count' => $count,
121 'others' => $count - 1,
122 'moderationQueue' => $this->getModerationQueue()
123 ]);
124 }
125
126 $comment = CommentRuntimeCache::getInstance()->getObject($this->getUserNotificationObject()->commentID);
127 if ($comment->userID) {
128 $commentAuthor = UserProfileRuntimeCache::getInstance()->getObject($comment->userID);
129 }
130 else {
131 $commentAuthor = UserProfile::getGuestUserProfile($comment->username);
132 }
133
134 return $this->getLanguage()->getDynamicVariable($this->getLanguageItemPrefix().'.commentResponse.message', [
135 'author' => $this->author,
136 'commentAuthor' => $commentAuthor,
137 'moderationQueue' => $this->getModerationQueue()
138 ]);
139 }
140
141 /**
142 * Returns the moderation queue object the responded to comment belongs to.
143 * Returns null if the active user has no access to the moderation queue.
144 *
145 * @return ViewableModerationQueue
146 */
147 public function getModerationQueue() {
148 if (!$this->moderationQueueLoaded) {
149 $comment = CommentRuntimeCache::getInstance()->getObject($this->getUserNotificationObject()->commentID);
150
151 $this->moderationQueue = ViewableModerationQueue::getViewableModerationQueue($comment->objectID);
152 $this->moderationQueueLoaded = true;
153 }
154
155 return $this->moderationQueue;
156 }
157
158 /**
159 * Returns the language item prefix for the notification texts.
160 *
161 * @return string
162 */
163 public function getLanguageItemPrefix() {
164 if ($this->languageItemPrefix === null) {
165 /** @var IModerationQueueReportHandler $moderationHandler */
166 $moderationHandler = ObjectTypeCache::getInstance()->getObjectType($this->getModerationQueue()->objectTypeID)->getProcessor();
167 $this->languageItemPrefix = $moderationHandler->getCommentNotificationLanguageItemPrefix();
168 }
169
170 return $this->languageItemPrefix;
171 }
172
173 /**
174 * @inheritDoc
175 */
176 public function getTitle() {
177 $count = count($this->getAuthors());
178 if ($count > 1) {
179 return $this->getLanguage()->getDynamicVariable($this->getLanguageItemPrefix().'.commentResponse.title.stacked', [
180 'count' => $count,
181 'timesTriggered' => $this->notification->timesTriggered
182 ]);
183 }
184
185 return $this->getLanguage()->get($this->getLanguageItemPrefix().'.commentResponse.title');
186 }
187
188 /**
189 * @inheritDoc
190 */
191 protected function prepare() {
192 CommentRuntimeCache::getInstance()->cacheObjectID($this->getUserNotificationObject()->commentID);
193 UserProfileRuntimeCache::getInstance()->cacheObjectID($this->additionalData['userID']);
194 }
195
196 /**
197 * @inheritDoc
198 * @since 3.1
199 */
200 public static function canBeTriggeredByGuests() {
201 return false;
202 }
203
204 /**
205 * @inheritDoc
206 * @since 3.1
207 */
208 protected static function getTestCommentObjectData(UserProfile $recipient, UserProfile $author) {
209 return [
210 'objectID' => self::getTestUserModerationQueueEntry($author, $recipient)->queueID,
211 'objectTypeID' => CommentHandler::getInstance()->getObjectTypeID('com.woltlab.wcf.moderation.queue')
212 ];
213 }
214 }