1
0
mirror of https://github.com/wbbaddons/Tims-Chat.git synced 2024-12-22 21:40:08 +00:00

Moved readUserData to chatUtil

closes 2
This commit is contained in:
max-m 2011-12-23 15:26:54 +01:00
parent cf72777f13
commit 0f411e8015
4 changed files with 55 additions and 85 deletions

View File

@ -3,8 +3,6 @@
use \wcf\data\chat; use \wcf\data\chat;
use \wcf\system\exception\PermissionDeniedException; use \wcf\system\exception\PermissionDeniedException;
use \wcf\system\exception\UserInputException; use \wcf\system\exception\UserInputException;
use \wcf\system\package\PackageDependencyHandler;
use \wcf\system\user\storage\UserStorageHandler;
use \wcf\system\WCF; use \wcf\system\WCF;
use \wcf\util\StringUtil; use \wcf\util\StringUtil;
@ -27,7 +25,9 @@ class ChatForm extends AbstractForm {
* @see \wcf\page\AbstractPage::readData() * @see \wcf\page\AbstractPage::readData()
*/ */
public function readData() { public function readData() {
$this->readUserData(); $this->userData['color'] = \wcf\util\ChatUtil::readUserData('color');
$this->userData['roomID'] = \wcf\util\ChatUtil::readUserData('roomID');
parent::readData(); parent::readData();
} }
@ -41,34 +41,6 @@ public function readFormParameters() {
if (isset($_REQUEST['smilies'])) $this->enableSmilies = intval($_REQUEST['smilies']); if (isset($_REQUEST['smilies'])) $this->enableSmilies = intval($_REQUEST['smilies']);
} }
/**
* Reads user data.
*/
public function readUserData() {
// TODO: Move this into ChatUtil
$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];
$data = $ush->getStorage(array(WCF::getUser()->userID), 'roomID', $packageID);
$this->userData['roomID'] = $data[WCF::getUser()->userID];
}
/** /**
* @see \wcf\form\AbstractForm::validate() * @see \wcf\form\AbstractForm::validate()
*/ */

View File

@ -2,8 +2,6 @@
namespace wcf\page; namespace wcf\page;
use \wcf\data\chat; use \wcf\data\chat;
use \wcf\system\cache\CacheHandler; use \wcf\system\cache\CacheHandler;
use \wcf\system\package\PackageDependencyHandler;
use \wcf\system\user\storage\UserStorageHandler;
use \wcf\system\WCF; use \wcf\system\WCF;
/** /**
@ -66,7 +64,9 @@ public function readData() {
parent::readData(); parent::readData();
$this->readRoom(); $this->readRoom();
$this->readUserData(); $this->userData['color'] = \wcf\util\ChatUtil::readUserData('color');
\wcf\util\ChatUtil::writeUserData(array('roomID' => $this->room->roomID));
if (CHAT_DISPLAY_JOIN_LEAVE) { if (CHAT_DISPLAY_JOIN_LEAVE) {
$messageAction = new chat\message\ChatMessageAction(array(), 'create', array( $messageAction = new chat\message\ChatMessageAction(array(), 'create', array(
'data' => array( 'data' => array(
@ -146,33 +146,6 @@ public function readRoom() {
if (!$this->room) throw new \wcf\system\exception\IllegalLinkException(); if (!$this->room) throw new \wcf\system\exception\IllegalLinkException();
} }
/**
* Reads user data.
*/
public function readUserData() {
// TODO: Move this into ChatUtil
$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];
$ush->update(WCF::getUser()->userID, 'roomID', $this->room->roomID, $packageID);
}
/** /**
* @see \wcf\page\IPage::show() * @see \wcf\page\IPage::show()
*/ */

View File

