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