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