2011-11-27 11:21:58 +00:00
|
|
|
<?php
|
|
|
|
namespace wcf\data\chat\message;
|
2011-12-04 21:52:57 +00:00
|
|
|
use \wcf\system\WCF;
|
2011-11-27 11:21:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents a chat message.
|
|
|
|
*
|
|
|
|
* @author Tim Düsterhus
|
|
|
|
* @copyright 2010-2011 Tim Düsterhus
|
|
|
|
* @license Creative Commons Attribution-NonCommercial-ShareAlike <http://creativecommons.org/licenses/by-nc-sa/3.0/legalcode>
|
|
|
|
* @package timwolla.wcf.chat
|
|
|
|
* @subpackage data.chat.message
|
|
|
|
*/
|
|
|
|
class ChatMessage extends \wcf\data\DatabaseObject {
|
|
|
|
/**
|
|
|
|
* @see wcf\data\DatabaseObject::$databaseTableName
|
|
|
|
*/
|
|
|
|
protected static $databaseTableName = 'chat_message';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see wcf\data\DatabaseObject::$databaseTableIndexName
|
|
|
|
*/
|
|
|
|
protected static $databaseTableIndexName = 'messageID';
|
|
|
|
|
|
|
|
const TYPE_NORMAL = 0;
|
|
|
|
const TYPE_JOIN = 1;
|
|
|
|
const TYPE_LEAVE = 2;
|
|
|
|
const TYPE_AWAY = 3;
|
|
|
|
const TYPE_BACK = 4;
|
|
|
|
const TYPE_MODERATE = 5;
|
|
|
|
const TYPE_ME = 6;
|
|
|
|
const TYPE_WHISPER = 7;
|
|
|
|
const TYPE_INFORMATION = 8;
|
|
|
|
const TYPE_CLEAR = 9;
|
|
|
|
const TYPE_TEAM = 10;
|
|
|
|
const TYPE_GLOBALMESSAGE = 11;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the message.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function __toString() {
|
2011-12-05 13:45:50 +00:00
|
|
|
return $this->getFormattedMessage();
|
2011-12-04 21:52:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the formatted message.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getFormattedMessage() {
|
|
|
|
$message = $this->message;
|
|
|
|
switch ($this->type) {
|
|
|
|
case self::TYPE_JOIN:
|
|
|
|
case self::TYPE_LEAVE:
|
|
|
|
case self::TYPE_BACK:
|
|
|
|
$message = WCF::getLanguage()->get('wcf.chat.message.'.$this->type);
|
|
|
|
}
|
|
|
|
return $message;
|
2011-11-27 11:21:58 +00:00
|
|
|
}
|
2011-12-04 21:46:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the formatted username
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getFormattedUsername() {
|
2011-12-05 13:45:50 +00:00
|
|
|
if ($this->type == self::TYPE_INFORMATION) return '<strong>'.$this->getUsername().'</strong>';
|
2011-12-04 21:57:15 +00:00
|
|
|
|
2011-12-05 13:45:50 +00:00
|
|
|
$string = str_split($this->getUsername());
|
2011-12-04 21:46:50 +00:00
|
|
|
$r = (int) (($this->color1 >> 16 & 255) - ($this->color2 >> 16 & 255)) / (count($string) - 1);
|
|
|
|
$g = (int) (($this->color1 >> 8 & 255) - ($this->color2 >> 8 & 255)) / (count($string) - 1);
|
|
|
|
$b = (int) (($this->color1 & 255) - ($this->color2 & 255)) / (count($string) - 1);
|
|
|
|
$result = '';
|
|
|
|
for ($i = 0, $max = count($string); $i < $max; $i++) {
|
|
|
|
$result .= '<span style="color:rgb('.(($this->color1 >> 16 & 255) - $i * $r).', '.(($this->color1 >> 8 & 255) - $i * $g).', '.(($this->color1 & 255) - $i * $b).')">'.$string[$i].'</span>';
|
|
|
|
}
|
|
|
|
|
|
|
|
return '<strong>'.$result.'</strong>';
|
|
|
|
}
|
2011-12-05 13:45:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the unformatted username
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getUsername() {
|
|
|
|
if ($this->type == self::TYPE_INFORMATION) return WCF::getLanguage()->get('wcf.chat.information');
|
|
|
|
return $this->username;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function jsonify() {
|
|
|
|
return \wcf\util\JSON::encode(array(
|
|
|
|
'formattedUsername' => $this->getFormattedUsername(),
|
|
|
|
'formattedMessage' => $this->getFormattedMessage(),
|
2011-12-05 14:39:56 +00:00
|
|
|
'formattedTime' => \wcf\util\DateUtil::format(\wcf\util\DateUtil::getDateTimeByTimestamp($this->time), 'H:i:s'),
|
2011-12-05 13:45:50 +00:00
|
|
|
'sender' => $this->sender,
|
|
|
|
'username' => $this->getUsername(),
|
2011-12-05 14:39:56 +00:00
|
|
|
'time' => $this->time,
|
2011-12-05 13:45:50 +00:00
|
|
|
'receiver' => $this->receiver,
|
|
|
|
'type' => $this->type
|
|
|
|
));
|
|
|
|
|
|
|
|
}
|
2011-11-27 11:21:58 +00:00
|
|
|
}
|