1
0
mirror of https://github.com/wbbaddons/Tims-Chat.git synced 2024-12-22 21:40:08 +00:00
Tims-Chat/file/lib/page/ChatPage.class.php

246 lines
6.4 KiB
PHP
Raw Normal View History

2011-11-26 14:17:17 +00:00
<?php
2013-01-19 19:36:40 +00:00
namespace chat\page;
use \chat\data;
use \wcf\system\WCF;
2011-11-26 14:17:17 +00:00
/**
* Shows the chat-interface
*
* @author Tim Düsterhus
2013-01-19 19:36:40 +00:00
* @copyright 2010-2013 Tim Düsterhus
2011-11-26 14:17:17 +00:00
* @license Creative Commons Attribution-NonCommercial-ShareAlike <http://creativecommons.org/licenses/by-nc-sa/3.0/legalcode>
2013-01-19 19:36:40 +00:00
* @package be.bastelstu.chat
2011-11-26 14:17:17 +00:00
* @subpackage page
*/
2013-01-19 19:36:40 +00:00
class ChatPage extends \wcf\page\AbstractPage {
2012-08-08 14:51:24 +00:00
/**
* @see wcf\page\AbstractPage::$loginRequired
*/
public $loginRequired = true;
/**
* @see \wcf\page\AbstractPage::$neededModules
*/
public $neededModules = array('MODULE_CHAT');
/**
* @see \wcf\page\AbstractPage::$neededPermissions
*/
public $neededPermissions = array();
2012-06-04 18:30:08 +00:00
/**
* The last X messages for the current room.
*
2013-01-19 19:36:40 +00:00
* @var array<\chat\data\message\Message>
2012-06-04 18:30:08 +00:00
*/
public $newestMessages = array();
2012-06-04 18:30:08 +00:00
/**
* The current room.
*
2013-01-19 19:36:40 +00:00
* @var \chat\data\room\Room
2012-06-04 18:30:08 +00:00
*/
2011-11-26 16:37:46 +00:00
public $room = null;
2012-06-04 18:30:08 +00:00
/**
* The given roomID.
*
* @var integer
*/
2011-11-26 16:37:46 +00:00
public $roomID = 0;
2012-06-04 18:30:08 +00:00
/**
* List of accessible rooms.
*
2013-01-19 19:36:40 +00:00
* @var \chat\data\room\RoomList
2012-06-04 18:30:08 +00:00
*/
2011-11-26 16:37:46 +00:00
public $rooms = array();
2012-06-04 18:30:08 +00:00
/**
* List of smilies in the default category.
*
* @var array<\wcf\data\smiley\Smiley>
* @see \wcf\data\smiley\SmileyCache
*/
public $defaultSmilies = array();
/**
* List of all smiley categories.
*
* @var array<\wcf\data\smiley\SmileyCategory>
* @see \wcf\data\smiley\SmileyCache
*/
public $smileyCategories = array();
2012-06-04 18:30:08 +00:00
/**
* Values read from the UserStorage of the current user.
*
* @var array
*/
public $userData = array();
2013-05-10 15:51:48 +00:00
/**
* @see wcf\page\AbstractPage::$enableTracking
*/
public $enableTracking = true;
2011-11-26 14:29:28 +00:00
/**
* @see \wcf\page\IPage::assignVariables()
*/
public function assignVariables() {
parent::assignVariables();
WCF::getTPL()->assign(array(
'newestMessages' => $this->newestMessages,
'room' => $this->room,
'roomID' => $this->roomID,
2011-11-27 12:07:53 +00:00
'rooms' => $this->rooms,
'defaultSmilies' => $this->defaultSmilies,
'smileyCategories' => $this->smileyCategories,
2013-01-19 19:36:40 +00:00
'sidebarCollapsed' => \wcf\system\user\collapsible\content\UserCollapsibleContentHandler::getInstance()->isCollapsed('com.woltlab.wcf.collapsibleSidebar', 'be.bastelstu.chat.ChatPage'),
'sidebarName' => 'be.bastelstu.chat.ChatPage'
));
}
/**
* @see \wcf\page\IPage::readData()
*/
public function readData() {
parent::readData();
$this->readRoom();
2013-01-19 19:36:40 +00:00
$this->userData['color'] = \chat\util\ChatUtil::readUserData('color');
\chat\util\ChatUtil::writeUserData(array(
2012-04-15 19:37:20 +00:00
'roomID' => $this->room->roomID,
'away' => null,
'lastActivity' => TIME_NOW
));
2011-12-10 17:05:21 +00:00
if (CHAT_DISPLAY_JOIN_LEAVE) {
2013-01-19 19:36:40 +00:00
$messageAction = new data\message\MessageAction(array(), 'create', array(
2011-12-13 21:35:11 +00:00
'data' => array(
'roomID' => $this->room->roomID,
'sender' => WCF::getUser()->userID,
'username' => WCF::getUser()->username,
'time' => TIME_NOW,
'type' => \chat\data\message\Message::TYPE_JOIN,
2012-08-28 19:53:21 +00:00
'message' => serialize(array('ipAddress' => \wcf\util\UserUtil::convertIPv6To4(\wcf\util\UserUtil::getIpAddress()))),
2011-12-13 21:35:11 +00:00
'color1' => $this->userData['color'][1],
'color2' => $this->userData['color'][2]
)
2011-12-10 17:05:21 +00:00
));
2011-12-13 21:35:11 +00:00
$messageAction->executeAction();
2012-06-04 18:30:08 +00:00
$messageAction->getReturnValues();
2011-12-10 17:05:21 +00:00
}
2011-11-27 12:07:53 +00:00
2013-01-19 19:36:40 +00:00
$this->newestMessages = data\message\MessageList::getNewestMessages($this->room, CHAT_LASTMESSAGES);
try {
2013-01-19 19:36:40 +00:00
\chat\util\ChatUtil::writeUserData(array('lastSeen' => end($this->newestMessages)->messageID));
}
catch (\wcf\system\exception\SystemException $e) {
2013-01-19 19:36:40 +00:00
\chat\util\ChatUtil::writeUserData(array('lastSeen' => 0));
}
2013-04-28 14:54:56 +00:00
// get default smilies
if (MODULE_SMILEY) {
$this->smileyCategories = \wcf\data\smiley\SmileyCache::getInstance()->getCategories();
foreach ($this->smileyCategories as $index => $category) {
$category->loadSmilies();
// remove empty categories
if (!count($category) || $category->isDisabled) {
unset($this->smileyCategories[$index]);
}
}
$firstCategory = reset($this->smileyCategories);
if ($firstCategory) {
$this->defaultSmilies = \wcf\data\smiley\SmileyCache::getInstance()->getCategorySmilies($firstCategory->categoryID ?: null);
}
}
}
/**
* @see \wcf\page\IPage::readParameters()
*/
public function readParameters() {
parent::readParameters();
2011-12-03 16:37:20 +00:00
if (isset($_REQUEST['id'])) $this->roomID = (int) $_REQUEST['id'];
if (isset($_REQUEST['ajax'])) $this->useTemplate = false;
}
2011-11-27 16:22:07 +00:00
/**
* Reads room data.
2011-11-27 16:22:07 +00:00
*/
public function readRoom() {
2013-01-19 19:36:40 +00:00
$this->rooms = data\room\Room::getCache();
if ($this->roomID === 0) {
// no room given
2012-09-07 20:37:25 +00:00
$room = reset($this->rooms);
if ($room === null) {
// no valid room found
throw new \wcf\system\exception\IllegalLinkException();
}
2012-09-07 20:37:25 +00:00
// redirect to first chat-room
\wcf\util\HeaderUtil::redirect(\wcf\system\request\LinkHandler::getInstance()->getLink('Chat', array(
'object' => $room
)));
exit;
}
2012-09-07 20:37:25 +00:00
if (!isset($this->rooms[$this->roomID])) throw new \wcf\system\exception\IllegalLinkException();
$this->room = $this->rooms[$this->roomID];
2011-12-26 13:14:03 +00:00
if (!$this->room->canEnter()) throw new \wcf\system\exception\PermissionDeniedException();
}
/**
* @see \wcf\page\IPage::show()
2011-11-26 14:29:28 +00:00
*/
public function show() {
2013-01-19 19:36:40 +00:00
\wcf\system\menu\page\PageMenu::getInstance()->setActiveMenuItem('chat.header.menu.chat');
// remove index breadcrumb
WCF::getBreadcrumbs()->remove(0);
2011-11-26 14:29:28 +00:00
parent::show();
2013-01-14 20:08:44 +00:00
// add activity points
2013-03-19 16:55:33 +00:00
$microtime = microtime(true) * 1000;
$result = $microtime & 0xFFFFFFFF;
if ($result > 0x7FFFFFFF) $result -= 0x80000000;
\wcf\system\user\activity\point\UserActivityPointHandler::getInstance()->fireEvent('be.bastelstu.chat.activityPointEvent.join', $result, WCF::getUser()->userID);
2013-01-14 20:08:44 +00:00
// break if not using ajax
2013-05-02 20:43:52 +00:00
\wcf\system\nodePush\NodePushHandler::getInstance()->sendMessage('be.bastelstu.chat.join');
if ($this->useTemplate) exit;
2011-12-03 16:37:20 +00:00
@header('Content-type: application/json');
$messages = array();
foreach ($this->newestMessages as $message) $messages[] = $message->jsonify(true);
2011-12-03 16:37:20 +00:00
echo \wcf\util\JSON::encode(array(
2011-12-10 20:55:34 +00:00
'title' => $this->room->getTitle(),
'topic' => WCF::getLanguage()->get($this->room->topic),
'messages' => $messages
2011-12-03 16:37:20 +00:00
));
exit;
2011-11-26 14:29:28 +00:00
}
2013-05-10 15:51:48 +00:00
/**
* @see wcf\page\ITrackablePage::getObjectType()
*/
public function getObjectType() {
return 'be.bastelstu.chat.room';
}
/**
* @see wcf\page\ITrackablePage::getObjectID()
*/
public function getObjectID() {
2013-05-23 20:40:47 +00:00
if ($this->room === null) return 0;
2013-05-10 15:51:48 +00:00
return $this->room->roomID;
}
2011-11-26 14:17:17 +00:00
}