2 namespace wcf\data\conversation\message
;
3 use wcf\data\attachment\GroupedAttachmentList
;
4 use wcf\data\conversation\Conversation
;
5 use wcf\data\DatabaseObject
;
7 use wcf\data\TUserContent
;
8 use wcf\system\html\output\HtmlOutputProcessor
;
9 use wcf\system\request\LinkHandler
;
11 use wcf\util\StringUtil
;
14 * Represents a conversation message.
17 * @copyright 2001-2019 WoltLab GmbH
18 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
19 * @package WoltLabSuite\Core\Data\Conversation\Message
21 * @property-read integer $messageID unique id of the conversation message
22 * @property-read integer $conversationID id of the conversation the conversation message belongs to
23 * @property-read integer|null $userID id of the user who wrote the conversation message or `null` if the user does not exist anymore
24 * @property-read string $username name of the user who wrote the conversation message
25 * @property-read string $message text of the conversation message
26 * @property-read integer $time timestamp at which the conversation message has been written
27 * @property-read integer $attachments number of attachments
28 * @property-read integer $enableHtml is `1` if the conversation message's format has been converted to html, otherwise `0`
29 * @property-read string $ipAddress ip address of the user who wrote the conversation message at time of writing or empty if no ip addresses are logged
30 * @property-read integer $lastEditTime timestamp at which the conversation message has been edited the last time
31 * @property-read integer $editCount number of times the conversation message has been edited
32 * @property-read integer $hasEmbeddedObjects number of embedded objects in the conversation message
34 class ConversationMessage
extends DatabaseObject
implements IMessage
{
41 protected $conversation;
46 public function getFormattedMessage() {
47 $processor = new HtmlOutputProcessor();
48 $processor->process($this->message
, 'com.woltlab.wcf.conversation.message', $this->messageID
);
50 return $processor->getHtml();
54 * Returns a simplified version of the formatted message.
58 public function getSimplifiedFormattedMessage() {
59 $processor = new HtmlOutputProcessor();
60 $processor->setOutputType('text/simplified-html');
61 $processor->process($this->message
, 'com.woltlab.wcf.conversation.message', $this->messageID
);
63 return $processor->getHtml();
67 * Assigns and returns the embedded attachments.
69 * @param boolean $ignoreCache
70 * @return GroupedAttachmentList
72 public function getAttachments($ignoreCache = false) {
73 if ($this->attachments ||
$ignoreCache) {
74 $attachmentList = new GroupedAttachmentList('com.woltlab.wcf.conversation.message');
75 $attachmentList->getConditionBuilder()->add('attachment.objectID IN (?)', [$this->messageID
]);
76 $attachmentList->readObjects();
77 $attachmentList->setPermissions([
78 'canDownload' => true,
79 'canViewPreview' => true
82 if ($ignoreCache && !count($attachmentList)) {
86 return $attachmentList;
95 public function getExcerpt($maxLength = 255) {
96 return StringUtil
::truncateHTML($this->getSimplifiedFormattedMessage(), $maxLength);
100 * Returns a version of this message optimized for use in emails.
102 * @param string $mimeType Either 'text/plain' or 'text/html'
105 public function getMailText($mimeType = 'text/plain') {
108 $processor = new HtmlOutputProcessor();
109 $processor->setOutputType('text/plain');
110 $processor->process($this->message
, 'com.woltlab.wcf.conversation.message', $this->messageID
);
112 return $processor->getHtml();
114 return $this->getSimplifiedFormattedMessage();
117 throw new \
LogicException('Unreachable');
121 * Returns the conversation of this message.
123 * @return Conversation
125 public function getConversation() {
126 if ($this->conversation
=== null) {
127 $this->conversation
= Conversation
::getUserConversation($this->conversationID
, WCF
::getUser()->userID
);
130 return $this->conversation
;
134 * Sets the conversation of this message.
136 * @param Conversation $conversation
138 public function setConversation(Conversation
$conversation) {
139 if ($this->conversationID
== $conversation->conversationID
) {
140 $this->conversation
= $conversation;
145 * Returns true if current user may edit this message.
149 public function canEdit() {
150 return WCF
::getUser()->userID
== $this->userID
151 && ($this->getConversation()->isDraft || WCF
::getSession()->getPermission('user.conversation.canEditMessage'))
152 && $this->getConversation()->canReply();
158 public function getMessage() {
159 return $this->message
;
165 public function getLink() {
166 return LinkHandler
::getInstance()->getLink('Conversation', [
167 'object' => $this->getConversation(),
168 'messageID' => $this->messageID
,
169 'forceFrontend' => true
170 ], '#message'.$this->messageID
);
176 public function getTitle() {
177 if ($this->messageID
== $this->getConversation()->firstMessageID
) {
178 return $this->getConversation()->subject
;
181 return 'RE: '.$this->getConversation()->subject
;
187 public function isVisible() {
194 public function __toString() {
195 return $this->getFormattedMessage();