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

192 lines
5.1 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\package\PackageDependencyHandler;
use \wcf\system\user\storage\UserStorageHandler;
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');
2011-11-28 20:40:37 +00:00
public $joinMessage = null;
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,
2011-11-28 20:40:37 +00:00
'joinMessage' => $this->joinMessage,
'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->readUserData();
2011-12-10 17:05:21 +00:00
if (CHAT_DISPLAY_JOIN_LEAVE) {
$this->joinMessage = chat\message\ChatMessageEditor::create(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-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();
if ($this->action == 'Log') {
//TODO: Initialise LogPage
exit;
}
elseif($this->action == 'Send') {
//TODO: Safe message in database
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(
'object' => $this->rooms->search($this->rooms->key())
)));
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();
}
/**
* Reads user data.
*/
public function readUserData() {
$ush = UserStorageHandler::getInstance();
$packageID = PackageDependencyHandler::getPackageID('timwolla.wcf.chat');
// load storage
$ush->loadStorage(array(WCF::getUser()->userID), $packageID);
$data = $ush->getStorage(array(WCF::getUser()->userID), 'color', $packageID);
if ($data[WCF::getUser()->userID] === null) {
// set defaults
$data[WCF::getUser()->userID] = array(1 => 0xFF0000, 2 => 0x00FF00); // TODO: Change default values
$ush->update(WCF::getUser()->userID, 'color', serialize($data[WCF::getUser()->userID]), $packageID);
}
else {
// load existing data
$data[WCF::getUser()->userID] = unserialize($data[WCF::getUser()->userID]);
}
$this->userData['color'] = $data[WCF::getUser()->userID];
}
/**
* @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();
if ($this->useTemplate) exit;
2011-12-03 16:37:20 +00:00
@header('Content-type: application/json');
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
}