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