Add access checks for notifications events
[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\message\ConversationMessage;
5 use wcf\data\conversation\Conversation;
6 use wcf\data\conversation\ConversationAction;
7 use wcf\data\conversation\ConversationParticipantList;
8 use wcf\data\conversation\ViewableConversation;
9 use wcf\data\modification\log\ConversationLogModificationLogList;
10 use wcf\data\smiley\SmileyCache;
11 use wcf\system\attachment\AttachmentHandler;
12 use wcf\system\bbcode\BBCodeHandler;
13 use wcf\system\breadcrumb\Breadcrumb;
14 use wcf\system\exception\IllegalLinkException;
15 use wcf\system\exception\PermissionDeniedException;
16 use wcf\system\message\quote\MessageQuoteManager;
17 use wcf\system\request\LinkHandler;
18 use wcf\system\WCF;
19 use wcf\util\HeaderUtil;
20 use wcf\util\StringUtil;
21
22 /**
23 * Shows a conversation.
24 *
25 * @author Marcel Werk
26 * @copyright 2001-2014 WoltLab GmbH
27 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
28 * @package com.woltlab.wcf.conversation
29 * @subpackage page
30 * @category Community Framework
31 */
32 class ConversationPage extends MultipleLinkPage {
33 /**
34 * @see \wcf\page\AbstractPage::$enableTracking
35 */
36 public $enableTracking = true;
37
38 /**
39 * @see \wcf\page\MultipleLinkPage::$itemsPerPage
40 */
41 public $itemsPerPage = CONVERSATION_MESSAGES_PER_PAGE;
42
43 /**
44 * @see \wcf\page\MultipleLinkPage::$sortOrder
45 */
46 public $sortOrder = 'ASC';
47
48 /**
49 * @see \wcf\page\MultipleLinkPage::$objectListClassName
50 */
51 public $objectListClassName = 'wcf\data\conversation\message\ViewableConversationMessageList';
52
53 /**
54 * @see \wcf\page\AbstractPage::$loginRequired
55 */
56 public $loginRequired = true;
57
58 /**
59 * @see \wcf\page\AbstractPage::$neededModules
60 */
61 public $neededModules = array('MODULE_CONVERSATION');
62
63 /**
64 * @see \wcf\page\AbstractPage::$neededPermissions
65 */
66 public $neededPermissions = array('user.conversation.canUseConversation');
67
68 /**
69 * conversation id
70 * @var integer
71 */
72 public $conversationID = 0;
73
74 /**
75 * viewable conversation object
76 * @var \wcf\data\conversation\ViewableConversation
77 */
78 public $conversation = null;
79
80 /**
81 * conversation label list
82 * @var \wcf\data\conversation\label\ConversationLabelList
83 */
84 public $labelList = null;
85
86 /**
87 * message id
88 * @var integer
89 */
90 public $messageID = 0;
91
92 /**
93 * conversation message object
94 * @var \wcf\data\conversation\message\ConversationMessage
95 */
96 public $message = null;
97
98 /**
99 * modification log list object
100 * @var \wcf\data\wcf\data\modification\log\ConversationLogModificationLogList
101 */
102 public $modificationLogList = null;
103
104 /**
105 * list of participants
106 * @var \wcf\data\conversation\ConversationParticipantList
107 */
108 public $participantList = null;
109
110 /**
111 * @see \wcf\page\IPage::readParameters()
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 // posts per page
139 if (WCF::getUser()->conversationMessagesPerPage) $this->itemsPerPage = WCF::getUser()->conversationMessagesPerPage;
140 }
141
142 /**
143 * @see \wcf\page\MultipleLinkPage::initObjectList()
144 */
145 protected function initObjectList() {
146 parent::initObjectList();
147
148 $this->objectList->getConditionBuilder()->add('conversation_message.conversationID = ?', array($this->conversation->conversationID));
149 $this->objectList->setConversation($this->conversation->getDecoratedObject());
150
151 // handle jump to
152 if ($this->action == 'lastPost') $this->goToLastPost();
153 if ($this->action == 'firstNew') $this->goToFirstNewPost();
154 if ($this->messageID) $this->goToPost();
155 }
156
157 /**
158 * @see \wcf\page\IPage::readData()
159 */
160 public function readData() {
161 parent::readData();
162
163 // add breadcrumbs
164 WCF::getBreadcrumbs()->add(new Breadcrumb(WCF::getLanguage()->get('wcf.conversation.conversations'), LinkHandler::getInstance()->getLink('ConversationList')));
165 if ($this->conversation->isDraft) {
166 WCF::getBreadcrumbs()->add(new Breadcrumb(WCF::getLanguage()->get('wcf.conversation.folder.draft'), LinkHandler::getInstance()->getLink('ConversationList', array(
167 'filter' => 'draft'
168 ))));
169 }
170
171 // update last visit time count
172 if ($this->conversation->isNew() && $this->objectList->getMaxPostTime() > $this->conversation->lastVisitTime) {
173 $visitTime = $this->objectList->getMaxPostTime();
174 if ($visitTime == $this->conversation->lastPostTime) $visitTime = TIME_NOW;
175 $conversationAction = new ConversationAction(array($this->conversation->getDecoratedObject()), 'markAsRead', array('visitTime' => $visitTime));
176 $conversationAction->executeAction();
177 }
178
179 // get participants
180 $this->participantList = new ConversationParticipantList($this->conversationID, WCF::getUser()->userID, ($this->conversation->userID == WCF::getUser()->userID));
181 $this->participantList->readObjects();
182
183 // init quote objects
184 $messageIDs = array();
185 foreach ($this->objectList as $message) {
186 $messageIDs[] = $message->messageID;
187 }
188 MessageQuoteManager::getInstance()->initObjects('com.woltlab.wcf.conversation.message', $messageIDs);
189
190 // set attachment permissions
191 if ($this->objectList->getAttachmentList() !== null) {
192 $this->objectList->getAttachmentList()->setPermissions(array(
193 'canDownload' => true,
194 'canViewPreview' => true
195 ));
196 }
197
198 // get timeframe for modifications
199 $this->objectList->rewind();
200 $startTime = $this->objectList->current()->time;
201
202 $count = count($this->objectList);
203 if ($count == 1) {
204 $endTime = $startTime;
205 }
206 else {
207 $this->objectList->seek($count - 1);
208 $endTime = $this->objectList->current()->time;
209 }
210 $this->objectList->rewind();
211
212 // load modification log entries
213 $this->modificationLogList = new ConversationLogModificationLogList();
214 $this->modificationLogList->setConversation($this->conversation->getDecoratedObject());
215 $this->modificationLogList->getConditionBuilder()->add("modification_log.time BETWEEN ? AND ?", array($startTime, $endTime));
216 $this->modificationLogList->readObjects();
217 }
218
219 /**
220 * @see \wcf\page\IPage::assignVariables()
221 */
222 public function assignVariables() {
223 parent::assignVariables();
224
225 MessageQuoteManager::getInstance()->assignVariables();
226
227 $tmpHash = StringUtil::getRandomID();
228 $attachmentHandler = new AttachmentHandler('com.woltlab.wcf.conversation.message', 0, $tmpHash, 0);
229
230 WCF::getTPL()->assign(array(
231 'attachmentHandler' => $attachmentHandler,
232 'attachmentObjectID' => 0,
233 'attachmentObjectType' => 'com.woltlab.wcf.conversation.message',
234 'attachmentParentObjectID' => 0,
235 'tmpHash' => $tmpHash,
236 'attachmentList' => $this->objectList->getAttachmentList(),
237 'labelList' => $this->labelList,
238 'modificationLogList' => $this->modificationLogList,
239 'sortOrder' => $this->sortOrder,
240 'conversation' => $this->conversation,
241 'conversationID' => $this->conversationID,
242 'participants' => $this->participantList->getObjects(),
243 'defaultSmilies' => SmileyCache::getInstance()->getCategorySmilies(),
244 'permissionCanUseSmilies' => 'user.message.canUseSmilies'
245 ));
246
247 BBCodeHandler::getInstance()->setAllowedBBCodes(explode(',', WCF::getSession()->getPermission('user.message.allowedBBCodes')));
248 }
249
250 /**
251 * Calculates the position of a specific post in this conversation.
252 */
253 protected function goToPost() {
254 $conditionBuilder = clone $this->objectList->getConditionBuilder();
255 $conditionBuilder->add('time '.($this->sortOrder == 'ASC' ? '<=' : '>=').' ?', array($this->message->time));
256
257 $sql = "SELECT COUNT(*) AS messages
258 FROM wcf".WCF_N."_conversation_message conversation_message
259 ".$conditionBuilder;
260 $statement = WCF::getDB()->prepareStatement($sql);
261 $statement->execute($conditionBuilder->getParameters());
262 $row = $statement->fetchArray();
263 $this->pageNo = intval(ceil($row['messages'] / $this->itemsPerPage));
264 }
265
266 /**
267 * Gets the id of the last post in this conversation and forwards the user to this post.
268 */
269 protected function goToLastPost() {
270 $sql = "SELECT conversation_message.messageID
271 FROM wcf".WCF_N."_conversation_message conversation_message
272 ".$this->objectList->getConditionBuilder()."
273 ORDER BY time ".($this->sortOrder == 'ASC' ? 'DESC' : 'ASC');
274 $statement = WCF::getDB()->prepareStatement($sql, 1);
275 $statement->execute($this->objectList->getConditionBuilder()->getParameters());
276 $row = $statement->fetchArray();
277 HeaderUtil::redirect(LinkHandler::getInstance()->getLink('Conversation', array(
278 'object' => $this->conversation,
279 'messageID' => $row['messageID']
280 )).'#message'.$row['messageID']);
281 exit;
282 }
283
284 /**
285 * Forwards the user to the first new message in this conversation.
286 */
287 protected function goToFirstNewPost() {
288 $conditionBuilder = clone $this->objectList->getConditionBuilder();
289 $conditionBuilder->add('time > ?', array($this->conversation->lastVisitTime));
290
291 $sql = "SELECT conversation_message.messageID
292 FROM wcf".WCF_N."_conversation_message conversation_message
293 ".$conditionBuilder."
294 ORDER BY time ASC";
295 $statement = WCF::getDB()->prepareStatement($sql, 1);
296 $statement->execute($conditionBuilder->getParameters());
297 $row = $statement->fetchArray();
298 if ($row !== false) {
299 HeaderUtil::redirect(LinkHandler::getInstance()->getLink('Conversation', array(
300 'object' => $this->conversation,
301 'messageID' => $row['messageID']
302 )).'#message'.$row['messageID']);
303 exit;
304 }
305 else {
306 $this->goToLastPost();
307 }
308 }
309
310 /**
311 * @see \wcf\page\ITrackablePage::getObjectType()
312 */
313 public function getObjectType() {
314 return 'com.woltlab.wcf.conversation';
315 }
316
317 /**
318 * @see \wcf\page\ITrackablePage::getObjectID()
319 */
320 public function getObjectID() {
321 return $this->conversationID;
322 }
323 }