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