3 namespace wcf\data\conversation
;
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
;
16 * Represents a viewable conversation.
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
23 * @method Conversation getDecoratedObject()
25 * @property-read integer|null $otherParticipantID
26 * @property-read string|null $otherParticipant
28 class ViewableConversation
extends DatabaseObjectDecorator
30 use TLegacyUserPropertyAccess
;
36 protected $__participantSummary;
42 protected $userProfile;
45 * last poster's profile
48 protected $lastPosterProfile;
51 * other participant's profile
54 protected $otherParticipantProfile;
57 * list of assigned labels
58 * @var ConversationLabel[]
60 protected $labels = [];
65 protected static $baseClass = Conversation
::class;
68 * maps legacy direct access to last poster's user profile data to the real
69 * user profile property names
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',
90 public function __get($name)
92 $value = parent
::__get($name);
93 if ($value !== null) {
95 } elseif (\array_key_exists
($name, $this->object->data
)) {
99 /** @noinspection PhpVariableVariableInspection */
100 $value = $this->getUserProfile()->{$name};
101 if ($value !== null) {
105 if (isset(static::$__lastUserAvatarPropertyMapping[$name])) {
106 return $this->getLastPosterProfile()->getAvatar()->{static::$__lastUserAvatarPropertyMapping[$name]};
111 * Returns the user profile object.
113 * @return UserProfile
115 public function getUserProfile()
117 if ($this->userProfile
=== null) {
119 $this->userProfile
= UserProfileRuntimeCache
::getInstance()->getObject($this->userID
);
121 $this->userProfile
= UserProfile
::getGuestUserProfile($this->username
);
125 return $this->userProfile
;
129 * Returns the last poster's profile object.
131 * @return UserProfile
133 public function getLastPosterProfile()
135 if ($this->lastPosterProfile
=== null) {
136 if ($this->lastPosterID
) {
137 $this->lastPosterProfile
= UserProfileRuntimeCache
::getInstance()->getObject($this->lastPosterID
);
139 $this->lastPosterProfile
= UserProfile
::getGuestUserProfile($this->lastPoster
);
143 return $this->lastPosterProfile
;
147 * Returns the number of pages in this conversation.
151 public function getPages()
153 /** @noinspection PhpUndefinedFieldInspection */
154 if (WCF
::getUser()->conversationMessagesPerPage
) {
155 /** @noinspection PhpUndefinedFieldInspection */
156 $messagesPerPage = WCF
::getUser()->conversationMessagesPerPage
;
158 $messagesPerPage = CONVERSATION_MESSAGES_PER_PAGE
;
161 return \
intval(\
ceil(($this->replies +
1) / $messagesPerPage));
165 * Returns a summary of the participants.
169 public function getParticipantSummary()
171 if ($this->__participantSummary
=== null) {
172 $this->__participantSummary
= [];
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'],
188 return $this->__participantSummary
;
192 * Returns the other participant's profile object.
194 * @return UserProfile
196 public function getOtherParticipantProfile()
198 if ($this->otherParticipantProfile
=== null) {
199 if ($this->otherParticipantID
) {
200 $this->otherParticipantProfile
= UserProfileRuntimeCache
::getInstance()
201 ->getObject($this->otherParticipantID
);
203 $this->otherParticipantProfile
= UserProfile
::getGuestUserProfile($this->otherParticipant
);
207 return $this->otherParticipantProfile
;
213 * @param ConversationLabel $label
215 public function assignLabel(ConversationLabel
$label)
217 $this->labels
[$label->labelID
] = $label;
221 * Returns a list of assigned labels.
223 * @return ConversationLabel[]
225 public function getAssignedLabels()
227 return $this->labels
;
231 * Converts a conversation into a viewable conversation.
233 * @param Conversation $conversation
234 * @param ConversationLabelList $labelList
235 * @return ViewableConversation
237 public static function getViewableConversation(Conversation
$conversation, ?ConversationLabelList
$labelList = null)
239 $conversation = new self($conversation);
241 if ($labelList === null) {
242 $labelList = ConversationLabel
::getLabelsByUser();
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)]);
251 $sql = "SELECT labelID
252 FROM wcf" . WCF_N
. "_conversation_label_to_object
254 $statement = WCF
::getDB()->prepareStatement($sql);
255 $statement->execute($conditions->getParameters());
256 while ($row = $statement->fetchArray()) {
257 $conversation->assignLabel($labels[$row['labelID']]);
261 return $conversation;