c0c1c4e9616e929e7461896b21714ed086b8ccc2
[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\moderation\queue\report\IModerationQueueReportHandler;
9 use wcf\system\WCF;
10
11 /**
12 * User notification event for moderation queue commments.
13 *
14 * @author Matthias Schmidt
15 * @copyright 2001-2016 WoltLab GmbH
16 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
17 * @package com.woltlab.wcf
18 * @subpackage system.user.notification.event
19 * @category Community Framework
20 * @since 2.2
21 */
22 class ModerationQueueCommentResponseUserNotificationEvent extends AbstractSharedUserNotificationEvent {
23 /**
24 * language item prefix for the notification texts
25 * @var string
26 */
27 protected $languageItemPrefix = null;
28
29 /**
30 * moderation queue object the notifications (indirectly) belong to
31 * @var ViewableModerationQueue
32 */
33 protected $moderationQueue = null;
34
35 /**
36 * true if the moderation queue is already loaded
37 * @var boolean
38 */
39 protected $moderationQueueLoaded = false;
40
41 /**
42 * @inheritDoc
43 */
44 protected $stackable = true;
45
46 /**
47 * @inheritDoc
48 */
49 public function checkAccess() {
50 if (!WCF::getSession()->getPermission('mod.general.canUseModeration') || $this->getModerationQueue() === null) {
51 return false;
52 }
53
54 return $this->getModerationQueue()->canEdit();
55 }
56
57 /**
58 * @inheritDoc
59 */
60 public function getEmailMessage($notificationType = 'instant') {
61 $authors = $this->getAuthors();
62 if (count($authors) > 1) {
63 if (isset($authors[0])) {
64 unset($authors[0]);
65 }
66 $count = count($authors);
67
68 return $this->getLanguage()->getDynamicVariable($this->getLanguageItemPrefix().'.commentResponse.mail.stacked', [
69 'author' => $this->author,
70 'authors' => array_values($authors),
71 'count' => $count,
72 'notificationType' => $notificationType,
73 'others' => $count - 1,
74 'moderationQueue' => $this->getModerationQueue(),
75 'response' => $this->userNotificationObject
76 ]);
77 }
78
79 $comment = CommentRuntimeCache::getInstance()->getObject($this->userNotificationObject->commentID);
80 if ($comment->userID) {
81 $commentAuthor = UserProfileRuntimeCache::getInstance()->getObject($comment->userID);
82 }
83 else {
84 $commentAuthor = UserProfile::getGuestUserProfile($comment->username);
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 = CommentRuntimeCache::getInstance()->getObject($this->userNotificationObject->commentID);
130 if ($comment->userID) {
131 $commentAuthor = UserProfileRuntimeCache::getInstance()->getObject($comment->userID);
132 }
133 else {
134 $commentAuthor = UserProfile::getGuestUserProfile($comment->username);
135 }
136
137 return $this->getLanguage()->getDynamicVariable($this->getLanguageItemPrefix().'.commentResponse.message', [
138 'author' => $this->author,
139 'commentAuthor' => $commentAuthor,
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->userNotificationObject->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->userNotificationObject->commentID);
196 UserProfileRuntimeCache::getInstance()->cacheObjectID($this->additionalData['userID']);
197 }
198 }