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