3c328bf9d9b0dffbf44be1ae9a79679c35f0c686
[GitHub/WoltLab/WCF.git] /
1 <?php
2
3 namespace wcf\system\user\notification\event;
4
5 use wcf\data\page\PageCache;
6 use wcf\data\user\UserProfile;
7 use wcf\system\cache\runtime\CommentRuntimeCache;
8 use wcf\system\cache\runtime\UserProfileRuntimeCache;
9 use wcf\system\comment\CommentHandler;
10 use wcf\system\email\Email;
11 use wcf\system\user\notification\object\CommentResponseUserNotificationObject;
12
13 /**
14 * User notification event for page comments.
15 *
16 * @author Joshua Ruesweg
17 * @copyright 2001-2019 WoltLab GmbH
18 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
19 * @since 5.2
20 *
21 * @method CommentResponseUserNotificationObject getUserNotificationObject()
22 */
23 class PageCommentResponseUserNotificationEvent extends AbstractSharedUserNotificationEvent implements
24 ITestableUserNotificationEvent
25 {
26 use TTestableCommentResponseUserNotificationEvent;
27 use TTestablePageUserNotificationEvent;
28
29 /**
30 * @inheritDoc
31 */
32 protected $stackable = true;
33
34 /**
35 * @inheritDoc
36 */
37 protected function prepare()
38 {
39 CommentRuntimeCache::getInstance()->cacheObjectID($this->getUserNotificationObject()->commentID);
40 }
41
42 /**
43 * @inheritDoc
44 */
45 public function getTitle(): string
46 {
47 $count = \count($this->getAuthors());
48 if ($count > 1) {
49 return $this->getLanguage()->getDynamicVariable(
50 'wcf.user.notification.pageComment.response.title.stacked',
51 [
52 'count' => $count,
53 'timesTriggered' => $this->notification->timesTriggered,
54 ]
55 );
56 }
57
58 return $this->getLanguage()->get('wcf.user.notification.pageComment.response.title');
59 }
60
61 /**
62 * @inheritDoc
63 */
64 public function getMessage()
65 {
66 $authors = $this->getAuthors();
67 if (\count($authors) > 1) {
68 if (isset($authors[0])) {
69 unset($authors[0]);
70 }
71 $count = \count($authors);
72
73 return $this->getLanguage()->getDynamicVariable(
74 'wcf.user.notification.pageComment.response.message.stacked',
75 [
76 'author' => $this->author,
77 'authors' => \array_values($authors),
78 'commentID' => $this->getUserNotificationObject()->commentID,
79 'page' => PageCache::getInstance()->getPage($this->additionalData['objectID']),
80 'count' => $count,
81 'others' => $count - 1,
82 'guestTimesTriggered' => $this->notification->guestTimesTriggered,
83 'commentAuthor' => $this->getCommentAuthorProfile(),
84 ]
85 );
86 }
87
88 return $this->getLanguage()->getDynamicVariable('wcf.user.notification.pageComment.response.message', [
89 'author' => $this->author,
90 'commentID' => $this->getUserNotificationObject()->commentID,
91 'page' => PageCache::getInstance()->getPage($this->additionalData['objectID']),
92 'responseID' => $this->getUserNotificationObject()->responseID,
93 'commentAuthor' => $this->getCommentAuthorProfile(),
94 ]);
95 }
96
97 /**
98 * @inheritDoc
99 */
100 public function getEmailMessage($notificationType = 'instant')
101 {
102 $messageID = '<com.woltlab.wcf.user.pageComment.notification/' . $this->getUserNotificationObject()->commentID . '@' . Email::getHost() . '>';
103
104 return [
105 'template' => 'email_notification_commentResponse',
106 'in-reply-to' => [$messageID],
107 'references' => [$messageID],
108 'application' => 'wcf',
109 'variables' => [
110 'commentID' => $this->getUserNotificationObject()->commentID,
111 'page' => PageCache::getInstance()->getPage($this->additionalData['objectID']),
112 'languageVariablePrefix' => 'wcf.user.notification.pageComment.response',
113 'responseID' => $this->getUserNotificationObject()->responseID,
114 'commentAuthor' => $this->getCommentAuthorProfile(),
115 ],
116 ];
117 }
118
119 /**
120 * Returns the comment authors profile.
121 *
122 * @return UserProfile
123 */
124 private function getCommentAuthorProfile()
125 {
126 $comment = CommentRuntimeCache::getInstance()->getObject($this->getUserNotificationObject()->commentID);
127
128 if ($comment->userID) {
129 return UserProfileRuntimeCache::getInstance()->getObject($comment->userID);
130 } else {
131 return UserProfile::getGuestUserProfile($comment->username);
132 }
133 }
134
135 /**
136 * @inheritDoc
137 */
138 public function getLink(): string
139 {
140 return PageCache::getInstance()->getPage($this->additionalData['objectID'])->getLink() . '#comment' . $this->getUserNotificationObject()->commentID;
141 }
142
143 /**
144 * @inheritDoc
145 */
146 public function getEventHash()
147 {
148 return \sha1($this->eventID . '-' . $this->notification->objectID);
149 }
150
151 /**
152 * @inheritDoc
153 */
154 protected static function getTestCommentObjectData(UserProfile $recipient, UserProfile $author)
155 {
156 return [
157 'objectID' => self::getTestPage()->pageID,
158 'objectTypeID' => CommentHandler::getInstance()->getObjectTypeID('com.woltlab.wcf.page'),
159 ];
160 }
161 }