af4e75a645b8cd626c1d0640edf65c832aad3d50
[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\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-2015 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 * @see \wcf\system\moderation\queue\AbstractModerationQueueHandler::$className
28 */
29 protected $className = 'wcf\data\comment\response\CommentResponse';
30
31 /**
32 * @see \wcf\system\moderation\queue\AbstractModerationQueueHandler::$objectType
33 */
34 protected $objectType = 'com.woltlab.wcf.comment.response';
35
36 /**
37 * list of comment responses
38 * @var array<\wcf\data\comment\response\CommentResponse>
39 */
40 protected static $responses = array();
41
42 /**
43 * @see \wcf\system\moderation\queue\IModerationQueueHandler::assignQueues()
44 */
45 public function assignQueues(array $queues) {
46 $assignments = array();
47
48 // read comments and responses
49 $responseIDs = array();
50 foreach ($queues as $queue) {
51 $responseIDs[] = $queue->objectID;
52 }
53
54 $conditions = new PreparedStatementConditionBuilder();
55 $conditions->add("comment_response.responseID IN (?)", array($responseIDs));
56
57 $sql = "SELECT comment_response.responseID, comment.commentID, comment.objectTypeID, comment.objectID
58 FROM wcf".WCF_N."_comment_response comment_response
59 LEFT JOIN wcf".WCF_N."_comment comment
60 ON (comment.commentID = comment_response.commentID)
61 ".$conditions;
62 $statement = WCF::getDB()->prepareStatement($sql);
63 $statement->execute($conditions->getParameters());
64 $comments = $responses = array();
65 while ($row = $statement->fetchArray()) {
66 $comments[$row['commentID']] = new Comment(null, $row);
67 $responses[$row['responseID']] = new CommentResponse(null, $row);
68 }
69
70 $orphanedQueueIDs = array();
71 foreach ($queues as $queue) {
72 $assignUser = false;
73
74 if (!isset($responses[$queue->objectID]) || !isset($comments[$responses[$queue->objectID]->commentID])) {
75 $orphanedQueueIDs[] = $queue->queueID;
76 continue;
77 }
78
79 $comment = $comments[$responses[$queue->objectID]->commentID];
80 if ($this->getCommentManager($comment)->canModerate($comment->objectTypeID, $comment->objectID)) {
81 $assignUser = true;
82 }
83
84 $assignments[$queue->queueID] = $assignUser;
85 }
86
87 ModerationQueueManager::getInstance()->removeOrphans($orphanedQueueIDs);
88 ModerationQueueManager::getInstance()->setAssignment($assignments);
89 }
90
91 /**
92 * @see \wcf\system\moderation\queue\report\IModerationQueueReportHandler::canReport()
93 */
94 public function canReport($objectID) {
95 if (!$this->isValid($objectID)) {
96 return false;
97 }
98
99 $response = $this->getResponse($objectID);
100 $comment = $this->getComment($response->commentID);
101 if (!$this->getCommentManager($comment)->isAccessible($comment->objectID)) {
102 return false;
103 }
104
105 return true;
106 }
107
108 /**
109 * @see \wcf\system\moderation\queue\IModerationQueueHandler::getContainerID()
110 */
111 public function getContainerID($objectID) {
112 return 0;
113 }
114
115 /**
116 * @see \wcf\system\moderation\queue\report\IModerationQueueReportHandler::getReportedContent()
117 */
118 public function getReportedContent(ViewableModerationQueue $queue) {
119 WCF::getTPL()->assign(array(
120 'message' => ViewableCommentResponse::getResponse($queue->objectID)
121 ));
122
123 return WCF::getTPL()->fetch('moderationComment');
124 }
125
126 /**
127 * @see \wcf\system\moderation\queue\report\IModerationQueueReportHandler::getReportedObject()
128 */
129 public function getReportedObject($objectID) {
130 return $this->getResponse($objectID);
131 }
132
133 /**
134 * @see \wcf\system\moderation\queue\IModerationQueueHandler::isValid()
135 */
136 public function isValid($objectID) {
137 if ($this->getResponse($objectID) === null) {
138 return false;
139 }
140
141 return true;
142 }
143
144 /**
145 * Returns a comment response object by response id or null if response id is invalid.
146 *
147 * @param integer $objectID
148 * @return \wcf\data\comment\response\CommentResponse
149 */
150 protected function getResponse($objectID) {
151 if (!array_key_exists($objectID, self::$responses)) {
152 self::$responses[$objectID] = new CommentResponse($objectID);
153 if (!self::$responses[$objectID]->responseID) {
154 self::$responses[$objectID] = null;
155 }
156 }
157
158 return self::$responses[$objectID];
159 }
160
161 /**
162 * @see \wcf\system\moderation\queue\IModerationQueueHandler::populate()
163 */
164 public function populate(array $queues) {
165 $objectIDs = array();
166 foreach ($queues as $object) {
167 $objectIDs[] = $object->objectID;
168 }
169
170 // fetch responses
171 $responseList = new CommentResponseList();
172 $responseList->setObjectIDs($objectIDs);
173 $responseList->readObjects();
174 $responses = $responseList->getObjects();
175
176 // fetch comments
177 $commentIDs = array();
178 foreach ($responses as $response) {
179 $commentIDs[] = $response->commentID;
180 }
181
182 if (!empty($commentIDs)) {
183 $commentList = new CommentList();
184 $commentList->setObjectIDs($commentIDs);
185 $commentList->readObjects();
186 $comments = $commentList->getObjects();
187 }
188
189 foreach ($queues as $object) {
190 if (isset($responses[$object->objectID])) {
191 $response = $responses[$object->objectID];
192 $response->setComment($comments[$response->commentID]);
193
194 $object->setAffectedObject($response);
195 }
196 else {
197 $object->setIsOrphaned();
198 }
199 }
200 }
201
202 /**
203 * @see \wcf\system\moderation\queue\IModerationQueueHandler::removeContent()
204 */
205 public function removeContent(ModerationQueue $queue, $message) {
206 if ($this->isValid($queue->objectID)) {
207 $responseAction = new CommentResponseAction(array($this->getResponse($queue->objectID)), 'delete');
208 $responseAction->executeAction();
209 }
210 }
211 }