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