Apply PSR-12 code style (#145)
[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\TLegacyUserPropertyAccess;
9 use wcf\data\user\User;
10 use wcf\data\user\UserProfile;
11 use wcf\system\cache\runtime\UserProfileRuntimeCache;
12 use wcf\system\database\util\PreparedStatementConditionBuilder;
13 use wcf\system\WCF;
14
15 /**
16 * Represents a viewable conversation.
17 *
18 * @author Marcel Werk
19 * @copyright 2001-2019 WoltLab GmbH
20 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
21 * @package WoltLabSuite\Core\Data\Conversation
22 *
23 * @method Conversation getDecoratedObject()
24 * @mixin Conversation
25 * @property-read integer|null $otherParticipantID
26 * @property-read string|null $otherParticipant
27 */
28 class ViewableConversation extends DatabaseObjectDecorator
29 {
30 use TLegacyUserPropertyAccess;
31
32 /**
33 * participant summary
34 * @var string
35 */
36 protected $__participantSummary;
37
38 /**
39 * user profile object
40 * @var UserProfile
41 */
42 protected $userProfile;
43
44 /**
45 * last poster's profile
46 * @var UserProfile
47 */
48 protected $lastPosterProfile;
49
50 /**
51 * other participant's profile
52 * @var UserProfile
53 */
54 protected $otherParticipantProfile;
55
56 /**
57 * list of assigned labels
58 * @var ConversationLabel[]
59 */
60 protected $labels = [];
61
62 /**
63 * @inheritDoc
64 */
65 protected static $baseClass = Conversation::class;
66
67 /**
68 * maps legacy direct access to last poster's user profile data to the real
69 * user profile property names
70 * @var string[]
71 * @deprecated
72 */
73 protected static $__lastUserAvatarPropertyMapping = [
74 'lastPosterAvatarID' => 'avatarID',
75 'lastPosterAvatarName' => 'avatarName',
76 'lastPosterAvatarExtension' => 'avatarExtension',
77 'lastPosterAvatarWidth' => 'width',
78 'lastPosterAvatarHeight' => 'height',
79 'lastPosterEmail' => 'email',
80 'lastPosterDisableAvatar' => 'disableAvatar',
81 'lastPosterEnableGravatar' => 'enableGravatar',
82 'lastPosterGravatarFileExtension' => 'gravatarFileExtension',
83 'lastPosterAvatarFileHash' => 'fileHash',
84 ];
85
86 /**
87 * @inheritDoc
88 * @deprecated
89 */
90 public function __get($name)
91 {
92 $value = parent::__get($name);
93 if ($value !== null) {
94 return $value;
95 } elseif (\array_key_exists($name, $this->object->data)) {
96 return;
97 }
98
99 /** @noinspection PhpVariableVariableInspection */
100 $value = $this->getUserProfile()->{$name};
101 if ($value !== null) {
102 return $value;
103 }
104
105 if (isset(static::$__lastUserAvatarPropertyMapping[$name])) {
106 return $this->getLastPosterProfile()->getAvatar()->{static::$__lastUserAvatarPropertyMapping[$name]};
107 }
108 }
109
110 /**
111 * Returns the user profile object.
112 *
113 * @return UserProfile
114 */
115 public function getUserProfile()
116 {
117 if ($this->userProfile === null) {
118 if ($this->userID) {
119 $this->userProfile = UserProfileRuntimeCache::getInstance()->getObject($this->userID);
120 } else {
121 $this->userProfile = UserProfile::getGuestUserProfile($this->username);
122 }
123 }
124
125 return $this->userProfile;
126 }
127
128 /**
129 * Returns the last poster's profile object.
130 *
131 * @return UserProfile
132 */
133 public function getLastPosterProfile()
134 {
135 if ($this->lastPosterProfile === null) {
136 if ($this->lastPosterID) {
137 $this->lastPosterProfile = UserProfileRuntimeCache::getInstance()->getObject($this->lastPosterID);
138 } else {
139 $this->lastPosterProfile = UserProfile::getGuestUserProfile($this->lastPoster);
140 }
141 }
142
143 return $this->lastPosterProfile;
144 }
145
146 /**
147 * Returns the number of pages in this conversation.
148 *
149 * @return integer
150 */
151 public function getPages()
152 {
153 /** @noinspection PhpUndefinedFieldInspection */
154 if (WCF::getUser()->conversationMessagesPerPage) {
155 /** @noinspection PhpUndefinedFieldInspection */
156 $messagesPerPage = WCF::getUser()->conversationMessagesPerPage;
157 } else {
158 $messagesPerPage = CONVERSATION_MESSAGES_PER_PAGE;
159 }
160
161 return \intval(\ceil(($this->replies + 1) / $messagesPerPage));
162 }
163
164 /**
165 * Returns a summary of the participants.
166 *
167 * @return User[]
168 */
169 public function getParticipantSummary()
170 {
171 if ($this->__participantSummary === null) {
172 $this->__participantSummary = [];
173
174 if ($this->participantSummary) {
175 $data = \unserialize($this->participantSummary);
176 if ($data !== false) {
177 foreach ($data as $userData) {
178 $this->__participantSummary[] = new User(null, [
179 'userID' => $userData['userID'],
180 'username' => $userData['username'],
181 'hideConversation' => $userData['hideConversation'],
182 ]);
183 }
184 }
185 }
186 }
187
188 return $this->__participantSummary;
189 }
190
191 /**
192 * Returns the other participant's profile object.
193 *
194 * @return UserProfile
195 */
196 public function getOtherParticipantProfile()
197 {
198 if ($this->otherParticipantProfile === null) {
199 if ($this->otherParticipantID) {
200 $this->otherParticipantProfile = UserProfileRuntimeCache::getInstance()
201 ->getObject($this->otherParticipantID);
202 } else {
203 $this->otherParticipantProfile = UserProfile::getGuestUserProfile($this->otherParticipant);
204 }
205 }
206
207 return $this->otherParticipantProfile;
208 }
209
210 /**
211 * Assigns a label.
212 *
213 * @param ConversationLabel $label
214 */
215 public function assignLabel(ConversationLabel $label)
216 {
217 $this->labels[$label->labelID] = $label;
218 }
219
220 /**
221 * Returns a list of assigned labels.
222 *
223 * @return ConversationLabel[]
224 */
225 public function getAssignedLabels()
226 {
227 return $this->labels;
228 }
229
230 /**
231 * Converts a conversation into a viewable conversation.
232 *
233 * @param Conversation $conversation
234 * @param ConversationLabelList $labelList
235 * @return ViewableConversation
236 */
237 public static function getViewableConversation(Conversation $conversation, ?ConversationLabelList $labelList = null)
238 {
239 $conversation = new self($conversation);
240
241 if ($labelList === null) {
242 $labelList = ConversationLabel::getLabelsByUser();
243 }
244
245 $labels = $labelList->getObjects();
246 if (!empty($labels)) {
247 $conditions = new PreparedStatementConditionBuilder();
248 $conditions->add("conversationID = ?", [$conversation->conversationID]);
249 $conditions->add("labelID IN (?)", [\array_keys($labels)]);
250
251 $sql = "SELECT labelID
252 FROM wcf" . WCF_N . "_conversation_label_to_object
253 " . $conditions;
254 $statement = WCF::getDB()->prepareStatement($sql);
255 $statement->execute($conditions->getParameters());
256 while ($row = $statement->fetchArray()) {
257 $conversation->assignLabel($labels[$row['labelID']]);
258 }
259 }
260
261 return $conversation;
262 }
263 }