9146db908d159768e5eb637c1f530d820e1a6316
[GitHub/WoltLab/WCF.git] /
1 <?php
2 declare(strict_types=1);
3 namespace wcf\system\user\activity\event;
4 use wcf\data\article\ViewableArticleList;
5 use wcf\data\comment\response\CommentResponseList;
6 use wcf\data\comment\CommentList;
7 use wcf\data\user\UserList;
8 use wcf\system\SingletonFactory;
9 use wcf\system\WCF;
10
11 /**
12 * User activity event implementation for responses to article comments.
13 *
14 * @author Marcel Werk
15 * @copyright 2001-2018 WoltLab GmbH
16 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
17 * @package WoltLabSuite\Core\System\User\Activity\Event
18 * @since 3.0
19 */
20 class ArticleCommentResponseUserActivityEvent extends SingletonFactory implements IUserActivityEvent {
21 /**
22 * @inheritDoc
23 */
24 public function prepare(array $events) {
25 $responseIDs = [];
26 foreach ($events as $event) {
27 $responseIDs[] = $event->objectID;
28 }
29
30 // fetch responses
31 $responseList = new CommentResponseList();
32 $responseList->setObjectIDs($responseIDs);
33 $responseList->readObjects();
34 $responses = $responseList->getObjects();
35
36 // fetch comments
37 $commentIDs = $comments = [];
38 foreach ($responses as $response) {
39 $commentIDs[] = $response->commentID;
40 }
41 if (!empty($commentIDs)) {
42 $commentList = new CommentList();
43 $commentList->setObjectIDs($commentIDs);
44 $commentList->readObjects();
45 $comments = $commentList->getObjects();
46 }
47
48 // fetch articles
49 $articleContentIDs = [];
50 foreach ($comments as $comment) {
51 $articleContentIDs[] = $comment->objectID;
52 }
53
54 $articles = $articleContentToArticle = [];
55 if (!empty($articleContentIDs)) {
56 $articleList = new ViewableArticleList();
57 $articleList->getConditionBuilder()->add("article.articleID IN (SELECT articleID FROM wcf".WCF_N."_article_content WHERE articleContentID IN (?))", [$articleContentIDs]);
58 $articleList->readObjects();
59 foreach ($articleList as $article) {
60 $articles[$article->articleID] = $article;
61
62 $articleContentToArticle[$article->getArticleContent()->articleContentID] = $article->articleID;
63 }
64 }
65
66 // fetch users
67 $userIDs = $users = [];
68 foreach ($comments as $comment) {
69 $userIDs[] = $comment->userID;
70 }
71 if (!empty($userIDs)) {
72 $userList = new UserList();
73 $userList->setObjectIDs($userIDs);
74 $userList->readObjects();
75 $users = $userList->getObjects();
76 }
77
78 // set message
79 foreach ($events as $event) {
80 if (isset($responses[$event->objectID])) {
81 $response = $responses[$event->objectID];
82 $comment = $comments[$response->commentID];
83 if (isset($articleContentToArticle[$comment->objectID]) && isset($users[$comment->userID])) {
84 $article = $articles[$articleContentToArticle[$comment->objectID]];
85
86 // check permissions
87 if (!$article->canRead()) {
88 continue;
89 }
90 $event->setIsAccessible();
91
92 // title
93 $text = WCF::getLanguage()->getDynamicVariable('wcf.article.recentActivity.articleCommentResponse', [
94 'commentAuthor' => $users[$comment->userID],
95 'commentID' => $comment->commentID,
96 'responseID' => $response->responseID,
97 'article' => $article
98 ]);
99 $event->setTitle($text);
100
101 // description
102 $event->setDescription($response->getExcerpt());
103 continue;
104 }
105 }
106
107 $event->setIsOrphaned();
108 }
109 }
110 }