1
0
mirror of https://github.com/wbbaddons/Tims-Chat.git synced 2025-01-23 02:10:41 +00:00
Tims-Chat/file/lib/data/chat/room/ChatRoom.class.php

199 lines
5.0 KiB
PHP
Raw Normal View History

2011-11-26 16:47:28 +01:00
<?php
namespace wcf\data\chat\room;
2012-05-19 21:25:50 +02:00
use \wcf\data\chat\suspension\ChatSuspension;
2011-11-26 17:37:46 +01:00
use \wcf\system\cache\CacheHandler;
2012-03-01 22:17:40 +01:00
use \wcf\system\WCF;
2011-11-26 16:47:28 +01:00
/**
* Represents a chat room.
*
* @author Tim Düsterhus
2012-01-28 17:50:33 +01:00
* @copyright 2010-2012 Tim Düsterhus
2011-11-26 16:47:28 +01:00
* @license Creative Commons Attribution-NonCommercial-ShareAlike <http://creativecommons.org/licenses/by-nc-sa/3.0/legalcode>
* @package be.bastelstu.wcf.chat
2011-11-26 16:47:28 +01:00
* @subpackage data.chat.room
*/
class ChatRoom extends \wcf\data\DatabaseObject implements \wcf\system\request\IRouteController {
2011-11-26 16:47:28 +01:00
/**
* @see \wcf\data\DatabaseObject::$databaseTableName
2011-11-26 16:47:28 +01:00
*/
protected static $databaseTableName = 'chat_room';
/**
* @see \wcf\data\DatabaseObject::$databaseTableIndexName
2011-11-26 16:47:28 +01:00
*/
protected static $databaseTableIndexName = 'roomID';
2012-03-22 18:45:36 +01:00
2011-11-26 16:47:28 +01:00
/**
* 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();
}
2012-03-24 21:47:03 +01:00
/**
* Returns whether the user is allowed to enter the room.
*
* @return boolean
*/
public function canEnter(\wcf\data\user\User $user = null) {
if ($user === null) $user = WCF::getUser();
$ph = new \wcf\system\chat\permission\ChatPermissionHandler($user);
$suspensions = ChatSuspension::getSuspensionsForUser($user);
2012-03-24 21:47:03 +01:00
2012-05-19 21:25:50 +02:00
$canEnter = $ph->getPermission($this, 'user.canEnter');
2012-06-04 21:10:03 +02:00
// room suspension
if ($canEnter && isset($suspensions[$this->roomID][ChatSuspension::TYPE_BAN])) {
2012-05-19 21:25:50 +02:00
if ($suspensions[$this->roomID][ChatSuspension::TYPE_BAN]['time'] > TIME_NOW) {
$canEnter = false;
}
}
2012-06-04 21:10:03 +02:00
// global suspension
if ($canEnter && isset($suspensions[null][ChatSuspension::TYPE_BAN])) {
if ($suspensions[null][ChatSuspension::TYPE_BAN]['time'] > TIME_NOW) {
$canEnter = false;
}
}
2012-05-19 21:25:50 +02:00
return $canEnter || $ph->getPermission($this, 'mod.canAlwaysEnter');
2012-03-24 21:47:03 +01:00
}
/**
* Returns whether the user is allowed to write messages in this room.
*
* @return boolean
*/
public function canWrite(\wcf\data\user\User $user = null) {
if ($user === null) $user = WCF::getUser();
$ph = new \wcf\system\chat\permission\ChatPermissionHandler($user);
$suspensions = ChatSuspension::getSuspensionsForUser($user);
2012-03-24 21:47:03 +01:00
2012-05-19 21:25:50 +02:00
$canWrite = $ph->getPermission($this, 'user.canWrite');
2012-06-04 21:10:03 +02:00
// room suspension
if ($canWrite && isset($suspensions[$this->roomID][ChatSuspension::TYPE_MUTE])) {
2012-05-19 21:25:50 +02:00
if ($suspensions[$this->roomID][ChatSuspension::TYPE_MUTE]['time'] > TIME_NOW) {
$canWrite = false;
}
}
2012-06-04 21:10:03 +02:00
// global suspension
if ($canWrite && isset($suspensions[null][ChatSuspension::TYPE_MUTE])) {
if ($suspensions[null][ChatSuspension::TYPE_MUTE]['time'] > TIME_NOW) {
$canWrite = false;
}
}
2012-05-19 21:25:50 +02:00
return $canWrite || $ph->getPermission($this, 'mod.canAlwaysWrite');
2012-03-24 21:47:03 +01:00
}
/**
* Returns the number of users currently active in this room.
*
* @return integer
*/
public function countUsers() {
$sql = "SELECT
2012-03-22 18:45:36 +01:00
COUNT(*)
FROM
wcf".WCF_N."_user_storage
WHERE
2012-03-22 18:45:36 +01:00
field = ?
AND packageID = ?
AND fieldValue = ?";
$stmt = WCF::getDB()->prepareStatement($sql);
2012-03-22 18:45:36 +01:00
$stmt->execute(array('roomID', \wcf\util\ChatUtil::getPackageID(), $this->roomID));
2012-03-22 18:45:36 +01:00
return $stmt->fetchColumn();
}
2011-11-26 16:47:28 +01:00
/**
* Loads the room cache.
*/
2011-11-26 17:37:46 +01: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 17:37:46 +01:00
);
self::$cache = CacheHandler::getInstance()->get('chatrooms');
}
return self::$cache;
2011-11-26 16:47:28 +01:00
}
/**
* Returns the ID of this chat-room.
*
* @see \wcf\system\request\IRouteController
*/
public function getID() {
return $this->roomID;
}
2011-11-26 16:47:28 +01:00
/**
* Returns the name of this chat-room.
*
* @see \wcf\system\request\IRouteController
2011-11-26 16:47:28 +01:00
*/
public function getTitle() {
2011-12-11 23:29:43 +01:00
return \wcf\system\WCF::getLanguage()->get($this->title);
2011-11-26 16:47:28 +01:00
}
/**
* Returns the users that are currently active in this room.
*
* @return array<\wcf\data\user\User>
*/
public function getUsers() {
$packageID = \wcf\util\ChatUtil::getPackageID();
2012-03-22 18:45:36 +01:00
$sql = "SELECT
userID
FROM
wcf".WCF_N."_user_storage
WHERE
2012-03-22 18:45:36 +01:00
field = ?
AND packageID = ?
AND fieldValue = ?";
$stmt = WCF::getDB()->prepareStatement($sql);
2012-03-22 18:45:36 +01:00
$stmt->execute(array('roomID', $packageID, $this->roomID));
$userIDs = array();
2012-03-22 18:45:36 +01:00
while ($userIDs[] = $stmt->fetchColumn());
if (!count($userIDs)) return array();
2012-03-22 18:45:36 +01:00
$sql = "SELECT
2012-03-22 18:45:36 +01:00
u.*,
st.fieldValue AS awayStatus
FROM
2012-03-22 18:45:36 +01:00
wcf".WCF_N."_user u
LEFT JOIN
2012-05-19 21:37:15 +02:00
wcf".WCF_N."_user_storage st
2012-03-22 18:45:36 +01:00
ON (
2012-05-19 21:37:15 +02:00
u.userID = st.userID
AND st.field = ?
AND st.packageID = ?
)
WHERE
u.userID IN (".rtrim(str_repeat('?,', count($userIDs)), ',').")
ORDER BY
2012-03-22 18:45:36 +01:00
u.username ASC";
$stmt = WCF::getDB()->prepareStatement($sql);
array_unshift($userIDs, 'away', $packageID);
$stmt->execute($userIDs);
2012-03-22 18:45:36 +01:00
return $stmt->fetchObjects('\wcf\data\user\User');
}
2011-11-26 16:47:28 +01:00
}