2011-11-27 11:21:58 +00:00
|
|
|
<?php
|
2013-01-19 19:36:40 +00:00
|
|
|
namespace chat\data\message;
|
2013-01-27 20:58:10 +00:00
|
|
|
use \chat\util\ChatUtil;
|
2013-10-06 15:58:24 +00:00
|
|
|
use \wcf\system\bbcode\AttachmentBBCode;
|
2012-11-20 15:01:32 +00:00
|
|
|
use \wcf\system\Regex;
|
2011-12-04 21:52:57 +00:00
|
|
|
use \wcf\system\WCF;
|
2011-11-27 11:21:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents a chat message.
|
2014-02-28 16:06:50 +00:00
|
|
|
*
|
2012-11-20 15:01:32 +00:00
|
|
|
* @author Tim Düsterhus
|
2014-02-27 22:05:09 +00:00
|
|
|
* @copyright 2010-2014 Tim Düsterhus
|
2011-11-27 11:21:58 +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.message
|
2011-11-27 11:21:58 +00:00
|
|
|
*/
|
2013-01-26 21:46:54 +00:00
|
|
|
class Message extends \chat\data\CHATDatabaseObject {
|
2011-11-27 11:21:58 +00:00
|
|
|
/**
|
2012-02-26 16:55:44 +00:00
|
|
|
* @see \wcf\data\DatabaseObject::$databaseTableName
|
2011-11-27 11:21:58 +00:00
|
|
|
*/
|
2013-01-19 19:36:40 +00:00
|
|
|
protected static $databaseTableName = 'message';
|
2011-11-27 11:21:58 +00:00
|
|
|
|
|
|
|
/**
|
2012-02-26 16:55:44 +00:00
|
|
|
* @see \wcf\data\DatabaseObject::$databaseTableIndexName
|
2011-11-27 11:21:58 +00: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;
|
2013-10-06 00:59:13 +00:00
|
|
|
const TYPE_ATTACHMENT = 12;
|
2011-11-27 11:21:58 +00:00
|
|
|
|
2012-11-20 15:01:32 +00:00
|
|
|
/**
|
|
|
|
* cache for users
|
|
|
|
* @var array<\wcf\data\user\User>
|
|
|
|
*/
|
|
|
|
protected static $users = array();
|
|
|
|
|
2013-05-28 21:39:19 +00:00
|
|
|
/**
|
|
|
|
* @see \wcf\data\DatabaseObject::handleData()
|
|
|
|
*/
|
|
|
|
protected function handleData($data) {
|
|
|
|
parent::handleData($data);
|
|
|
|
|
|
|
|
if ($this->data['additionalData'] !== null) {
|
|
|
|
$this->data['additionalData'] = unserialize($this->data['additionalData']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-27 11:21:58 +00:00
|
|
|
/**
|
2013-05-24 22:50:50 +00:00
|
|
|
* @see \chat\data\message\Message::getFormattedMessage()
|
2011-11-27 11:21:58 +00:00
|
|
|
*/
|
|
|
|
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.
|
2012-10-20 14:23:00 +00:00
|
|
|
*
|
|
|
|
* @param string $outputType outputtype for messageparser
|
2011-12-04 21:52:57 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2013-11-02 22:08:12 +00:00
|
|
|
public function getFormattedMessage($type = 'text/html') {
|
2011-12-04 21:52:57 +00:00
|
|
|
$message = $this->message;
|
2013-04-20 17:49:06 +00:00
|
|
|
$messageParser = \wcf\system\bbcode\MessageParser::getInstance();
|
2013-07-29 22:19:02 +00:00
|
|
|
$messageParser->setOutputType($type);
|
2012-11-20 15:01:32 +00:00
|
|
|
|
2011-12-04 21:52:57 +00:00
|
|
|
switch ($this->type) {
|
|
|
|
case self::TYPE_JOIN:
|
|
|
|
case self::TYPE_LEAVE:
|
|
|
|
case self::TYPE_BACK:
|
2013-05-28 21:39:19 +00:00
|
|
|
$message = WCF::getLanguage()->getDynamicVariable('chat.message.'.$this->type, $this->data['additionalData'] ?: array());
|
|
|
|
break;
|
2012-04-15 13:37:41 +00:00
|
|
|
case self::TYPE_AWAY:
|
2013-05-28 21:39:19 +00:00
|
|
|
$message = WCF::getLanguage()->getDynamicVariable('chat.message.'.$this->type, array('message' => $message));
|
2012-04-15 13:37:41 +00:00
|
|
|
break;
|
2012-10-20 16:04:46 +00:00
|
|
|
case self::TYPE_MODERATE:
|
2012-11-20 15:01:32 +00:00
|
|
|
$message = unserialize($message);
|
2013-01-19 19:36:40 +00:00
|
|
|
$message = WCF::getLanguage()->getDynamicVariable('chat.message.'.$this->type.'.'.$message['type'], $message ?: array());
|
2013-04-20 17:49:06 +00:00
|
|
|
|
2014-02-27 15:41:05 +00:00
|
|
|
if ($type === 'text/plain') return $message;
|
2013-04-20 17:49:06 +00:00
|
|
|
$message = $messageParser->parse($message, false, false, true, false);
|
2012-10-20 16:04:46 +00:00
|
|
|
break;
|
2013-10-06 00:59:13 +00:00
|
|
|
case self::TYPE_ATTACHMENT:
|
2013-11-02 22:08:12 +00:00
|
|
|
$attachmentList = new \wcf\data\attachment\GroupedAttachmentList('be.bastelstu.chat.message');
|
|
|
|
$attachmentList->getConditionBuilder()->add('attachment.objectID IN (?)', array($this->messageID));
|
|
|
|
$attachmentList->readObjects();
|
2013-10-06 00:59:13 +00:00
|
|
|
|
|
|
|
AttachmentBBCode::setAttachmentList($attachmentList);
|
|
|
|
AttachmentBBCode::setObjectID($this->messageID);
|
|
|
|
|
2013-10-16 13:40:29 +00:00
|
|
|
$message = $messageParser->parse($message, false, false, true, false);
|
2013-10-06 00:59:13 +00:00
|
|
|
break;
|
2012-10-20 16:04:46 +00:00
|
|
|
case self::TYPE_WHISPER:
|
2011-12-10 15:46:35 +00:00
|
|
|
case self::TYPE_NORMAL:
|
2011-12-27 13:10:40 +00:00
|
|
|
case self::TYPE_ME:
|
2012-11-20 15:01:32 +00:00
|
|
|
default:
|
2013-07-29 22:19:02 +00:00
|
|
|
if ($type === 'text/plain') return $message;
|
2013-04-11 16:00:53 +00:00
|
|
|
|
2013-03-11 14:42:05 +00:00
|
|
|
$message = $messageParser->parse($message, $this->enableSmilies, $this->enableHTML, true, false);
|
2012-11-20 15:01:32 +00:00
|
|
|
break;
|
2011-12-04 21:52:57 +00:00
|
|
|
}
|
2012-11-20 15:01:32 +00:00
|
|
|
|
2011-12-04 21:52:57 +00:00
|
|
|
return $message;
|
2011-11-27 11:21:58 +00:00
|
|
|
}
|
2011-12-04 21:46:50 +00:00
|
|
|
|
|
|
|
/**
|
2012-11-20 15:01:32 +00:00
|
|
|
* Returns the username.
|
|
|
|
*
|
|
|
|
* @param boolean $colored
|
2011-12-04 21:46:50 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2012-11-20 15:01:32 +00:00
|
|
|
public function getUsername($colored = false) {
|
|
|
|
$username = $this->username;
|
2014-02-27 17:55:53 +00:00
|
|
|
if ($this->type == self::TYPE_INFORMATION) return WCF::getLanguage()->get('chat.global.information');
|
2012-11-20 15:01:32 +00:00
|
|
|
|
|
|
|
if ($colored) {
|
2014-08-12 18:43:35 +00:00
|
|
|
if ($this->color1 !== null || $this->color2 !== null) {
|
|
|
|
$username = \chat\util\ChatUtil::gradient($username, $this->color1, $this->color2);
|
|
|
|
}
|
|
|
|
else {
|
2014-12-12 20:13:30 +00:00
|
|
|
$username = '<span class="defaultColor">'.\wcf\util\StringUtil::encodeHTML($username).'</span>';
|
2014-08-12 18:43:35 +00:00
|
|
|
}
|
2012-11-20 15:01:32 +00:00
|
|
|
}
|
2011-12-04 21:57:15 +00:00
|
|
|
|
2012-11-20 14:16:27 +00:00
|
|
|
return $username;
|
2011-12-04 21:46:50 +00:00
|
|
|
}
|
2011-12-05 13:45:50 +00:00
|
|
|
|
2011-12-10 14:24:36 +00:00
|
|
|
/**
|
|
|
|
* Converts this message into json-form.
|
2014-02-28 16:06:50 +00:00
|
|
|
*
|
2011-12-27 11:38:05 +00:00
|
|
|
* @param boolean $raw
|
2011-12-10 14:24:36 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2011-12-27 11:38:05 +00:00
|
|
|
public function jsonify($raw = false) {
|
2012-11-20 15:01:32 +00:00
|
|
|
switch ($this->type) {
|
2013-05-28 21:39:19 +00:00
|
|
|
case self::TYPE_WHISPER:
|
2012-11-20 15:01:32 +00:00
|
|
|
case self::TYPE_NORMAL:
|
|
|
|
case self::TYPE_INFORMATION:
|
|
|
|
$separator = ':';
|
|
|
|
break;
|
|
|
|
default:
|
2013-04-13 14:38:50 +00:00
|
|
|
$separator = '';
|
2012-11-20 15:01:32 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-10-31 01:19:33 +00:00
|
|
|
$time = \wcf\util\DateUtil::getDateTimeByTimestamp($this->time);
|
|
|
|
$startOfDay = new \DateTime('today', WCF::getUser()->getTimezone());
|
2011-12-27 11:38:05 +00:00
|
|
|
$array = array(
|
2012-11-20 15:01:32 +00:00
|
|
|
'formattedUsername' => $this->getUsername(true),
|
2013-11-02 22:08:12 +00:00
|
|
|
'formattedMessage' => $this->getFormattedMessage('text/html'),
|
2014-10-31 01:19:33 +00:00
|
|
|
'formattedTime' => ($time->getTimestamp() < $startOfDay->getTimestamp() ? \wcf\util\DateUtil::format($time, 'chat.global.dateFormat').' ' : '').\wcf\util\DateUtil::format($time, 'chat.global.timeFormat'),
|
2012-11-20 15:01:32 +00:00
|
|
|
'separator' => $separator,
|
2013-11-02 22:08:12 +00:00
|
|
|
'message' => $this->getFormattedMessage('text/plain'),
|
2012-05-02 18:27:16 +00:00
|
|
|
'sender' => (int) $this->sender,
|
2011-12-05 13:45:50 +00:00
|
|
|
'username' => $this->getUsername(),
|
2012-05-02 18:27:16 +00:00
|
|
|
'time' => (int) $this->time,
|
|
|
|
'receiver' => (int) $this->receiver,
|
|
|
|
'type' => (int) $this->type,
|
|
|
|
'roomID' => (int) $this->roomID,
|
2013-05-28 21:39:19 +00:00
|
|
|
'messageID' => (int) $this->messageID,
|
2013-11-02 22:08:12 +00:00
|
|
|
'additionalData' => $this->additionalData
|
2011-12-27 11:38:05 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if ($raw) return $array;
|
|
|
|
return \wcf\util\JSON::encode($array);
|
2011-12-05 13:45:50 +00:00
|
|
|
}
|
2011-11-27 11:21:58 +00:00
|
|
|
}
|