Merge branch '5.5'
[GitHub/WoltLab/com.woltlab.wcf.conversation.git] / files / lib / data / conversation / ViewableConversation.class.php
CommitLineData
9544b6b4 1<?php
fea86294 2
9544b6b4 3namespace wcf\data\conversation;
fea86294 4
6579145d 5use wcf\data\conversation\label\ConversationLabel;
b5f8e83f 6use wcf\data\conversation\label\ConversationLabelList;
232cdc4b 7use wcf\data\DatabaseObjectDecorator;
fea86294
TD
8use wcf\data\user\User;
9use wcf\data\user\UserProfile;
95ed3132 10use wcf\system\cache\runtime\UserProfileRuntimeCache;
2e0bc870 11use wcf\system\database\util\PreparedStatementConditionBuilder;
9544b6b4
MW
12use wcf\system\WCF;
13
14/**
15 * Represents a viewable conversation.
fea86294
TD
16 *
17 * @author Marcel Werk
18 * @copyright 2001-2019 WoltLab GmbH
19 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
fea86294
TD
20 *
21 * @method Conversation getDecoratedObject()
22 * @mixin Conversation
c85e9df8 23 * @property-read int|null $otherParticipantID
fea86294 24 * @property-read string|null $otherParticipant
9544b6b4 25 */
fea86294
TD
26class ViewableConversation extends DatabaseObjectDecorator
27{
fea86294
TD
28 /**
29 * participant summary
30 * @var string
31 */
32 protected $__participantSummary;
33
34 /**
35 * user profile object
36 * @var UserProfile
37 */
38 protected $userProfile;
39
40 /**
41 * last poster's profile
42 * @var UserProfile
43 */
44 protected $lastPosterProfile;
45
46 /**
47 * other participant's profile
48 * @var UserProfile
49 */
50 protected $otherParticipantProfile;
51
52 /**
53 * list of assigned labels
54 * @var ConversationLabel[]
55 */
56 protected $labels = [];
57
58 /**
59 * @inheritDoc
60 */
61 protected static $baseClass = Conversation::class;
62
fea86294
TD
63 /**
64 * Returns the user profile object.
65 *
66 * @return UserProfile
67 */
68 public function getUserProfile()
69 {
70 if ($this->userProfile === null) {
71 if ($this->userID) {
72 $this->userProfile = UserProfileRuntimeCache::getInstance()->getObject($this->userID);
73 } else {
74 $this->userProfile = UserProfile::getGuestUserProfile($this->username);
75 }
76 }
77
78 return $this->userProfile;
79 }
80
81 /**
82 * Returns the last poster's profile object.
83 *
84 * @return UserProfile
85 */
86 public function getLastPosterProfile()
87 {
88 if ($this->lastPosterProfile === null) {
89 if ($this->lastPosterID) {
90 $this->lastPosterProfile = UserProfileRuntimeCache::getInstance()->getObject($this->lastPosterID);
91 } else {
92 $this->lastPosterProfile = UserProfile::getGuestUserProfile($this->lastPoster);
93 }
94 }
95
96 return $this->lastPosterProfile;
97 }
98
99 /**
100 * Returns the number of pages in this conversation.
101 *
c85e9df8 102 * @return int
fea86294
TD
103 */
104 public function getPages()
105 {
106 /** @noinspection PhpUndefinedFieldInspection */
107 if (WCF::getUser()->conversationMessagesPerPage) {
108 /** @noinspection PhpUndefinedFieldInspection */
109 $messagesPerPage = WCF::getUser()->conversationMessagesPerPage;
110 } else {
111 $messagesPerPage = CONVERSATION_MESSAGES_PER_PAGE;
112 }
113
114 return \intval(\ceil(($this->replies + 1) / $messagesPerPage));
115 }
116
117 /**
118 * Returns a summary of the participants.
119 *
120 * @return User[]
121 */
122 public function getParticipantSummary()
123 {
124 if ($this->__participantSummary === null) {
125 $this->__participantSummary = [];
126
127 if ($this->participantSummary) {
128 $data = \unserialize($this->participantSummary);
129 if ($data !== false) {
130 foreach ($data as $userData) {
131 $this->__participantSummary[] = new User(null, [
132 'userID' => $userData['userID'],
133 'username' => $userData['username'],
134 'hideConversation' => $userData['hideConversation'],
135 ]);
136 }
137 }
138 }
139 }
140
141 return $this->__participantSummary;
142 }
143
144 /**
145 * Returns the other participant's profile object.
146 *
147 * @return UserProfile
148 */
149 public function getOtherParticipantProfile()
150 {
151 if ($this->otherParticipantProfile === null) {
152 if ($this->otherParticipantID) {
153 $this->otherParticipantProfile = UserProfileRuntimeCache::getInstance()
154 ->getObject($this->otherParticipantID);
155 } else {
156 $this->otherParticipantProfile = UserProfile::getGuestUserProfile($this->otherParticipant);
157 }
158 }
159
160 return $this->otherParticipantProfile;
161 }
162
163 /**
164 * Assigns a label.
165 *
166 * @param ConversationLabel $label
167 */
168 public function assignLabel(ConversationLabel $label)
169 {
170 $this->labels[$label->labelID] = $label;
171 }
172
173 /**
174 * Returns a list of assigned labels.
175 *
176 * @return ConversationLabel[]
177 */
178 public function getAssignedLabels()
179 {
180 return $this->labels;
181 }
182
183 /**
184 * Converts a conversation into a viewable conversation.
185 *
186 * @param Conversation $conversation
187 * @param ConversationLabelList $labelList
188 * @return ViewableConversation
189 */
190 public static function getViewableConversation(Conversation $conversation, ?ConversationLabelList $labelList = null)
191 {
192 $conversation = new self($conversation);
193
194 if ($labelList === null) {
195 $labelList = ConversationLabel::getLabelsByUser();
196 }
197
198 $labels = $labelList->getObjects();
199 if (!empty($labels)) {
200 $conditions = new PreparedStatementConditionBuilder();
201 $conditions->add("conversationID = ?", [$conversation->conversationID]);
202 $conditions->add("labelID IN (?)", [\array_keys($labels)]);
203
8fbd8b01
MS
204 $sql = "SELECT labelID
205 FROM wcf" . WCF_N . "_conversation_label_to_object
206 " . $conditions;
fea86294
TD
207 $statement = WCF::getDB()->prepareStatement($sql);
208 $statement->execute($conditions->getParameters());
209 while ($row = $statement->fetchArray()) {
210 $conversation->assignLabel($labels[$row['labelID']]);
211 }
212 }
213
214 return $conversation;
215 }
9544b6b4 216}