2f3aa315a1365aac85cfd7854fd10d6ef430c84b
[GitHub/WoltLab/WCF.git] /
1 <?php
2 namespace wcf\system\moderation\queue;
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\WCF;
13
14 /**
15 * An implementation of IModerationQueueHandler for comment responses.
16 *
17 * @author Alexander Ebert
18 * @copyright 2001-2017 WoltLab GmbH
19 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
20 * @package WoltLabSuite\Core\System\Moderation\Queue
21 */
22 class AbstractCommentResponseModerationQueueHandler extends AbstractCommentCommentModerationQueueHandler {
23 /**
24 * @inheritDoc
25 */
26 protected $className = CommentResponse::class;
27
28 /**
29 * @inheritDoc
30 */
31 protected $objectType = 'com.woltlab.wcf.comment.response';
32
33 /**
34 * @inheritDoc
35 */
36 public function assignQueues(array $queues) {
37 $assignments = [];
38
39 // read comments and responses
40 $responseIDs = [];
41 foreach ($queues as $queue) {
42 $responseIDs[] = $queue->objectID;
43 }
44
45 $conditions = new PreparedStatementConditionBuilder();
46 $conditions->add("comment_response.responseID IN (?)", [$responseIDs]);
47
48 $sql = "SELECT comment_response.responseID, comment.commentID, comment.objectTypeID, comment.objectID
49 FROM wcf".WCF_N."_comment_response comment_response
50 LEFT JOIN wcf".WCF_N."_comment comment
51 ON (comment.commentID = comment_response.commentID)
52 ".$conditions;
53 $statement = WCF::getDB()->prepareStatement($sql);
54 $statement->execute($conditions->getParameters());
55 $comments = $responses = [];
56 while ($row = $statement->fetchArray()) {
57 $comments[$row['commentID']] = new Comment(null, $row);
58 $responses[$row['responseID']] = new CommentResponse(null, $row);
59 }
60
61 $orphanedQueueIDs = [];
62 foreach ($queues as $queue) {
63 $assignUser = false;
64
65 if (!isset($responses[$queue->objectID]) || !isset($comments[$responses[$queue->objectID]->commentID])) {
66 $orphanedQueueIDs[] = $queue->queueID;
67 continue;
68 }
69
70 $comment = $comments[$responses[$queue->objectID]->commentID];
71 if ($this->getCommentManager($comment)->canModerate($comment->objectTypeID, $comment->objectID)) {
72 $assignUser = true;
73 }
74
75 $assignments[$queue->queueID] = $assignUser;
76 }
77
78 ModerationQueueManager::getInstance()->removeOrphans($orphanedQueueIDs);
79 ModerationQueueManager::getInstance()->setAssignment($assignments);
80 }
81
82 /**
83 * @inheritDoc
84 */
85 public function getRelatedContent(ViewableModerationQueue $queue) {
86 WCF::getTPL()->assign([
87 'message' => ViewableCommentResponse::getResponse($queue->objectID)
88 ]);
89
90 return WCF::getTPL()->fetch('moderationComment');
91 }
92
93 /**
94 * @inheritDoc
95 */
96 public function isValid($objectID) {
97 if ($this->getResponse($objectID) === null) {
98 return false;
99 }
100
101 return true;
102 }
103
104 /**
105 * Returns a comment response object by response id or null if response id is invalid.
106 *
107 * @param integer $objectID
108 * @return CommentResponse|null
109 */
110 protected function getResponse($objectID) {
111 return CommentResponseRuntimeCache::getInstance()->getObject($objectID);
112 }
113
114 /**
115 * @inheritDoc
116 */
117 public function populate(array $queues) {
118 $objectIDs = [];
119 foreach ($queues as $object) {
120 $objectIDs[] = $object->objectID;
121 }
122
123 $responses = CommentResponseRuntimeCache::getInstance()->getObjects($objectIDs);
124
125 $commentIDs = [];
126 foreach ($responses as $response) {
127 if ($response !== null) {
128 $commentIDs[] = $response->commentID;
129 }
130 }
131
132 $comments = [];
133 if (!empty($commentIDs)) {
134 $comments = CommentRuntimeCache::getInstance()->getObjects($commentIDs);
135 }
136
137 foreach ($queues as $object) {
138 if ($responses[$object->objectID] !== null) {
139 $response = $responses[$object->objectID];
140 $response->setComment($comments[$response->commentID]);
141
142 $object->setAffectedObject($response);
143 }
144 else {
145 $object->setIsOrphaned();
146 }
147 }
148 }
149
150 /**
151 * @inheritDoc
152 */
153 public function removeContent(ModerationQueue $queue, $message) {
154 if ($this->isValid($queue->objectID)) {
155 $responseAction = new CommentResponseAction([$this->getResponse($queue->objectID)], 'delete');
156 $responseAction->executeAction();
157 }
158 }
159 }