@ -2,8 +2,6 @@
namespace wcf\page; namespace wcf\page;
use \wcf\data\chat; use \wcf\data\chat;
use \wcf\system\cache\CacheHandler; use \wcf\system\cache\CacheHandler;
use \wcf\system\package\PackageDependencyHandler;
use \wcf\system\user\storage\UserStorageHandler;
use \wcf\system\WCF; use \wcf\system\WCF;
/** /**
@ -28,32 +26,13 @@ class ChatRefreshRoomListPage extends AbstractPage {
*/ */
public function readData() { public function readData() {
parent::readData(); parent::readData();
$this->readUserData(); $this->roomID = \wcf\util\ChatUtil::readUserData('roomID');
$this->rooms = chat\room\ChatRoom::getCache(); $this->rooms = chat\room\ChatRoom::getCache();
$this->room = $this->rooms->search($this->roomID); $this->room = $this->rooms->search($this->roomID);
if (!$this->room) throw new \wcf\system\exception\IllegalLinkException(); if (!$this->room) throw new \wcf\system\exception\IllegalLinkException();
} }
/**
* Reads user data.
*/
public function readUserData() {
// TODO: Move this into ChatUtil
$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), 'roomID', $packageID);
if ($data[WCF::getUser()->userID] === null) {
throw new \wcf\system\exception\IllegalLinkException();
}
$this->roomID = $data[WCF::getUser()->userID];
}
/** /**
* @see \wcf\page\IPage::show() * @see \wcf\page\IPage::show()
*/ */

View File

@ -1,5 +1,8 @@
<?php <?php
namespace wcf\util; namespace wcf\util;
use \wcf\system\user\storage\UserStorageHandler;
use \wcf\system\package\PackageDependencyHandler;
use \wcf\system\WCF;
/** /**
* Chat utilities * Chat utilities
@ -19,6 +22,8 @@ class ChatUtil {
*/ */
const TIME_MODIFIER_REGEX = '((?:[0-9]+[s|h|d|w|m|y|S|H|D|W|M|Y]?,?)+)'; const TIME_MODIFIER_REGEX = '((?:[0-9]+[s|h|d|w|m|y|S|H|D|W|M|Y]?,?)+)';
public static $serialize = array('color' => true);
/** /**
* Creates a gradient out of two colors represented by an integer. * Creates a gradient out of two colors represented by an integer.
* The first byte is red, the second byte is green, the third one is blue. * The first byte is red, the second byte is green, the third one is blue.
@ -27,7 +32,7 @@ class ChatUtil {
* @param string $string * @param string $string
* @param integer $start * @param integer $start
* @param integer $end * @param integer $end
* @returen string * @return string
*/ */
public static function gradient($string, $start, $end) { public static function gradient($string, $start, $end) {
$string = self::str_split($string); $string = self::str_split($string);
@ -45,6 +50,47 @@ public static function gradient($string, $start, $end) {
return $result; return $result;
} }
/**
* Reads user data.
*
* @param string $field
* @return mixed
*/
public static function readUserData($field) {
$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), $field, $packageID);
if ($data[WCF::getUser()->userID] === null) {
switch ($field) {
case 'color':
$data[WCF::getUser()->userID] = array(1 => 0xFF0000, 2 => 0x00FF00);
break;
}
static::writeUserData(array($field => $data[WCF::getUser()->userID]));
}
if (isset(static::$serialize[$field])) return unserialize($data[WCF::getUser()->userID]);
else return $data[WCF::getUser()->userID];
}
/**
* Writes user data
*
* @param array $data
*/
public static function writeUserData(array $data) {
$ush = UserStorageHandler::getInstance();
$packageID = PackageDependencyHandler::getPackageID('timwolla.wcf.chat');
foreach($data as $key => $value) {
$ush->update(WCF::getUser()->userID, $key, (isset(static::$serialize[$key])) ? serialize($value) : $value, $packageID);
}
}
/** /**
* Splits a string into smaller chunks. * Splits a string into smaller chunks.
* UTF-8 safe version of str_split(). * UTF-8 safe version of str_split().