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
;
23 use wcf\util\HeaderUtil
;
24 use wcf\util\StringUtil
;
27 * Shows a conversation.
30 * @copyright 2001-2019 WoltLab GmbH
31 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
32 * @package WoltLabSuite\Core\Page
34 * @property ViewableConversationMessageList $objectList
36 class ConversationPage
extends MultipleLinkPage
{
40 public $itemsPerPage = CONVERSATION_MESSAGES_PER_PAGE
;
45 public $sortOrder = 'ASC';
50 public $objectListClassName = ViewableConversationMessageList
::class;
55 public $loginRequired = true;
60 public $neededModules = ['MODULE_CONVERSATION'];
65 public $neededPermissions = ['user.conversation.canUseConversation'];
71 public $conversationID = 0;
74 * viewable conversation object
75 * @var ViewableConversation
80 * conversation label list
81 * @var ConversationLabelList
89 public $messageID = 0;
92 * conversation message object
93 * @var ConversationMessage
98 * modification log list object
99 * @var ConversationLogModificationLogList
101 public $modificationLogList;
104 * list of participants
105 * @var ConversationParticipantList
107 public $participantList;
112 public function readParameters() {
113 parent
::readParameters();
115 if (isset($_REQUEST['id'])) $this->conversationID
= intval($_REQUEST['id']);
116 if (isset($_REQUEST['messageID'])) $this->messageID
= intval($_REQUEST['messageID']);
117 if ($this->messageID
) {
118 $this->message
= new ConversationMessage($this->messageID
);
119 if (!$this->message
->messageID
) {
120 throw new IllegalLinkException();
122 $this->conversationID
= $this->message
->conversationID
;
125 $this->conversation
= Conversation
::getUserConversation($this->conversationID
, WCF
::getUser()->userID
);
126 if ($this->conversation
=== null) {
127 throw new IllegalLinkException();
129 if (!$this->conversation
->canRead()) {
130 throw new PermissionDeniedException();
134 $this->labelList
= ConversationLabel
::getLabelsByUser();
135 $this->conversation
= ViewableConversation
::getViewableConversation($this->conversation
, $this->labelList
);
138 /** @noinspection PhpUndefinedFieldInspection */
139 if (WCF
::getUser()->conversationMessagesPerPage
) {
140 /** @noinspection PhpUndefinedFieldInspection */
141 $this->itemsPerPage
= WCF
::getUser()->conversationMessagesPerPage
;
144 $this->canonicalURL
= LinkHandler
::getInstance()->getLink('Conversation', [
145 'object' => $this->conversation
146 ], ($this->pageNo ?
'pageNo=' . $this->pageNo
: ''));
152 protected function initObjectList() {
153 parent
::initObjectList();
155 $this->objectList
->getConditionBuilder()->add('conversation_message.conversationID = ?', [$this->conversation
->conversationID
]);
156 $this->objectList
->setConversation($this->conversation
->getDecoratedObject());
158 // handle visibility filter
159 if ($this->conversation
->joinedAt
> 0) $this->objectList
->getConditionBuilder()->add('conversation_message.time >= ?', [$this->conversation
->joinedAt
]);
160 if ($this->conversation
->leftAt
> 0) $this->objectList
->getConditionBuilder()->add('conversation_message.time <= ?', [$this->conversation
->leftAt
]);
163 if ($this->action
== 'lastPost') $this->goToLastPost();
164 if ($this->action
== 'firstNew') $this->goToFirstNewPost();
165 if ($this->messageID
) $this->goToPost();
171 public function readData() {
175 if ($this->conversation
->isDraft
) {
176 // `-1` = pseudo object id to have to pages with identifier `com.woltlab.wcf.conversation.ConversationList`
177 PageLocationManager
::getInstance()->addParentLocation('com.woltlab.wcf.conversation.ConversationList', -1, new ParentPageLocation(
178 WCF
::getLanguage()->get('wcf.conversation.folder.draft'),
179 LinkHandler
::getInstance()->getLink('ConversationList', ['filter' => 'draft'])
182 PageLocationManager
::getInstance()->addParentLocation('com.woltlab.wcf.conversation.ConversationList');
184 // update last visit time count
185 if ($this->conversation
->isNew() && $this->objectList
->getMaxPostTime() > $this->conversation
->lastVisitTime
) {
186 $visitTime = $this->objectList
->getMaxPostTime();
187 if ($visitTime == $this->conversation
->lastPostTime
) $visitTime = TIME_NOW
;
188 $conversationAction = new ConversationAction([$this->conversation
->getDecoratedObject()], 'markAsRead', ['visitTime' => $visitTime]);
189 $conversationAction->executeAction();
193 $this->participantList
= new ConversationParticipantList($this->conversationID
, WCF
::getUser()->userID
, $this->conversation
->userID
== WCF
::getUser()->userID
);
194 $this->participantList
->readObjects();
196 // init quote objects
198 foreach ($this->objectList
as $message) {
199 $messageIDs[] = $message->messageID
;
201 MessageQuoteManager
::getInstance()->initObjects('com.woltlab.wcf.conversation.message', $messageIDs);
203 // fetch special trophies
206 foreach ($this->objectList
as $message) {
207 if ($message->userID
) {
208 $userIDs[] = $message->userID
;
212 if (!empty($userIDs)) {
213 UserProfile
::prepareSpecialTrophies(array_unique($userIDs));
217 // set attachment permissions
218 if ($this->objectList
->getAttachmentList() !== null) {
219 $this->objectList
->getAttachmentList()->setPermissions([
220 'canDownload' => true,
221 'canViewPreview' => true
225 // get timeframe for modifications
226 $this->objectList
->rewind();
227 $startTime = ($this->conversation
->joinedAt ?
: $this->objectList
->current()->time
);
228 $endTime = ($this->conversation
->leftAt ?
: TIME_NOW
);
230 $count = count($this->objectList
);
232 $this->objectList
->seek($count - 1);
233 if ($this->objectList
->current()->time
< $this->conversation
->lastPostTime
) {
235 FROM wcf".WCF_N
."_conversation_message
236 WHERE conversationID = ?
239 $statement = WCF
::getDB()->prepareStatement($sql, 1);
240 $statement->execute([$this->conversationID
, $this->objectList
->current()->time
]);
241 $endTime = $statement->fetchSingleColumn() - 1;
244 $this->objectList
->rewind();
246 // get invisible participants
247 $invisibleParticipantIDs = [];
248 if (WCF
::getUser()->userID
!= $this->conversation
->userID
) {
249 foreach ($this->participantList
as $participant) {
250 if ($participant->isInvisible
) {
251 $invisibleParticipantIDs[] = $participant->userID
;
256 // load modification log entries
257 $this->modificationLogList
= new ConversationLogModificationLogList($this->conversation
->conversationID
);
258 $this->modificationLogList
->getConditionBuilder()->add("modification_log.time BETWEEN ? AND ?", [$startTime, $endTime]);
260 if (!empty($invisibleParticipantIDs)) {
261 $this->modificationLogList
->getConditionBuilder()->add("(modification_log.action <> ? OR modification_log.userID NOT IN (?))", ['leave', $invisibleParticipantIDs]);
264 $this->modificationLogList
->readObjects();
270 public function assignVariables() {
271 parent
::assignVariables();
273 MessageQuoteManager
::getInstance()->assignVariables();
275 $tmpHash = StringUtil
::getRandomID();
276 $attachmentHandler = new AttachmentHandler('com.woltlab.wcf.conversation.message', 0, $tmpHash, 0);
278 WCF
::getTPL()->assign([
279 'attachmentHandler' => $attachmentHandler,
280 'attachmentObjectID' => 0,
281 'attachmentObjectType' => 'com.woltlab.wcf.conversation.message',
282 'attachmentParentObjectID' => 0,
283 'tmpHash' => $tmpHash,
284 'attachmentList' => $this->objectList
->getAttachmentList(),
285 'labelList' => $this->labelList
,
286 'modificationLogList' => $this->modificationLogList
,
287 'sortOrder' => $this->sortOrder
,
288 'conversation' => $this->conversation
,
289 'conversationID' => $this->conversationID
,
290 'participants' => $this->participantList
->getObjects(),
291 'defaultSmilies' => SmileyCache
::getInstance()->getCategorySmilies()
294 BBCodeHandler
::getInstance()->setDisallowedBBCodes(explode(',', WCF
::getSession()->getPermission('user.message.disallowedBBCodes')));
298 * Calculates the position of a specific post in this conversation.
300 protected function goToPost() {
301 $conditionBuilder = clone $this->objectList
->getConditionBuilder();
302 $conditionBuilder->add('time '.($this->sortOrder
== 'ASC' ?
'<=' : '>=').' ?', [$this->message
->time
]);
304 $sql = "SELECT COUNT(*) AS messages
305 FROM wcf".WCF_N
."_conversation_message conversation_message
307 $statement = WCF
::getDB()->prepareStatement($sql);
308 $statement->execute($conditionBuilder->getParameters());
309 $row = $statement->fetchArray();
310 $this->pageNo
= intval(ceil($row['messages'] / $this->itemsPerPage
));
314 * Gets the id of the last post in this conversation and forwards the user to this post.
316 protected function goToLastPost() {
317 $sql = "SELECT conversation_message.messageID
318 FROM wcf".WCF_N
."_conversation_message conversation_message
319 ".$this->objectList
->getConditionBuilder()."
320 ORDER BY time ".($this->sortOrder
== 'ASC' ?
'DESC' : 'ASC');
321 $statement = WCF
::getDB()->prepareStatement($sql, 1);
322 $statement->execute($this->objectList
->getConditionBuilder()->getParameters());
323 $row = $statement->fetchArray();
324 HeaderUtil
::redirect(LinkHandler
::getInstance()->getLink('Conversation', [
325 'encodeTitle' => true,
326 'object' => $this->conversation
,
327 'messageID' => $row['messageID']
328 ]).'#message'.$row['messageID']);
333 * Forwards the user to the first new message in this conversation.
335 protected function goToFirstNewPost() {
336 $conditionBuilder = clone $this->objectList
->getConditionBuilder();
337 $conditionBuilder->add('time > ?', [$this->conversation
->lastVisitTime
]);
339 $sql = "SELECT conversation_message.messageID
340 FROM wcf".WCF_N
."_conversation_message conversation_message
341 ".$conditionBuilder."
343 $statement = WCF
::getDB()->prepareStatement($sql, 1);
344 $statement->execute($conditionBuilder->getParameters());
345 $row = $statement->fetchArray();
346 if ($row !== false) {
347 HeaderUtil
::redirect(LinkHandler
::getInstance()->getLink('Conversation', [
348 'encodeTitle' => true,
349 'object' => $this->conversation
,
350 'messageID' => $row['messageID']
351 ]).'#message'.$row['messageID']);
355 $this->goToLastPost();