bcad919b4591529237ee2003373412604a992560
[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\email\Email;
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 comments.
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 WoltLabSuite\Core\System\User\Notification\Event
19 * @since 3.0
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 $comment = CommentRuntimeCache::getInstance()->getObject($this->userNotificationObject->commentID);
61 if ($comment->userID) {
62 $commentAuthor = UserProfileRuntimeCache::getInstance()->getObject($comment->userID);
63 }
64 else {
65 $commentAuthor = UserProfile::getGuestUserProfile($comment->username);
66 }
67
68 $messageID = '<com.woltlab.wcf.moderation.queue.notification/'.$comment->commentID.'@'.Email::getHost().'>';
69
70 return [
71 'template' => 'email_notification_moderationQueueCommentResponse',
72 'application' => 'wcf',
73 'in-reply-to' => [$messageID],
74 'references' => [
75 '<com.woltlab.wcf.moderation.queue/'.$this->getModerationQueue()->queueID.'@'.Email::getHost().'>',
76 $messageID
77 ],
78 'variables' => [
79 'moderationQueue' => $this->getModerationQueue(),
80 'commentAuthor' => $commentAuthor,
81 'languageItemPrefix' => $this->getLanguageItemPrefix()
82 ]
83 ];
84 }
85
86 /**
87 * @inheritDoc
88 */
89 public function getEventHash() {
90 return sha1($this->eventID . '-' . $this->getModerationQueue()->queueID);
91 }
92
93 /**
94 * @inheritDoc
95 */
96 public function getLink() {
97 return $this->getModerationQueue()->getLink() . '#comments';
98 }
99
100 /**
101 * @inheritDoc
102 */
103 public function getMessage() {
104 $authors = $this->getAuthors();
105 if (count($authors) > 1) {
106 if (isset($authors[0])) {
107 unset($authors[0]);
108 }
109 $count = count($authors);
110
111 return $this->getLanguage()->getDynamicVariable($this->getLanguageItemPrefix().'.commentResponse.message.stacked', [
112 'authors' => array_values($authors),
113 'count' => $count,
114 'others' => $count - 1,
115 'moderationQueue' => $this->getModerationQueue()
116 ]);
117 }
118
119 $comment = CommentRuntimeCache::getInstance()->getObject($this->userNotificationObject->commentID);
120 if ($comment->userID) {
121 $commentAuthor = UserProfileRuntimeCache::getInstance()->getObject($comment->userID);
122 }
123 else {
124 $commentAuthor = UserProfile::getGuestUserProfile($comment->username);
125 }
126
127 return $this->getLanguage()->getDynamicVariable($this->getLanguageItemPrefix().'.commentResponse.message', [
128 'author' => $this->author,
129 'commentAuthor' => $commentAuthor,
130 'moderationQueue' => $this->getModerationQueue()
131 ]);
132 }
133
134 /**
135 * Returns the moderation queue object the responded to comment belongs to.
136 * Returns null if the active user has no access to the moderation queue.
137 *
138 * @return ViewableModerationQueue
139 */
140 public function getModerationQueue() {
141 if (!$this->moderationQueueLoaded) {
142 $comment = CommentRuntimeCache::getInstance()->getObject($this->userNotificationObject->commentID);
143
144 $this->moderationQueue = ViewableModerationQueue::getViewableModerationQueue($comment->objectID);
145 $this->moderationQueueLoaded = true;
146 }
147
148 return $this->moderationQueue;
149 }
150
151 /**
152 * Returns the language item prefix for the notification texts.
153 *
154 * @return string
155 */
156 public function getLanguageItemPrefix() {
157 if ($this->languageItemPrefix === null) {
158 /** @var IModerationQueueReportHandler $moderationHandler */
159 $moderationHandler = ObjectTypeCache::getInstance()->getObjectType($this->getModerationQueue()->objectTypeID)->getProcessor();
160 $this->languageItemPrefix = $moderationHandler->getCommentNotificationLanguageItemPrefix();
161 }
162
163 return $this->languageItemPrefix;
164 }
165
166 /**
167 * @inheritDoc
168 */
169 public function getTitle() {
170 $count = count($this->getAuthors());
171 if ($count > 1) {
172 return $this->getLanguage()->getDynamicVariable($this->getLanguageItemPrefix().'.commentResponse.title.stacked', [
173 'count' => $count,
174 'timesTriggered' => $this->notification->timesTriggered
175 ]);
176 }
177
178 return $this->getLanguage()->get($this->getLanguageItemPrefix().'.commentResponse.title');
179 }
180
181 /**
182 * @inheritDoc
183 */
184 protected function prepare() {
185 CommentRuntimeCache::getInstance()->cacheObjectID($this->userNotificationObject->commentID);
186 UserProfileRuntimeCache::getInstance()->cacheObjectID($this->additionalData['userID']);
187 }
188 }