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

119 lines
3.2 KiB
PHP
Raw Normal View History

2012-02-04 20:06:44 +00:00
<?php
namespace wcf\data\chat\room;
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
* @copyright 2010-2012 Tim Düsterhus
* @license Creative Commons Attribution-NonCommercial-ShareAlike <http://creativecommons.org/licenses/by-nc-sa/3.0/legalcode>
2012-03-15 19:16:24 +00:00
* @package be.bastelstu.wcf.chat
2012-02-04 20:06:44 +00:00
* @subpackage data.chat.room
*/
2013-01-07 16:42:33 +00:00
class ChatRoomAction 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
*/
protected $className = '\wcf\data\chat\room\ChatRoomEditor';
/**
* @see \wcf\data\AbstractDatabaseObjectAction::$permissionsDelete
2012-02-04 20:06:44 +00:00
*/
protected $permissionsDelete = array('admin.content.chat.canDeleteRoom');
/**
* @see \wcf\data\AbstractDatabaseObjectAction::$permissionsUpdate
2012-02-04 20:06:44 +00:00
*/
protected $permissionsUpdate = array('admin.content.chat.canEditRoom');
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
$editor = new ChatRoomEditor($room);
$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
AND field = ?
AND fieldValue IS NOT NULL
)";
$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-01-07 16:42:33 +00:00
* @see wcf\data\ISortableAction
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 {
2012-08-07 21:56:57 +00:00
throw new PermissionDeniedException();
2012-03-03 20:36:52 +00:00
}
if (!isset($this->parameters['data']['structure'])) {
2012-08-07 21:56:57 +00:00
throw new UserInputException('structure');
2012-03-03 20:36:52 +00:00
}
2012-03-16 16:09:12 +00:00
if (!isset($this->parameters['data']['offset'])) $this->parameters['data']['offset'] = 0;
2012-03-03 20:36:52 +00:00
}
/**
2013-01-07 16:42:33 +00:00
* @see wcf\data\ISortableAction
2012-03-03 20:36:52 +00:00
*/
public function updatePosition() {
$roomList = new \wcf\data\chat\room\ChatRoomList();
$roomList->sqlOrderBy = "chat_room.position";
$roomList->sqlLimit = 0;
$roomList->readObjects();
2012-03-16 16:09:12 +00:00
$i = $this->parameters['data']['offset'];
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;
$editor = new ChatRoomEditor($room);
2012-03-03 20:36:52 +00:00
$editor->update(array('position' => $i++));
}
WCF::getDB()->commitTransaction();
}
2012-02-04 20:06:44 +00:00
}