Remove obsolete template code
[GitHub/WoltLab/com.woltlab.wcf.conversation.git] / files / lib / page / ConversationPage.class.php
1 <?php
2 namespace wcf\page;
3 use wcf\data\conversation\label\ConversationLabel;
4 use wcf\data\conversation\label\ConversationLabelList;
5 use wcf\data\conversation\message\ConversationMessage;
6 use wcf\data\conversation\message\ViewableConversationMessageList;
7 use wcf\data\conversation\Conversation;
8 use wcf\data\conversation\ConversationAction;
9 use wcf\data\conversation\ConversationParticipantList;
10 use wcf\data\conversation\ViewableConversation;
11 use wcf\data\modification\log\ConversationLogModificationLogList;
12 use wcf\data\smiley\SmileyCache;
13 use wcf\data\user\UserProfile;
14 use wcf\system\attachment\AttachmentHandler;
15 use wcf\system\bbcode\BBCodeHandler;
16 use wcf\system\exception\IllegalLinkException;
17 use wcf\system\exception\PermissionDeniedException;
18 use wcf\system\message\quote\MessageQuoteManager;
19 use wcf\system\page\PageLocationManager;
20 use wcf\system\page\ParentPageLocation;
21 use wcf\system\request\LinkHandler;
22 use wcf\system\user\signature\SignatureCache;
23 use wcf\system\WCF;
24 use wcf\util\HeaderUtil;
25 use wcf\util\StringUtil;
26
27 /**
28 * Shows a conversation.
29 *
30 * @author Marcel Werk
31 * @copyright 2001-2019 WoltLab GmbH
32 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
33 * @package WoltLabSuite\Core\Page
34 *
35 * @property ViewableConversationMessageList $objectList
36 */
37 class ConversationPage extends MultipleLinkPage {
38 /**
39 * @inheritDoc
40 */
41 public $itemsPerPage = CONVERSATION_MESSAGES_PER_PAGE;
42
43 /**
44 * @inheritDoc
45 */
46 public $sortOrder = 'ASC';
47
48 /**
49 * @inheritDoc
50 */
51 public $objectListClassName = ViewableConversationMessageList::class;
52
53 /**
54 * @inheritDoc
55 */
56 public $loginRequired = true;
57
58 /**
59 * @inheritDoc
60 */
61 public $neededModules = ['MODULE_CONVERSATION'];
62
63 /**
64 * @inheritDoc
65 */
66 public $neededPermissions = ['user.conversation.canUseConversation'];
67
68 /**
69 * conversation id
70 * @var integer
71 */
72 public $conversationID = 0;
73
74 /**
75 * viewable conversation object
76 * @var ViewableConversation
77 */
78 public $conversation;
79
80 /**
81 * conversation label list
82 * @var ConversationLabelList
83 */
84 public $labelList;
85
86 /**
87 * message id
88 * @var integer
89 */
90 public $messageID = 0;
91
92 /**
93 * conversation message object
94 * @var ConversationMessage
95 */
96 public $message;
97
98 /**
99 * modification log list object
100 * @var ConversationLogModificationLogList
101 */
102 public $modificationLogList;
103
104 /**
105 * list of participants
106 * @var ConversationParticipantList
107 */
108 public $participantList;
109
110 /**
111 * @inheritDoc
112 */
113 public function readParameters() {
114 parent::readParameters();
115
116 if (isset($_REQUEST['id'])) $this->conversationID = intval($_REQUEST['id']);
117 if (isset($_REQUEST['messageID'])) $this->messageID = intval($_REQUEST['messageID']);
118 if ($this->messageID) {
119 $this->message = new ConversationMessage($this->messageID);
120 if (!$this->message->messageID) {
121 throw new IllegalLinkException();
122 }
123 $this->conversationID = $this->message->conversationID;
124 }
125
126 $this->conversation = Conversation::getUserConversation($this->conversationID, WCF::getUser()->userID);
127 if ($this->conversation === null) {
128 throw new IllegalLinkException();
129 }
130 if (!$this->conversation->canRead()) {
131 throw new PermissionDeniedException();
132 }
133
134 // load labels
135 $this->labelList = ConversationLabel::getLabelsByUser();
136 $this->conversation = ViewableConversation::getViewableConversation($this->conversation, $this->labelList);
137
138 // messages per page
139 /** @noinspection PhpUndefinedFieldInspection */
140 if (WCF::getUser()->conversationMessagesPerPage) {
141 /** @noinspection PhpUndefinedFieldInspection */
142 $this->itemsPerPage = WCF::getUser()->conversationMessagesPerPage;
143 }
144
145 $this->canonicalURL = LinkHandler::getInstance()->getLink('Conversation', [
146 'object' => $this->conversation
147 ], ($this->pageNo ? 'pageNo=' . $this->pageNo : ''));
148 }
149
150 /**
151 * @inheritDoc
152 */
153 protected function initObjectList() {
154 parent::initObjectList();
155
156 $this->objectList->getConditionBuilder()->add('conversation_message.conversationID = ?', [$this->conversation->conversationID]);
157 $this->objectList->setConversation($this->conversation->getDecoratedObject());
158
159 // handle visibility filter
160 if ($this->conversation->joinedAt > 0) $this->objectList->getConditionBuilder()->add('conversation_message.time >= ?', [$this->conversation->joinedAt]);
161 if ($this->conversation->leftAt > 0) $this->objectList->getConditionBuilder()->add('conversation_message.time <= ?', [$this->conversation->leftAt]);
162
163 // handle jump to
164 if ($this->action == 'lastPost') $this->goToLastPost();
165 if ($this->action == 'firstNew') $this->goToFirstNewPost();
166 if ($this->messageID) $this->goToPost();
167 }
168
169 /**
170 * @inheritDoc
171 */
172 public function readData() {
173 parent::readData();
174
175 // add breadcrumbs
176 if ($this->conversation->isDraft) {
177 // `-1` = pseudo object id to have to pages with identifier `com.woltlab.wcf.conversation.ConversationList`
178 PageLocationManager::getInstance()->addParentLocation('com.woltlab.wcf.conversation.ConversationList', -1, new ParentPageLocation(
179 WCF::getLanguage()->get('wcf.conversation.folder.draft'),
180 LinkHandler::getInstance()->getLink('ConversationList', ['filter' => 'draft'])
181 ));
182 }
183 PageLocationManager::getInstance()->addParentLocation('com.woltlab.wcf.conversation.ConversationList');
184
185 // update last visit time count
186 if (
187 $this->conversation->isNew()
188 && (
189 $this->objectList->getMaxPostTime() > $this->conversation->lastVisitTime
190 || ($this->conversation->joinedAt && !count($this->objectList))
191 )
192 ) {
193 $visitTime = $this->objectList->getMaxPostTime();
194 if ($visitTime == $this->conversation->lastPostTime) $visitTime = TIME_NOW;
195 $conversationAction = new ConversationAction([$this->conversation->getDecoratedObject()], 'markAsRead', ['visitTime' => $visitTime]);
196 $conversationAction->executeAction();
197 }
198
199 // get participants
200 $this->participantList = new ConversationParticipantList($this->conversationID, WCF::getUser()->userID, $this->conversation->userID == WCF::getUser()->userID);
201 $this->participantList->readObjects();
202
203 // init quote objects
204 $messageIDs = [];
205 foreach ($this->objectList as $message) {
206 $messageIDs[] = $message->messageID;
207 }
208 MessageQuoteManager::getInstance()->initObjects('com.woltlab.wcf.conversation.message', $messageIDs);
209
210 $userIDs = [];
211 foreach ($this->objectList as $message) {
212 if ($message->userID) {
213 $userIDs[] = $message->userID;
214 }
215 }
216
217 // fetch special trophies
218 if (MODULE_TROPHY) {
219 if (!empty($userIDs)) {
220 UserProfile::prepareSpecialTrophies(array_unique($userIDs));
221 }
222 }
223
224 if (MODULE_USER_SIGNATURE) {
225 if (!empty($userIDs)) {
226 SignatureCache::getInstance()->cacheUserSignature($userIDs);
227 }
228 }
229
230 // set attachment permissions
231 if ($this->objectList->getAttachmentList() !== null) {
232 $this->objectList->getAttachmentList()->setPermissions([
233 'canDownload' => true,
234 'canViewPreview' => true
235 ]);
236 }
237
238 // get timeframe for modifications
239 $this->objectList->rewind();
240 $startTime = ($this->conversation->joinedAt ?: $this->objectList->current()->time);
241 $endTime = ($this->conversation->leftAt ?: TIME_NOW);
242
243 $count = count($this->objectList);
244 if ($count > 1) {
245 $this->objectList->seek($count - 1);
246 if ($this->objectList->current()->time < $this->conversation->lastPostTime) {
247 $sql = "SELECT time
248 FROM wcf".WCF_N."_conversation_message
249 WHERE conversationID = ?
250 AND time > ?
251 ORDER BY time";
252 $statement = WCF::getDB()->prepareStatement($sql, 1);
253 $statement->execute([$this->conversationID, $this->objectList->current()->time]);
254 $endTime = $statement->fetchSingleColumn() - 1;
255 }
256 }
257 $this->objectList->rewind();
258
259 // get invisible participants
260 $invisibleParticipantIDs = [];
261 if (WCF::getUser()->userID != $this->conversation->userID) {
262 foreach ($this->participantList as $participant) {
263 if ($participant->isInvisible) {
264 $invisibleParticipantIDs[] = $participant->userID;
265 }
266 }
267 }
268
269 // load modification log entries
270 $this->modificationLogList = new ConversationLogModificationLogList($this->conversation->conversationID);
271 $this->modificationLogList->getConditionBuilder()->add("modification_log.time BETWEEN ? AND ?", [$startTime, $endTime]);
272
273 if (!empty($invisibleParticipantIDs)) {
274 $this->modificationLogList->getConditionBuilder()->add("(modification_log.action <> ? OR modification_log.userID NOT IN (?))", ['leave', $invisibleParticipantIDs]);
275 }
276
277 $this->modificationLogList->readObjects();
278 }
279
280 /**
281 * @inheritDoc
282 */
283 public function assignVariables() {
284 parent::assignVariables();
285
286 MessageQuoteManager::getInstance()->assignVariables();
287
288 $tmpHash = StringUtil::getRandomID();
289 $attachmentHandler = new AttachmentHandler('com.woltlab.wcf.conversation.message', 0, $tmpHash, 0);
290
291 WCF::getTPL()->assign([
292 'attachmentHandler' => $attachmentHandler,
293 'attachmentObjectID' => 0,
294 'attachmentObjectType' => 'com.woltlab.wcf.conversation.message',
295 'attachmentParentObjectID' => 0,
296 'tmpHash' => $tmpHash,
297 'attachmentList' => $this->objectList->getAttachmentList(),
298 'labelList' => $this->labelList,
299 'modificationLogList' => $this->modificationLogList,
300 'sortOrder' => $this->sortOrder,
301 'conversation' => $this->conversation,
302 'conversationID' => $this->conversationID,
303 'participants' => $this->participantList->getObjects(),
304 'defaultSmilies' => SmileyCache::getInstance()->getCategorySmilies()
305 ]);
306
307 BBCodeHandler::getInstance()->setDisallowedBBCodes(explode(',', WCF::getSession()->getPermission('user.message.disallowedBBCodes')));
308 }
309
310 /**
311 * Calculates the position of a specific post in this conversation.
312 */
313 protected function goToPost() {
314 $conditionBuilder = clone $this->objectList->getConditionBuilder();
315 $conditionBuilder->add('time '.($this->sortOrder == 'ASC' ? '<=' : '>=').' ?', [$this->message->time]);
316
317 $sql = "SELECT COUNT(*) AS messages
318 FROM wcf".WCF_N."_conversation_message conversation_message
319 ".$conditionBuilder;
320 $statement = WCF::getDB()->prepareStatement($sql);
321 $statement->execute($conditionBuilder->getParameters());
322 $row = $statement->fetchArray();
323 $this->pageNo = intval(ceil($row['messages'] / $this->itemsPerPage));
324 }
325
326 /**
327 * Gets the id of the last post in this conversation and forwards the user to this post.
328 */
329 protected function goToLastPost() {
330 $sql = "SELECT conversation_message.messageID
331 FROM wcf".WCF_N."_conversation_message conversation_message
332 ".$this->objectList->getConditionBuilder()."
333 ORDER BY time ".($this->sortOrder == 'ASC' ? 'DESC' : 'ASC');
334 $statement = WCF::getDB()->prepareStatement($sql, 1);
335 $statement->execute($this->objectList->getConditionBuilder()->getParameters());
336 $row = $statement->fetchArray();
337 HeaderUtil::redirect(LinkHandler::getInstance()->getLink('Conversation', [
338 'encodeTitle' => true,
339 'object' => $this->conversation,
340 'messageID' => $row['messageID']
341 ]).'#message'.$row['messageID']);
342 exit;
343 }
344
345 /**
346 * Forwards the user to the first new message in this conversation.
347 */
348 protected function goToFirstNewPost() {
349 $conditionBuilder = clone $this->objectList->getConditionBuilder();
350 $conditionBuilder->add('time > ?', [$this->conversation->lastVisitTime]);
351
352 $sql = "SELECT conversation_message.messageID
353 FROM wcf".WCF_N."_conversation_message conversation_message
354 ".$conditionBuilder."
355 ORDER BY time ASC";
356 $statement = WCF::getDB()->prepareStatement($sql, 1);
357 $statement->execute($conditionBuilder->getParameters());
358 $row = $statement->fetchArray();
359 if ($row !== false) {
360 HeaderUtil::redirect(LinkHandler::getInstance()->getLink('Conversation', [
361 'encodeTitle' => true,
362 'object' => $this->conversation,
363 'messageID' => $row['messageID']
364 ]).'#message'.$row['messageID']);
365 exit;
366 }
367 else {
368 $this->goToLastPost();
369 }
370 }
371 }