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