5 use wcf\data\conversation\Conversation
;
6 use wcf\data\conversation\ConversationAction
;
7 use wcf\data\conversation\ConversationParticipantList
;
8 use wcf\data\conversation\label\ConversationLabel
;
9 use wcf\data\conversation\label\ConversationLabelList
;
10 use wcf\data\conversation\message\ConversationMessage
;
11 use wcf\data\conversation\message\ViewableConversationMessageList
;
12 use wcf\data\conversation\ViewableConversation
;
13 use wcf\data\modification\log\ConversationLogModificationLogList
;
14 use wcf\data\smiley\SmileyCache
;
15 use wcf\data\user\UserProfile
;
16 use wcf\system\attachment\AttachmentHandler
;
17 use wcf\system\bbcode\BBCodeHandler
;
18 use wcf\system\exception\IllegalLinkException
;
19 use wcf\system\exception\PermissionDeniedException
;
20 use wcf\system\message\quote\MessageQuoteManager
;
21 use wcf\system\page\PageLocationManager
;
22 use wcf\system\page\ParentPageLocation
;
23 use wcf\system\request\LinkHandler
;
24 use wcf\system\user\signature\SignatureCache
;
26 use wcf\util\HeaderUtil
;
27 use wcf\util\StringUtil
;
30 * Shows a conversation.
33 * @copyright 2001-2019 WoltLab GmbH
34 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
35 * @package WoltLabSuite\Core\Page
37 * @property ViewableConversationMessageList $objectList
39 class ConversationPage
extends MultipleLinkPage
44 public $itemsPerPage = CONVERSATION_MESSAGES_PER_PAGE
;
49 public $sortOrder = 'ASC';
54 public $objectListClassName = ViewableConversationMessageList
::class;
59 public $loginRequired = true;
64 public $neededModules = ['MODULE_CONVERSATION'];
69 public $neededPermissions = ['user.conversation.canUseConversation'];
75 public $conversationID = 0;
78 * viewable conversation object
79 * @var ViewableConversation
84 * conversation label list
85 * @var ConversationLabelList
93 public $messageID = 0;
96 * conversation message object
97 * @var ConversationMessage
102 * modification log list object
103 * @var ConversationLogModificationLogList
105 public $modificationLogList;
108 * list of participants
109 * @var ConversationParticipantList
111 public $participantList;
116 public function readParameters()
118 parent
::readParameters();
120 if (isset($_REQUEST['id'])) {
121 $this->conversationID
= \
intval($_REQUEST['id']);
123 if (isset($_REQUEST['messageID'])) {
124 $this->messageID
= \
intval($_REQUEST['messageID']);
126 if ($this->messageID
) {
127 $this->message
= new ConversationMessage($this->messageID
);
128 if (!$this->message
->messageID
) {
129 throw new IllegalLinkException();
131 $this->conversationID
= $this->message
->conversationID
;
134 $this->conversation
= Conversation
::getUserConversation($this->conversationID
, WCF
::getUser()->userID
);
135 if ($this->conversation
=== null) {
136 throw new IllegalLinkException();
138 if (!$this->conversation
->canRead()) {
139 throw new PermissionDeniedException();
143 $this->labelList
= ConversationLabel
::getLabelsByUser();
144 $this->conversation
= ViewableConversation
::getViewableConversation($this->conversation
, $this->labelList
);
147 /** @noinspection PhpUndefinedFieldInspection */
148 if (WCF
::getUser()->conversationMessagesPerPage
) {
149 /** @noinspection PhpUndefinedFieldInspection */
150 $this->itemsPerPage
= WCF
::getUser()->conversationMessagesPerPage
;
153 $this->canonicalURL
= LinkHandler
::getInstance()->getLink('Conversation', [
154 'object' => $this->conversation
,
155 ], ($this->pageNo ?
'pageNo=' . $this->pageNo
: ''));
161 protected function initObjectList()
163 parent
::initObjectList();
165 $this->objectList
->getConditionBuilder()
166 ->add('conversation_message.conversationID = ?', [$this->conversation
->conversationID
]);
167 $this->objectList
->setConversation($this->conversation
->getDecoratedObject());
169 // handle visibility filter
170 if ($this->conversation
->joinedAt
> 0) {
171 $this->objectList
->getConditionBuilder()
172 ->add('conversation_message.time >= ?', [$this->conversation
->joinedAt
]);
174 if ($this->conversation
->leftAt
> 0) {
175 $this->objectList
->getConditionBuilder()
176 ->add('conversation_message.time <= ?', [$this->conversation
->leftAt
]);
180 if ($this->action
== 'lastPost') {
181 $this->goToLastPost();
183 if ($this->action
== 'firstNew') {
184 $this->goToFirstNewPost();
186 if ($this->messageID
) {
194 public function readData()
199 if ($this->conversation
->isDraft
) {
200 // `-1` = pseudo object id to have to pages with identifier `com.woltlab.wcf.conversation.ConversationList`
201 PageLocationManager
::getInstance()->addParentLocation(
202 'com.woltlab.wcf.conversation.ConversationList',
204 new ParentPageLocation(
205 WCF
::getLanguage()->get('wcf.conversation.folder.draft'),
206 LinkHandler
::getInstance()->getLink('ConversationList', ['filter' => 'draft'])
210 PageLocationManager
::getInstance()->addParentLocation('com.woltlab.wcf.conversation.ConversationList');
212 // update last visit time count
214 $this->conversation
->isNew()
216 $this->objectList
->getMaxPostTime() > $this->conversation
->lastVisitTime
217 ||
($this->conversation
->joinedAt
&& !\
count($this->objectList
))
220 $visitTime = $this->objectList
->getMaxPostTime();
221 if ($visitTime == $this->conversation
->lastPostTime
) {
222 $visitTime = TIME_NOW
;
224 $conversationAction = new ConversationAction(
225 [$this->conversation
->getDecoratedObject()],
227 ['visitTime' => $visitTime]
229 $conversationAction->executeAction();
233 $this->participantList
= new ConversationParticipantList(
234 $this->conversationID
,
235 WCF
::getUser()->userID
,
236 $this->conversation
->userID
== WCF
::getUser()->userID
238 $this->participantList
->readObjects();
240 // init quote objects
242 foreach ($this->objectList
as $message) {
243 $messageIDs[] = $message->messageID
;
245 MessageQuoteManager
::getInstance()->initObjects('com.woltlab.wcf.conversation.message', $messageIDs);
248 foreach ($this->objectList
as $message) {
249 if ($message->userID
) {
250 $userIDs[] = $message->userID
;
254 // fetch special trophies
256 if (!empty($userIDs)) {
257 UserProfile
::prepareSpecialTrophies(\array_unique
($userIDs));
261 if (MODULE_USER_SIGNATURE
) {
262 if (!empty($userIDs)) {
263 SignatureCache
::getInstance()->cacheUserSignature($userIDs);
267 // set attachment permissions
268 if ($this->objectList
->getAttachmentList() !== null) {
269 $this->objectList
->getAttachmentList()->setPermissions([
270 'canDownload' => true,
271 'canViewPreview' => true,
275 // get timeframe for modifications
276 $this->objectList
->rewind();
277 $startTime = ($this->conversation
->joinedAt ?
: $this->objectList
->current()->time
);
278 $endTime = ($this->conversation
->leftAt ?
: TIME_NOW
);
280 $count = \
count($this->objectList
);
282 $this->objectList
->seek($count - 1);
283 if ($this->objectList
->current()->time
< $this->conversation
->lastPostTime
) {
285 FROM wcf" . WCF_N
. "_conversation_message
286 WHERE conversationID = ?
289 $statement = WCF
::getDB()->prepareStatement($sql, 1);
290 $statement->execute([$this->conversationID
, $this->objectList
->current()->time
]);
291 $endTime = $statement->fetchSingleColumn() - 1;
294 $this->objectList
->rewind();
296 // get invisible participants
297 $invisibleParticipantIDs = [];
298 if (WCF
::getUser()->userID
!= $this->conversation
->userID
) {
299 foreach ($this->participantList
as $participant) {
300 if ($participant->isInvisible
) {
301 $invisibleParticipantIDs[] = $participant->userID
;
306 // load modification log entries
307 $this->modificationLogList
= new ConversationLogModificationLogList($this->conversation
->conversationID
);
308 $this->modificationLogList
->getConditionBuilder()
309 ->add("modification_log.time BETWEEN ? AND ?", [$startTime, $endTime]);
311 if (!empty($invisibleParticipantIDs)) {
312 $this->modificationLogList
->getConditionBuilder()->add(
313 "(modification_log.action <> ? OR modification_log.userID NOT IN (?))",
314 ['leave', $invisibleParticipantIDs]
318 $this->modificationLogList
->readObjects();
324 public function assignVariables()
326 parent
::assignVariables();
328 MessageQuoteManager
::getInstance()->assignVariables();
330 $tmpHash = StringUtil
::getRandomID();
331 $attachmentHandler = new AttachmentHandler('com.woltlab.wcf.conversation.message', 0, $tmpHash, 0);
333 WCF
::getTPL()->assign([
334 'attachmentHandler' => $attachmentHandler,
335 'attachmentObjectID' => 0,
336 'attachmentObjectType' => 'com.woltlab.wcf.conversation.message',
337 'attachmentParentObjectID' => 0,
338 'tmpHash' => $tmpHash,
339 'attachmentList' => $this->objectList
->getAttachmentList(),
340 'labelList' => $this->labelList
,
341 'modificationLogList' => $this->modificationLogList
,
342 'sortOrder' => $this->sortOrder
,
343 'conversation' => $this->conversation
,
344 'conversationID' => $this->conversationID
,
345 'participants' => $this->participantList
->getObjects(),
346 'defaultSmilies' => SmileyCache
::getInstance()->getCategorySmilies(),
349 BBCodeHandler
::getInstance()->setDisallowedBBCodes(\
explode(
351 WCF
::getSession()->getPermission('user.message.disallowedBBCodes')
356 * Calculates the position of a specific post in this conversation.
358 protected function goToPost()
360 $conditionBuilder = clone $this->objectList
->getConditionBuilder();
361 $conditionBuilder->add('time ' . ($this->sortOrder
== 'ASC' ?
'<=' : '>=') . ' ?', [$this->message
->time
]);
363 $sql = "SELECT COUNT(*) AS messages
364 FROM wcf" . WCF_N
. "_conversation_message conversation_message
365 " . $conditionBuilder;
366 $statement = WCF
::getDB()->prepareStatement($sql);
367 $statement->execute($conditionBuilder->getParameters());
368 $row = $statement->fetchArray();
369 $this->pageNo
= \
intval(\
ceil($row['messages'] / $this->itemsPerPage
));
373 * Gets the id of the last post in this conversation and forwards the user to this post.
375 protected function goToLastPost()
377 $sql = "SELECT conversation_message.messageID
378 FROM wcf" . WCF_N
. "_conversation_message conversation_message
379 " . $this->objectList
->getConditionBuilder() . "
380 ORDER BY time " . ($this->sortOrder
== 'ASC' ?
'DESC' : 'ASC');
381 $statement = WCF
::getDB()->prepareStatement($sql, 1);
382 $statement->execute($this->objectList
->getConditionBuilder()->getParameters());
383 $row = $statement->fetchArray();
384 HeaderUtil
::redirect(
385 LinkHandler
::getInstance()->getLink(
388 'encodeTitle' => true,
389 'object' => $this->conversation
,
390 'messageID' => $row['messageID'],
392 ) . '#message' . $row['messageID']
399 * Forwards the user to the first new message in this conversation.
401 protected function goToFirstNewPost()
403 $conditionBuilder = clone $this->objectList
->getConditionBuilder();
404 $conditionBuilder->add('time > ?', [$this->conversation
->lastVisitTime
]);
406 $sql = "SELECT conversation_message.messageID
407 FROM wcf" . WCF_N
. "_conversation_message conversation_message
408 " . $conditionBuilder . "
410 $statement = WCF
::getDB()->prepareStatement($sql, 1);
411 $statement->execute($conditionBuilder->getParameters());
412 $row = $statement->fetchArray();
413 if ($row !== false) {
414 HeaderUtil
::redirect(
415 LinkHandler
::getInstance()->getLink(
418 'encodeTitle' => true,
419 'object' => $this->conversation
,
420 'messageID' => $row['messageID'],
422 ) . '#message' . $row['messageID']
427 $this->goToLastPost();