1
0
mirror of https://github.com/wbbaddons/Tims-Chat.git synced 2024-11-01 14:20:07 +00:00
Tims-Chat/file/lib/page/ChatPage.class.php

181 lines
4.8 KiB
PHP
Raw Normal View History

2011-11-26 14:17:17 +00:00
<?php
namespace wcf\page;
2011-11-26 16:37:46 +00:00
use \wcf\data\chat;
use \wcf\system\cache\CacheHandler;
use \wcf\system\WCF;
2011-11-26 14:17:17 +00:00
/**
* Shows the chat-interface
*
* @author Tim Düsterhus
* @copyright 2010-2011 Tim Düsterhus
* @license Creative Commons Attribution-NonCommercial-ShareAlike <http://creativecommons.org/licenses/by-nc-sa/3.0/legalcode>
* @package timwolla.wcf.chat
* @subpackage page
*/
class ChatPage extends AbstractPage {
public $chatVersion = '';
2011-12-10 15:56:36 +00:00
public $neededModules = array('CHAT_ACTIVE');
//public $neededPermissions = array('user.chat.canEnter');
public $newestMessages = array();
2011-11-26 16:37:46 +00:00
public $room = null;
public $roomID = 0;
public $rooms = array();
2011-11-27 12:07:53 +00:00
public $smilies = array();
public $userData = array();
2011-11-26 14:29:28 +00:00
/**
* @see \wcf\page\IPage::assignVariables()
*/
public function assignVariables() {
parent::assignVariables();
WCF::getTPL()->assign(array(
'chatVersion' => $this->chatVersion,
'newestMessages' => $this->newestMessages,
'room' => $this->room,
'roomID' => $this->roomID,
2011-11-27 12:07:53 +00:00
'rooms' => $this->rooms,
'smilies' => $this->smilies
));
}
2011-11-27 16:22:07 +00:00
/**
* Reads chat-version. Used to avoid caching of JS-File when Tims Chat is updated.
*/
public function readChatVersion() {
CacheHandler::getInstance()->addResource(
'packages',
WCF_DIR.'cache/cache.packages.php',
'wcf\system\cache\builder\PackageCacheBuilder'
);
$packages = CacheHandler::getInstance()->get('packages');
foreach ($packages as $package) {
if ($package->package != 'timwolla.wcf.chat') continue;
$this->chatVersion = $package->packageVersion;
2011-11-27 16:22:07 +00:00
return;
}
}
/**
* @see \wcf\page\IPage::readData()
*/
public function readData() {
parent::readData();
$this->readRoom();
$this->userData['color'] = \wcf\util\ChatUtil::readUserData('color');
\wcf\util\ChatUtil::writeUserData(array('roomID' => $this->room->roomID));
2011-12-10 17:05:21 +00:00
if (CHAT_DISPLAY_JOIN_LEAVE) {
2011-12-13 21:35:11 +00:00
$messageAction = new chat\message\ChatMessageAction(array(), 'create', array(
'data' => array(
'roomID' => $this->room->roomID,
'sender' => WCF::getUser()->userID,
'username' => WCF::getUser()->username,
'time' => TIME_NOW,
'type' => chat\message\ChatMessage::TYPE_JOIN,
'message' => '',
'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();
$return = $messageAction->getReturnValues();
\wcf\util\ChatUtil::writeUserData(array('lastSeen' => $return['returnValues'] -> messageID));
2011-12-10 17:05:21 +00:00
}
2011-11-27 12:07:53 +00:00
$this->readDefaultSmileys();
$this->readChatVersion();
2011-12-10 17:05:21 +00:00
$this->newestMessages = chat\message\ChatMessageList::getNewestMessages($this->room, CHAT_LASTMESSAGES);
}
/**
* Reads the smilies in the default category.
*/
public function readDefaultSmileys() {
$smilies = \wcf\data\smiley\SmileyCache::getInstance()->getSmilies();
$this->smilies = $smilies[null];
}
/**
* @see \wcf\page\IPage::readParameters()
*/
public function readParameters() {
parent::readParameters();
2011-12-26 16:01:24 +00:00
if ($this->action == 'Message') {
new ChatMessagePage();
exit;
}
else if ($this->action == 'Log') {
//TODO: Initialise LogPage
exit;
}
else if ($this->action == 'RefreshRoomList') {
new ChatRefreshRoomListPage();
exit;
}
else if ($this->action == 'Send') {
2011-12-13 21:10:35 +00:00
new \wcf\form\ChatForm();
exit;
}
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() {
$this->rooms = chat\room\ChatRoom::getCache();
if ($this->roomID === 0) {
// no room given
try {
// redirect to first chat-room
$this->rooms->seek(0);
\wcf\util\HeaderUtil::redirect(\wcf\system\request\LinkHandler::getInstance()->getLink('Chat', array(
2011-12-19 15:14:27 +00:00
'object' => $this->rooms->current()
)));
exit;
}
catch (\OutOfBoundsException $e) {
// no valid room found
throw new \wcf\system\exception\IllegalLinkException();
}
}
$this->room = $this->rooms->search($this->roomID);
if (!$this->room) throw new \wcf\system\exception\IllegalLinkException();
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() {
2011-11-27 16:22:07 +00:00
// guests are not supported
if (!WCF::getUser()->userID) {
throw new \wcf\system\exception\PermissionDeniedException();
}
2011-11-26 14:29:28 +00:00
\wcf\system\menu\page\PageMenu::getInstance()->setActiveMenuItem('wcf.header.menu.chat');
// remove index breadcrumb
WCF::getBreadcrumbs()->remove(0);
2011-11-26 14:29:28 +00:00
parent::show();
// break if not ajax
if ($this->useTemplate) exit;
2011-12-03 16:37:20 +00:00
@header('Content-type: application/json');
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(),
2011-12-03 16:37:20 +00:00
'topic' => WCF::getLanguage()->get($this->room->topic)
));
exit;
2011-11-26 14:29:28 +00:00
}
2011-11-26 14:17:17 +00:00
}