ca2135c9025bb530c57ddce63ea8fcbf73ab7823
[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-2013 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' => new ViewableCommentResponse($queue->getAffectedObject())
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 if ($this->isValid($objectID)) {
131 return $this->getResponse($objectID);
132 }
133
134 return null;
135 }
136
137 /**
138 * @see \wcf\system\moderation\queue\IModerationQueueHandler::isValid()
139 */
140 public function isValid($objectID) {
141 if ($this->getResponse($objectID) === null) {
142 return false;
143 }
144
145 return true;
146 }
147
148 /**
149 * Returns a comment response object by response id or null if response id is invalid.
150 *
151 * @param integer $objectID
152 * @return \wcf\data\comment\response\CommentResponse
153 */
154 protected function getResponse($objectID) {
155 if (!array_key_exists($objectID, self::$responses)) {
156 self::$responses[$objectID] = new CommentResponse($objectID);
157 if (!self::$responses[$objectID]->responseID) {
158 self::$responses[$objectID] = null;
159 }
160 }
161
162 return self::$responses[$objectID];
163 }
164
165 /**
166 * @see \wcf\system\moderation\queue\IModerationQueueHandler::populate()
167 */
168 public function populate(array $queues) {
169 $objectIDs = array();
170 foreach ($queues as $object) {
171 $objectIDs[] = $object->objectID;
172 }
173
174 // fetch responses
175 $responseList = new CommentResponseList();
176 $responseList->getConditionBuilder()->add("comment_response.responseID IN (?)", array($objectIDs));
177 $responseList->readObjects();
178 $responses = $responseList->getObjects();
179
180 // fetch comments
181 $commentIDs = array();
182 foreach ($responses as $response) {
183 $commentIDs[] = $response->commentID;
184 }
185
186 if (!empty($commentIDs)) {
187 $commentList = new CommentList();
188 $commentList->getConditionBuilder()->add("comment.commentID IN (?)", array($commentIDs));
189 $commentList->readObjects();
190 $comments = $commentList->getObjects();
191 }
192
193 foreach ($queues as $object) {
194 if (isset($responses[$object->objectID])) {
195 $response = $responses[$object->objectID];
196 $response->setComment($comments[$response->commentID]);
197
198 $object->setAffectedObject($response);
199 }
200 else {
201 $object->setIsOrphaned();
202 }
203 }
204 }
205
206 /**
207 * @see \wcf\system\moderation\queue\IModerationQueueHandler::removeContent()
208 */
209 public function removeContent(ModerationQueue $queue, $message) {
210 if ($this->isValid($queue->objectID)) {
211 $responseAction = new CommentResponseAction(array($this->getResponse($queue->objectID)), 'delete');
212 $responseAction->executeAction();
213 }
214 }
215 }