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

135 lines
3.9 KiB
PHP
Raw Normal View History

2011-11-27 12:21:58 +01:00
<?php
namespace wcf\data\chat\message;
use \wcf\system\WCF;
2011-11-27 12:21:58 +01:00
/**
* Represents a chat message.
*
* @author Tim Düsterhus
2012-01-28 17:50:33 +01:00
* @copyright 2010-2012 Tim Düsterhus
2011-11-27 12:21:58 +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-27 12:21:58 +01:00
* @subpackage data.chat.message
*/
class ChatMessage extends \wcf\data\DatabaseObject {
/**
* @see \wcf\data\DatabaseObject::$databaseTableName
2011-11-27 12:21:58 +01:00
*/
protected static $databaseTableName = 'chat_message';
/**
* @see \wcf\data\DatabaseObject::$databaseTableIndexName
2011-11-27 12:21:58 +01:00
*/
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;
const TYPE_ERROR = 12;
2011-11-27 12:21:58 +01:00
/**
2011-12-27 12:35:58 +01:00
* @see \wcf\data\chat\message\ChatMessage::getFormattedMessage()
2011-11-27 12:21:58 +01:00
*/
public function __toString() {
return $this->getFormattedMessage();
}
/**
* Returns the formatted message.
2012-10-20 16:23:00 +02:00
*
* @param string $outputType outputtype for messageparser
* @return string
*/
2012-01-21 19:05:34 +01:00
public function getFormattedMessage($outputType = 'text/html') {
$message = $this->message;
switch ($this->type) {
case self::TYPE_JOIN:
case self::TYPE_LEAVE:
case self::TYPE_BACK:
case self::TYPE_AWAY:
2012-08-07 21:12:32 +02:00
WCF::getTPL()->assign(@unserialize($message));
$message = WCF::getLanguage()->getDynamicVariable('wcf.chat.message.'.$this->type);
break;
2012-10-20 18:04:46 +02:00
case self::TYPE_MODERATE:
2012-09-07 22:58:13 +02:00
$message = @unserialize($message);
2012-10-20 18:04:46 +02:00
WCF::getTPL()->assign($message);
$message = WCF::getLanguage()->getDynamicVariable('wcf.chat.message.'.$this->type.'.'.$message['type']);
break;
case self::TYPE_WHISPER:
2012-11-17 14:45:51 +01:00
$message = @unserialize($message);
2012-09-07 22:58:13 +02:00
$message = $message['message'];
case self::TYPE_NORMAL:
case self::TYPE_ME:
2012-01-21 19:05:34 +01:00
if (!$this->enableHTML && $outputType == 'text/html') {
2011-12-20 21:20:32 +01:00
$message = \wcf\system\bbcode\SimpleMessageParser::getInstance()->parse($message, true, $this->enableSmilies);
}
break;
}
return $message;
2011-11-27 12:21:58 +01:00
}
/**
* Returns the formatted username
*
* @return string
*/
public function getFormattedUsername() {
2011-12-05 16:53:12 +01:00
$username = $this->getUsername();
2011-12-27 14:09:29 +01:00
if ($this->type != self::TYPE_INFORMATION && $this->type != self::TYPE_ERROR) $username = \wcf\util\ChatUtil::gradient($username, $this->color1, $this->color2);
2012-09-07 22:58:13 +02:00
if ($this->type == self::TYPE_WHISPER) {
$message = @unserialize($this->message);
$username .= ' -> '.$message['username'];
}
return $username;
}
/**
2011-12-10 15:24:36 +01:00
* Returns the unformatted username.
*
* @return string
*/
public function getUsername() {
if ($this->type == self::TYPE_INFORMATION) return WCF::getLanguage()->get('wcf.chat.information');
if ($this->type == self::TYPE_ERROR) return WCF::getLanguage()->get('wcf.chat.error');
2011-12-27 14:09:29 +01:00
return $this->username;
}
2011-12-10 15:24:36 +01:00
/**
* Converts this message into json-form.
*
2011-12-27 12:38:05 +01:00
* @param boolean $raw
2011-12-10 15:24:36 +01:00
* @return string
*/
2011-12-27 12:38:05 +01:00
public function jsonify($raw = false) {
$array = array(
'formattedUsername' => $this->getFormattedUsername(),
'formattedMessage' => (string) $this,
'formattedTime' => \wcf\util\DateUtil::format(\wcf\util\DateUtil::getDateTimeByTimestamp($this->time), 'H:i:s'),
2012-10-20 18:04:46 +02:00
'separator' => ($this->type == self::TYPE_NORMAL || $this->type == self::TYPE_ERROR || $this->type == self::TYPE_INFORMATION) ? ': ' : ' ',
2012-01-21 19:05:34 +01:00
'message' => $this->getFormattedMessage('text/plain'),
2012-05-02 20:27:16 +02:00
'sender' => (int) $this->sender,
'username' => $this->getUsername(),
2012-05-02 20:27:16 +02:00
'time' => (int) $this->time,
'receiver' => (int) $this->receiver,
'type' => (int) $this->type,
'roomID' => (int) $this->roomID,
'messageID' => (int) $this->messageID
2011-12-27 12:38:05 +01:00
);
if ($raw) return $array;
return \wcf\util\JSON::encode($array);
}
2011-11-27 12:21:58 +01:00
}