abd4766d586793157711766b46a69f29a3cedd7c
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / data / comment / StructuredCommentList.class.php
1 <?php
2
3 namespace wcf\data\comment;
4
5 use wcf\data\comment\response\CommentResponseList;
6 use wcf\data\comment\response\StructuredCommentResponse;
7 use wcf\data\like\object\LikeObject;
8 use wcf\system\cache\runtime\UserProfileRuntimeCache;
9 use wcf\system\comment\manager\ICommentManager;
10 use wcf\system\reaction\ReactionHandler;
11
12 /**
13 * Provides a structured comment list fetching last responses for every comment.
14 *
15 * @author Alexander Ebert
16 * @copyright 2001-2019 WoltLab GmbH
17 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
18 * @package WoltLabSuite\Core\Data\Comment
19 *
20 * @method StructuredComment current()
21 * @method StructuredComment[] getObjects()
22 * @method StructuredComment|null search($objectID)
23 * @property StructuredComment[] $objects
24 */
25 class StructuredCommentList extends CommentList
26 {
27 /**
28 * comment manager object
29 * @var ICommentManager
30 */
31 public $commentManager;
32
33 /**
34 * minimum comment time
35 * @var int
36 */
37 public $minCommentTime = 0;
38
39 /**
40 * object type id
41 * @var int
42 */
43 public $objectTypeID = 0;
44
45 /**
46 * object id
47 * @var int
48 */
49 public $objectID = 0;
50
51 /**
52 * ids of the responses of the comments in the list
53 * @var int[]
54 */
55 public $responseIDs = [];
56
57 /**
58 * @inheritDoc
59 */
60 public $decoratorClassName = StructuredComment::class;
61
62 /**
63 * @inheritDoc
64 */
65 public $sqlLimit = 30;
66
67 /**
68 * @inheritDoc
69 */
70 public $sqlOrderBy = 'comment.time DESC';
71
72 /**
73 * enables/disables the loading of responses
74 * @var bool
75 */
76 public $responseLoading = true;
77
78 /**
79 * Creates a new structured comment list.
80 *
81 * @param ICommentManager $commentManager
82 * @param int $objectTypeID
83 * @param int $objectID
84 */
85 public function __construct(ICommentManager $commentManager, $objectTypeID, $objectID)
86 {
87 parent::__construct();
88
89 $this->commentManager = $commentManager;
90 $this->objectTypeID = $objectTypeID;
91 $this->objectID = $objectID;
92
93 $this->getConditionBuilder()->add("comment.objectTypeID = ?", [$objectTypeID]);
94 $this->getConditionBuilder()->add("comment.objectID = ?", [$objectID]);
95 $this->sqlLimit = $this->commentManager->getCommentsPerPage();
96
97 if (!$this->commentManager->canModerate($objectTypeID, $objectID)) {
98 $this->getConditionBuilder()->add('comment.isDisabled = 0');
99 }
100 }
101
102 /**
103 * @inheritDoc
104 */
105 public function readObjects()
106 {
107 parent::readObjects();
108
109 $canModerate = $this->commentManager->canModerate($this->objectTypeID, $this->objectID);
110
111 // fetch response ids
112 $responseIDs = $userIDs = [];
113 /** @var StructuredComment $comment */
114 foreach ($this->objects as $comment) {
115 if (!$this->minCommentTime || $comment->time < $this->minCommentTime) {
116 $this->minCommentTime = $comment->time;
117 }
118
119 if ($this->responseLoading) {
120 $commentResponseIDs = ($canModerate) ? $comment->getUnfilteredResponseIDs() : $comment->getResponseIDs();
121 foreach ($commentResponseIDs as $responseID) {
122 $this->responseIDs[] = $responseID;
123 $responseIDs[$responseID] = $comment->commentID;
124 }
125 }
126
127 if ($comment->userID) {
128 $userIDs[] = $comment->userID;
129 }
130
131 $comment->setIsDeletable($this->commentManager->canDeleteComment($comment->getDecoratedObject()));
132 $comment->setIsEditable($this->commentManager->canEditComment($comment->getDecoratedObject()));
133 }
134
135 // fetch last responses
136 if (!empty($responseIDs)) {
137 $responseList = new CommentResponseList();
138 $responseList->setObjectIDs(\array_keys($responseIDs));
139 $responseList->readObjects();
140
141 foreach ($responseList as $response) {
142 $response = new StructuredCommentResponse($response);
143
144 if (isset($this->objects[$response->commentID])) {
145 $response->setComment($this->objects[$response->commentID]->getDecoratedObject());
146 }
147
148 $response->setIsDeletable($this->commentManager->canDeleteResponse($response->getDecoratedObject()));
149 $response->setIsEditable($this->commentManager->canEditResponse($response->getDecoratedObject()));
150
151 $commentID = $responseIDs[$response->responseID];
152 $this->objects[$commentID]->addResponse($response);
153
154 if ($response->userID) {
155 $userIDs[] = $response->userID;
156 }
157 }
158 }
159
160 // cache user ids
161 if (!empty($userIDs)) {
162 UserProfileRuntimeCache::getInstance()->cacheObjectIDs(\array_unique($userIDs));
163 }
164 }
165
166 /**
167 * Fetches the like data.
168 *
169 * @return LikeObject[][]
170 */
171 public function getLikeData()
172 {
173 if (empty($this->objectIDs)) {
174 return [];
175 }
176
177 $likeData = [];
178 $commentObjectType = ReactionHandler::getInstance()->getObjectType('com.woltlab.wcf.comment');
179 ReactionHandler::getInstance()->loadLikeObjects($commentObjectType, $this->getObjectIDs());
180 $likeData['comment'] = ReactionHandler::getInstance()->getLikeObjects($commentObjectType);
181
182 if (!empty($this->responseIDs)) {
183 $responseObjectType = ReactionHandler::getInstance()->getObjectType('com.woltlab.wcf.comment.response');
184 ReactionHandler::getInstance()->loadLikeObjects($responseObjectType, $this->responseIDs);
185 $likeData['response'] = ReactionHandler::getInstance()->getLikeObjects($responseObjectType);
186 }
187
188 return $likeData;
189 }
190
191 /**
192 * Returns minimum comment time.
193 *
194 * @return int
195 */
196 public function getMinCommentTime()
197 {
198 return $this->minCommentTime;
199 }
200
201 /**
202 * Returns the comment manager object.
203 *
204 * @return ICommentManager
205 */
206 public function getCommentManager()
207 {
208 return $this->commentManager;
209 }
210 }