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

144 lines
3.4 KiB
PHP
Raw Normal View History

2011-11-26 15:47:28 +00:00
<?php
namespace wcf\data\chat\room;
2011-11-26 16:37:46 +00:00
use \wcf\system\cache\CacheHandler;
2012-03-01 21:17:40 +00:00
use \wcf\system\WCF;
2011-11-26 15:47:28 +00:00
/**
* Represents a chat room.
*
* @author Tim Düsterhus
2012-01-28 16:50:33 +00:00
* @copyright 2010-2012 Tim Düsterhus
2011-11-26 15:47:28 +00:00
* @license Creative Commons Attribution-NonCommercial-ShareAlike <http://creativecommons.org/licenses/by-nc-sa/3.0/legalcode>
* @package timwolla.wcf.chat
* @subpackage data.chat.room
*/
class ChatRoom extends \wcf\data\DatabaseObject implements \wcf\system\request\IRouteController {
2011-11-26 15:47:28 +00:00
/**
* @see \wcf\data\DatabaseObject::$databaseTableName
2011-11-26 15:47:28 +00:00
*/
protected static $databaseTableName = 'chat_room';
/**
* @see \wcf\data\DatabaseObject::$databaseTableIndexName
2011-11-26 15:47:28 +00:00
*/
protected static $databaseTableIndexName = 'roomID';
/**
* Caches rooms.
*
* @var array<wcf\data\chat\room\ChatRoom>
*/
protected static $cache = null;
/**
* @see \wcf\data\chat\room\ChatRoom::getTitle();
*/
public function __toString() {
return $this->getTitle();
}
/**
* Returns the number of users currently active in this room.
*
* @return integer
*/
public function countUsers() {
$packageID = \wcf\system\package\PackageDependencyHandler::getInstance()->getPackageID('timwolla.wcf.chat');
$sql = "SELECT
count(*) as count
FROM
wcf".WCF_N."_user_storage
WHERE
field = 'roomID'
AND packageID = ".intval($packageID)."
AND fieldValue = ".intval($this->roomID);
$stmt = WCF::getDB()->prepareStatement($sql);
$stmt->execute();
$row = $stmt->fetchArray();
return $row['count'];
}
2011-11-26 15:47:28 +00:00
/**
* Loads the room cache.
*/
2011-11-26 16:37:46 +00:00
public static function getCache() {
if (self::$cache === null) {
CacheHandler::getInstance()->addResource(
'chatrooms',
WCF_DIR.'cache/cache.chatrooms.php',
'\wcf\system\cache\builder\ChatRoomCacheBuilder'
2011-11-26 16:37:46 +00:00
);
self::$cache = CacheHandler::getInstance()->get('chatrooms');
}
return self::$cache;
2011-11-26 15:47:28 +00:00
}
/**
* Returns the ID of this chat-room.
*
* @see \wcf\system\request\IRouteController
*/
public function getID() {
return $this->roomID;
}
2011-11-26 15:47:28 +00:00
/**
* Returns the name of this chat-room.
*
* @see \wcf\system\request\IRouteController
2011-11-26 15:47:28 +00:00
*/
public function getTitle() {
2011-12-11 22:29:43 +00:00
return \wcf\system\WCF::getLanguage()->get($this->title);
2011-11-26 15:47:28 +00:00
}
/**
* Returns the users that are currently active in this room.
*
* @return array<\wcf\data\user\User>
*/
public function getUsers() {
$packageID = \wcf\system\package\PackageDependencyHandler::getInstance()->getPackageID('timwolla.wcf.chat');
$sql = "SELECT
userID
FROM
wcf".WCF_N."_user_storage
WHERE
field = 'roomID'
AND packageID = ".intval($packageID)."
AND fieldValue = ".intval($this->roomID);
$stmt = WCF::getDB()->prepareStatement($sql);
$stmt->execute();
$userIDs = array();
while ($row = $stmt->fetchArray()) $userIDs[] = $row['userID'];
if (!count($userIDs)) return;
$sql = "SELECT
*
FROM
wcf".WCF_N."_user
WHERE
userID IN (".rtrim(str_repeat('?,', count($userIDs)), ',').")
ORDER BY
username ASC";
$stmt = WCF::getDB()->prepareStatement($sql);
$stmt->execute($userIDs);
return $stmt->fetchObjects('\wcf\data\user\User');
}
2011-12-19 15:22:56 +00:00
/**
* Returns whether the user is allowed to enter the room
*
* @return boolean
*/
public function canEnter() {
2012-03-03 21:00:54 +00:00
$ph = \wcf\system\chat\permission\ChatPermissionHandler::getInstance();
2012-03-04 12:42:04 +00:00
return $ph->getPermission($this, 'user.canEnter') || $ph->getPermission($this, 'mod.canAlwaysEnter');
2011-12-19 15:22:56 +00:00
}
2011-11-26 15:47:28 +00:00
}