c2562b8482c92298aae4de6a39958e13744b86a4
[GitHub/WoltLab/WCF.git] /
1 <?php
2 namespace wcf\system\moderation\queue\report;
3 use wcf\data\comment\Comment;
4 use wcf\data\comment\CommentAction;
5 use wcf\data\comment\CommentList;
6 use wcf\data\comment\ViewableComment;
7 use wcf\data\moderation\queue\ModerationQueue;
8 use wcf\data\moderation\queue\ViewableModerationQueue;
9 use wcf\data\object\type\ObjectTypeCache;
10 use wcf\system\database\util\PreparedStatementConditionBuilder;
11 use wcf\system\moderation\queue\AbstractModerationQueueHandler;
12 use wcf\system\moderation\queue\ModerationQueueManager;
13 use wcf\system\WCF;
14
15 /**
16 * An implementation of IModerationQueueReportHandler for comments.
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.comment
22 * @subpackage system.moderation.queue
23 * @category Community Framework
24 */
25 class CommentCommentModerationQueueReportHandler extends AbstractModerationQueueHandler implements IModerationQueueReportHandler {
26 /**
27 * @see wcf\system\moderation\queue\AbstractModerationQueueHandler::$definitionName
28 */
29 protected $definitionName = 'com.woltlab.wcf.moderation.report';
30
31 /**
32 * @see wcf\system\moderation\queue\AbstractModerationQueueHandler::$objectType
33 */
34 protected $objectType = 'com.woltlab.wcf.comment.comment';
35
36 /**
37 * list of comments
38 * @var array<wcf\data\comment\Comment>
39 */
40 protected static $comments = array();
41
42 /**
43 * list of comment managers
44 * @var array<wcf\system\comment\manager\ICommentManager>
45 */
46 protected static $commentManagers = array();
47
48 /**
49 * @see wcf\system\moderation\queue\IModerationQueueHandler::assignQueues()
50 */
51 public function assignQueues(array $queues) {
52 $assignments = array();
53
54 // read comments
55 $commentIDs = array();
56 foreach ($queues as $queue) {
57 $commentIDs[] = $queue->objectID;
58 }
59
60 $conditions = new PreparedStatementConditionBuilder();
61 $conditions->add("commentID IN (?)", array($commentIDs));
62
63 $sql = "SELECT commentID, objectTypeID, objectID
64 FROM wcf".WCF_N."_comment
65 ".$conditions;
66 $statement = WCF::getDB()->prepareStatement($sql);
67 $statement->execute($conditions->getParameters());
68 $comments = array();
69 while ($row = $statement->fetchArray()) {
70 $comments[$row['commentID']] = new Comment(null, $row);
71 }
72
73 foreach ($queues as $queue) {
74 $assignUser = false;
75
76 $comment = $comments[$queue->objectID];
77 if ($this->getCommentManager($comment)->canModerate($comment->objectTypeID, $comment->objectID)) {
78 $assignUser = true;
79 }
80
81 $assignments[$queue->queueID] = $assignUser;
82 }
83
84 ModerationQueueManager::getInstance()->setAssignment($assignments);
85 }
86
87 /**
88 * @see wcf\system\moderation\queue\report\IModerationQueueReportHandler::canReport()
89 */
90 public function canReport($objectID) {
91 if (!$this->isValid($objectID)) {
92 return false;
93 }
94
95 $comment = $this->getComment($objectID);
96 if (!$this->getCommentManager($comment)->isAccessible($comment->objectID)) {
97 return false;
98 }
99
100 return true;
101 }
102
103 /**
104 * @see wcf\system\moderation\queue\IModerationQueueHandler::getContainerID()
105 */
106 public function getContainerID($objectID) {
107 return 0;
108 }
109
110 /**
111 * @see wcf\system\moderation\queue\report\IModerationQueueReportHandler::getReportedContent()
112 */
113 public function getReportedContent(ViewableModerationQueue $queue) {
114 WCF::getTPL()->assign(array(
115 'message' => new ViewableComment($queue->getAffectedObject())
116 ));
117
118 return WCF::getTPL()->fetch('moderationComment');
119 }
120
121 /**
122 * @see wcf\system\moderation\queue\report\IModerationQueueReportHandler::getReportedObject()
123 */
124 public function getReportedObject($objectID) {
125 if ($this->isValid($objectID)) {
126 return $this->getComment($objectID);
127 }
128
129 return null;
130 }
131
132 /**
133 * @see wcf\system\moderation\queue\IModerationQueueHandler::isValid()
134 */
135 public function isValid($objectID) {
136 if ($this->getComment($objectID) === null) {
137 return false;
138 }
139
140 return true;
141 }
142
143 /**
144 * Returns a comment object by comment id or null if comment id is invalid.
145 *
146 * @param integer $objectID
147 * @return wcf\data\comment\Comment
148 */
149 protected function getComment($objectID) {
150 if (!array_key_exists($objectID, self::$comments)) {
151 self::$comments[$objectID] = new Comment($objectID);
152 if (!self::$comments[$objectID]->commentID) {
153 self::$comments[$objectID] = null;
154 }
155 }
156
157 return self::$comments[$objectID];
158 }
159
160 /**
161 * Returns a comment manager for given comment.
162 *
163 * @param wcf\data\comment\Comment $comment
164 * @return wcf\system\comment\manager\ICommentManager
165 */
166 protected function getCommentManager(Comment $comment) {
167 if (!isset(self::$commentManagers[$comment->objectTypeID])) {
168 self::$commentManagers[$comment->objectTypeID] = ObjectTypeCache::getInstance()->getObjectType($comment->objectTypeID)->getProcessor();
169 }
170
171 return self::$commentManagers[$comment->objectTypeID];
172 }
173
174 /**
175 * @see wcf\system\moderation\queue\IModerationQueueHandler::populate()
176 */
177 public function populate(array $queues) {
178 $objectIDs = array();
179 foreach ($queues as $object) {
180 $objectIDs[] = $object->objectID;
181 }
182
183 // fetch comments
184 $commentList = new CommentList();
185 $commentList->getConditionBuilder()->add("comment.commentID IN (?)", array($objectIDs));
186 $commentList->readObjects();
187 $comments = $commentList->getObjects();
188
189 foreach ($queues as $object) {
190 if (isset($comments[$object->objectID])) {
191 $object->setAffectedObject($comments[$object->objectID]);
192 }
193 else {
194 $object->setIsOrphaned();
195 }
196 }
197 }
198
199 /**
200 * @see wcf\system\moderation\queue\IModerationQueueHandler::removeContent()
201 */
202 public function removeContent(ModerationQueue $queue, $message) {
203 if ($this->isValid($queue->objectID)) {
204 $commentAction = new CommentAction(array($this->getComment($queue->objectID)), 'delete');
205 $commentAction->executeAction();
206 }
207 }
208 }