Remove obsolete template code
[GitHub/WoltLab/com.woltlab.wcf.conversation.git] / files / lib / data / conversation / message / ConversationMessage.class.php
1 <?php
2 namespace wcf\data\conversation\message;
3 use wcf\data\attachment\GroupedAttachmentList;
4 use wcf\data\conversation\Conversation;
5 use wcf\data\DatabaseObject;
6 use wcf\data\IMessage;
7 use wcf\data\TUserContent;
8 use wcf\system\html\output\HtmlOutputProcessor;
9 use wcf\system\request\LinkHandler;
10 use wcf\system\WCF;
11 use wcf\util\StringUtil;
12
13 /**
14 * Represents a conversation message.
15 *
16 * @author Marcel Werk
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
20 *
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
33 */
34 class ConversationMessage extends DatabaseObject implements IMessage {
35 use TUserContent;
36
37 /**
38 * conversation object
39 * @var Conversation
40 */
41 protected $conversation;
42
43 /**
44 * @inheritDoc
45 */
46 public function getFormattedMessage() {
47 $processor = new HtmlOutputProcessor();
48 $processor->process($this->message, 'com.woltlab.wcf.conversation.message', $this->messageID);
49
50 return $processor->getHtml();
51 }
52
53 /**
54 * Returns a simplified version of the formatted message.
55 *
56 * @return string
57 */
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);
62
63 return $processor->getHtml();
64 }
65
66 /**
67 * Assigns and returns the embedded attachments.
68 *
69 * @param boolean $ignoreCache
70 * @return GroupedAttachmentList
71 */
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
80 ]);
81
82 if ($ignoreCache && !count($attachmentList)) {
83 return null;
84 }
85
86 return $attachmentList;
87 }
88
89 return null;
90 }
91
92 /**
93 * @inheritDoc
94 */
95 public function getExcerpt($maxLength = 255) {
96 return StringUtil::truncateHTML($this->getSimplifiedFormattedMessage(), $maxLength);
97 }
98
99 /**
100 * Returns a version of this message optimized for use in emails.
101 *
102 * @param string $mimeType Either 'text/plain' or 'text/html'
103 * @return string
104 */
105 public function getMailText($mimeType = 'text/plain') {
106 switch ($mimeType) {
107 case 'text/plain':
108 $processor = new HtmlOutputProcessor();
109 $processor->setOutputType('text/plain');
110 $processor->process($this->message, 'com.woltlab.wcf.conversation.message', $this->messageID);
111
112 return $processor->getHtml();
113 case 'text/html':
114 return $this->getSimplifiedFormattedMessage();
115 }
116
117 throw new \LogicException('Unreachable');
118 }
119
120 /**
121 * Returns the conversation of this message.
122 *
123 * @return Conversation
124 */
125 public function getConversation() {
126 if ($this->conversation === null) {
127 $this->conversation = Conversation::getUserConversation($this->conversationID, WCF::getUser()->userID);
128 }
129
130 return $this->conversation;
131 }
132
133 /**
134 * Sets the conversation of this message.
135 *
136 * @param Conversation $conversation
137 */
138 public function setConversation(Conversation $conversation) {
139 if ($this->conversationID == $conversation->conversationID) {
140 $this->conversation = $conversation;
141 }
142 }
143
144 /**
145 * Returns true if current user may edit this message.
146 *
147 * @return boolean
148 */
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();
153 }
154
155 /**
156 * @inheritDoc
157 */
158 public function getMessage() {
159 return $this->message;
160 }
161
162 /**
163 * @inheritDoc
164 */
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);
171 }
172
173 /**
174 * @inheritDoc
175 */
176 public function getTitle() {
177 if ($this->messageID == $this->getConversation()->firstMessageID) {
178 return $this->getConversation()->subject;
179 }
180
181 return 'RE: '.$this->getConversation()->subject;
182 }
183
184 /**
185 * @inheritDoc
186 */
187 public function isVisible() {
188 return true;
189 }
190
191 /**
192 * @inheritDoc
193 */
194 public function __toString() {
195 return $this->getFormattedMessage();
196 }
197 }