225dd4f703de2ce9a34c8f2b74e6bbdac01b4448
[GitHub/WoltLab/WCF.git] /
1 <?php
2 namespace wcf\system\moderation\queue\report;
3 use wcf\data\comment\response\CommentResponse;
4 use wcf\data\comment\response\CommentResponseAction;
5 use wcf\data\comment\response\CommentResponseList;
6 use wcf\data\comment\response\ViewableCommentResponse;
7 use wcf\data\comment\Comment;
8 use wcf\data\comment\CommentList;
9 use wcf\data\moderation\queue\ModerationQueue;
10 use wcf\data\moderation\queue\ViewableModerationQueue;
11 use wcf\system\cache\runtime\CommentResponseRuntimeCache;
12 use wcf\system\cache\runtime\CommentRuntimeCache;
13 use wcf\system\database\util\PreparedStatementConditionBuilder;
14 use wcf\system\moderation\queue\ModerationQueueManager;
15 use wcf\system\WCF;
16
17 /**
18 * An implementation of IModerationQueueReportHandler for comment responses.
19 *
20 * @author Alexander Ebert
21 * @copyright 2001-2016 WoltLab GmbH
22 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
23 * @package com.woltlab.wcf
24 * @subpackage system.moderation.queue
25 * @category Community Framework
26 */
27 class CommentResponseModerationQueueReportHandler extends CommentCommentModerationQueueReportHandler {
28 /**
29 * @inheritDoc
30 */
31 protected $className = CommentResponse::class;
32
33 /**
34 * @inheritDoc
35 */
36 protected $objectType = 'com.woltlab.wcf.comment.response';
37
38 /**
39 * @inheritDoc
40 */
41 public function assignQueues(array $queues) {
42 $assignments = [];
43
44 // read comments and responses
45 $responseIDs = [];
46 foreach ($queues as $queue) {
47 $responseIDs[] = $queue->objectID;
48 }
49
50 $conditions = new PreparedStatementConditionBuilder();
51 $conditions->add("comment_response.responseID IN (?)", [$responseIDs]);
52
53 $sql = "SELECT comment_response.responseID, comment.commentID, comment.objectTypeID, comment.objectID
54 FROM wcf".WCF_N."_comment_response comment_response
55 LEFT JOIN wcf".WCF_N."_comment comment
56 ON (comment.commentID = comment_response.commentID)
57 ".$conditions;
58 $statement = WCF::getDB()->prepareStatement($sql);
59 $statement->execute($conditions->getParameters());
60 $comments = $responses = [];
61 while ($row = $statement->fetchArray()) {
62 $comments[$row['commentID']] = new Comment(null, $row);
63 $responses[$row['responseID']] = new CommentResponse(null, $row);
64 }
65
66 $orphanedQueueIDs = [];
67 foreach ($queues as $queue) {
68 $assignUser = false;
69
70 if (!isset($responses[$queue->objectID]) || !isset($comments[$responses[$queue->objectID]->commentID])) {
71 $orphanedQueueIDs[] = $queue->queueID;
72 continue;
73 }
74
75 $comment = $comments[$responses[$queue->objectID]->commentID];
76 if ($this->getCommentManager($comment)->canModerate($comment->objectTypeID, $comment->objectID)) {
77 $assignUser = true;
78 }
79
80 $assignments[$queue->queueID] = $assignUser;
81 }
82
83 ModerationQueueManager::getInstance()->removeOrphans($orphanedQueueIDs);
84 ModerationQueueManager::getInstance()->setAssignment($assignments);
85 }
86
87 /**
88 * @inheritDoc
89 */
90 public function canReport($objectID) {
91 if (!$this->isValid($objectID)) {
92 return false;
93 }
94
95 $response = $this->getResponse($objectID);
96 $comment = $this->getComment($response->commentID);
97 if (!$this->getCommentManager($comment)->isAccessible($comment->objectID)) {
98 return false;
99 }
100
101 return true;
102 }
103
104 /**
105 * @inheritDoc
106 */
107 public function getContainerID($objectID) {
108 return 0;
109 }
110
111 /**
112 * @inheritDoc
113 */
114 public function getReportedContent(ViewableModerationQueue $queue) {
115 WCF::getTPL()->assign([
116 'message' => ViewableCommentResponse::getResponse($queue->objectID)
117 ]);
118
119 return WCF::getTPL()->fetch('moderationComment');
120 }
121
122 /**
123 * @inheritDoc
124 */
125 public function getReportedObject($objectID) {
126 return $this->getResponse($objectID);
127 }
128
129 /**
130 * @inheritDoc
131 */
132 public function isValid($objectID) {
133 if ($this->getResponse($objectID) === null) {
134 return false;
135 }
136
137 return true;
138 }
139
140 /**
141 * Returns a comment response object by response id or null if response id is invalid.
142 *
143 * @param integer $objectID
144 * @return CommentResponse|null
145 */
146 protected function getResponse($objectID) {
147 return CommentResponseRuntimeCache::getInstance()->getObject($objectID);
148 }
149
150 /**
151 * @inheritDoc
152 */
153 public function populate(array $queues) {
154 $objectIDs = [];
155 foreach ($queues as $object) {
156 $objectIDs[] = $object->objectID;
157 }
158
159 $responses = CommentResponseRuntimeCache::getInstance()->getObjects($objectIDs);
160
161 $commentIDs = [];
162 foreach ($responses as $response) {
163 if ($response !== null) {
164 $commentIDs[] = $response->commentID;
165 }
166 }
167
168 if (!empty($commentIDs)) {
169 $comments = CommentRuntimeCache::getInstance()->getObjects($commentIDs);
170 }
171
172 foreach ($queues as $object) {
173 if ($responses[$object->objectID] !== null) {
174 $response = $responses[$object->objectID];
175 $response->setComment($comments[$response->commentID]);
176
177 $object->setAffectedObject($response);
178 }
179 else {
180 $object->setIsOrphaned();
181 }
182 }
183 }
184
185 /**
186 * @inheritDoc
187 */
188 public function removeContent(ModerationQueue $queue, $message) {
189 if ($this->isValid($queue->objectID)) {
190 $responseAction = new CommentResponseAction([$this->getResponse($queue->objectID)], 'delete');
191 $responseAction->executeAction();
192 }
193 }
194 }