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/room/RoomAction.class.php

211 lines
5.7 KiB
PHP
Raw Normal View History

2012-02-04 20:06:44 +00:00
<?php
2013-01-19 19:36:40 +00:00
namespace chat\data\room;
2013-04-26 22:38:53 +00:00
use \chat\util\ChatUtil;
2012-03-03 20:36:52 +00:00
use \wcf\system\WCF;
2012-02-04 20:06:44 +00:00
/**
* Executes chatroom-related actions.
*
* @author Tim Düsterhus
2013-01-19 19:36:40 +00:00
* @copyright 2010-2013 Tim Düsterhus
2012-02-04 20:06:44 +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
* @subpackage data.room
2012-02-04 20:06:44 +00:00
*/
2013-01-19 19:36:40 +00:00
class RoomAction extends \wcf\data\AbstractDatabaseObjectAction implements \wcf\data\ISortableAction {
2012-02-04 20:06:44 +00:00
/**
* @see \wcf\data\AbstractDatabaseObjectAction::$className
2012-02-04 20:06:44 +00:00
*/
2013-01-19 19:36:40 +00:00
protected $className = '\chat\data\room\RoomEditor';
2012-02-04 20:06:44 +00:00
/**
* @see \wcf\data\AbstractDatabaseObjectAction::$permissionsDelete
2012-02-04 20:06:44 +00:00
*/
2013-01-19 19:36:40 +00:00
protected $permissionsDelete = array('admin.chat.canDeleteRoom');
2012-02-04 20:06:44 +00:00
/**
* @see \wcf\data\AbstractDatabaseObjectAction::$permissionsUpdate
2012-02-04 20:06:44 +00:00
*/
2013-01-19 19:36:40 +00:00
protected $permissionsUpdate = array('admin.chat.canEditRoom');
2012-03-03 20:36:52 +00:00
2013-03-11 21:33:17 +00:00
/**
* Resets cache if any of the listed actions is invoked
* @var array<string>
*/
protected $resetCache = array('create', 'delete', 'toggle', 'update', 'updatePosition', 'prune');
2012-03-03 20:36:52 +00:00
/**
2012-03-15 19:11:01 +00:00
* Fixes create to append new rooms.
2012-03-03 20:36:52 +00:00
*/
public function create() {
$room = parent::create();
WCF::getDB()->beginTransaction();
2012-03-22 15:38:08 +00:00
$sql = "SELECT MAX(position)
2012-05-19 18:33:25 +00:00
FROM ".call_user_func(array($this->className, 'getDatabaseTableName'))."
2012-03-03 20:40:43 +00:00
FOR UPDATE";
2012-03-03 20:36:52 +00:00
$stmt = WCF::getDB()->prepareStatement($sql);
$stmt->execute();
2012-05-19 18:33:25 +00:00
2013-01-19 19:36:40 +00:00
$editor = new RoomEditor($room);
2012-05-19 18:33:25 +00:00
$editor->update(array('position' => ($stmt->fetchColumn() + 1)));
2012-03-03 20:36:52 +00:00
WCF::getDB()->commitTransaction();
return $room;
}
2012-05-19 18:33:25 +00:00
/**
* Deletes temporary rooms that are unused.
*
* @return integer Number of deleted rooms
*/
public function prune() {
$sql = "SELECT
".call_user_func(array($this->className, 'getDatabaseTableIndexName'))."
FROM
".call_user_func(array($this->className, 'getDatabaseTableName'))."
WHERE
permanent = ?
AND roomID NOT IN (
SELECT
fieldValue AS roomID
FROM
wcf".WCF_N."_user_storage
WHERE
2013-02-01 22:25:24 +00:00
field = ?
AND fieldValue IS NOT NULL
2012-05-19 18:33:25 +00:00
)";
$stmt = \wcf\system\WCF::getDB()->prepareStatement($sql);
$stmt->execute(array(0, 'roomID'));
2012-05-19 18:33:25 +00:00
$objectIDs = array();
while ($objectIDs[] = $stmt->fetchColumn());
return call_user_func(array($this->className, 'deleteAll'), $objectIDs);
}
2012-03-03 20:36:52 +00:00
/**
2013-04-20 15:19:17 +00:00
* @see wcf\data\ISortableAction::validateUpdatePosition()
2012-03-03 20:36:52 +00:00
*/
public function validateUpdatePosition() {
// validate permissions
if (is_array($this->permissionsUpdate) && count($this->permissionsUpdate)) {
2012-08-07 21:56:57 +00:00
WCF::getSession()->checkPermissions($this->permissionsUpdate);
2012-03-03 20:36:52 +00:00
}
else {
2013-01-18 14:45:21 +00:00
throw new \wcf\system\exception\PermissionDeniedException();
2012-03-03 20:36:52 +00:00
}
if (!isset($this->parameters['data']['structure'])) {
2013-01-18 14:45:21 +00:00
throw new \wcf\system\exception\UserInputException('structure');
2012-03-03 20:36:52 +00:00
}
}
/**
2013-04-20 15:19:17 +00:00
* @see wcf\data\ISortableAction::updatePosition()
2012-03-03 20:36:52 +00:00
*/
public function updatePosition() {
2013-01-19 19:36:40 +00:00
$roomList = new RoomList();
2012-03-03 20:36:52 +00:00
$roomList->readObjects();
2013-04-05 20:28:12 +00:00
$i = 0;
2012-03-03 20:36:52 +00:00
WCF::getDB()->beginTransaction();
foreach ($this->parameters['data']['structure'][0] as $roomID) {
2012-03-03 22:16:24 +00:00
$room = $roomList->search($roomID);
if ($room === null) continue;
2013-04-05 20:28:12 +00:00
2013-01-19 19:36:40 +00:00
$editor = new RoomEditor($room);
2012-03-03 20:36:52 +00:00
$editor->update(array('position' => $i++));
}
WCF::getDB()->commitTransaction();
}
2013-04-21 16:27:55 +00:00
/**
* Validates parameters and permissions.
*/
public function validateGetRoomList() {
if (!CHAT_ACTIVE) throw new \wcf\system\exception\IllegalLinkException();
2013-04-26 22:38:53 +00:00
$rooms = Room::getCache();
$roomID = ChatUtil::readUserData('roomID');
if (!isset($rooms[$roomID])) {
throw new \wcf\system\exception\IllegalLinkException();
}
$this->parameters['room'] = $rooms[$roomID];
2013-04-21 16:27:55 +00:00
}
/**
* Returns the available rooms.
*/
public function getRoomList() {
$rooms = Room::getCache();
$result = array();
foreach ($rooms as $room) {
if (!$room->canEnter()) continue;
$result[] = array(
'title' => (string) $room,
'link' => \wcf\system\request\LinkHandler::getInstance()->getLink('Chat', array(
'application' => 'chat',
'object' => $room
)),
2013-04-26 22:38:53 +00:00
'active' => $room->roomID == $this->parameters['room']->roomID
2013-04-21 16:27:55 +00:00
);
}
return $result;
}
2013-04-26 22:38:53 +00:00
/**
* Validates parameters and permissions.
*/
public function validateLeave() {
if (!CHAT_ACTIVE) throw new \wcf\system\exception\IllegalLinkException();
unset($this->parameters['user']);
$rooms = Room::getCache();
$roomID = ChatUtil::readUserData('roomID');
if (!isset($rooms[$roomID])) throw new \wcf\system\exception\IllegalLinkException();
}
/**
* Leaves the room.
*/
public function leave() {
// user cannot be set during an AJAX request may be set by the chat itself
if (!isset($this->parameters['user'])) {
$this->parameters['user'] = WCF::getUser();
}
$rooms = Room::getCache();
$roomID = ChatUtil::readUserData('roomID', $this->parameters['user']);
if (!isset($rooms[$roomID])) throw new \wcf\system\exception\UserInputException();
$activeRoom = $rooms[$roomID];
if (CHAT_DISPLAY_JOIN_LEAVE) {
$userData['color'] = ChatUtil::readUserData('color', $this->parameters['user']);
// leave message
$messageAction = new \chat\data\message\MessageAction(array(), 'create', array(
'data' => array(
'roomID' => $activeRoom->roomID,
'sender' => $this->parameters['user']->userID,
'username' => $this->parameters['user']->username,
'time' => TIME_NOW,
'type' => \chat\data\message\Message::TYPE_LEAVE,
'message' => '',
'color1' => $userData['color'][1],
'color2' => $userData['color'][2]
)
));
$messageAction->executeAction();
}
// set current room to null
ChatUtil::writeUserData(array('roomID' => null), $this->parameters['user']);
}
2012-02-04 20:06:44 +00:00
}