2 namespace wcf\data\conversation
;
3 use wcf\data\conversation\label\ConversationLabel
;
4 use wcf\data\conversation\label\ConversationLabelList
;
5 use wcf\data\user\User
;
6 use wcf\data\user\UserProfile
;
7 use wcf\data\DatabaseObjectDecorator
;
8 use wcf\data\TLegacyUserPropertyAccess
;
9 use wcf\system\cache\runtime\UserProfileRuntimeCache
;
10 use wcf\system\database\util\PreparedStatementConditionBuilder
;
14 * Represents a viewable conversation.
17 * @copyright 2001-2018 WoltLab GmbH
18 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
19 * @package WoltLabSuite\Core\Data\Conversation
21 * @method Conversation getDecoratedObject()
23 * @property-read integer|null $otherParticipantID
24 * @property-read string|null $otherParticipant
26 class ViewableConversation
extends DatabaseObjectDecorator
{
27 use TLegacyUserPropertyAccess
;
33 protected $__participantSummary;
39 protected $userProfile;
42 * last poster's profile
45 protected $lastPosterProfile;
48 * other participant's profile
51 protected $otherParticipantProfile;
54 * list of assigned labels
55 * @var ConversationLabel[]
57 protected $labels = [];
62 protected static $baseClass = Conversation
::class;
65 * maps legacy direct access to last poster's user profile data to the real
66 * user profile property names
70 protected static $__lastUserAvatarPropertyMapping = [
71 'lastPosterAvatarID' => 'avatarID',
72 'lastPosterAvatarName' => 'avatarName',
73 'lastPosterAvatarExtension' => 'avatarExtension',
74 'lastPosterAvatarWidth' => 'width',
75 'lastPosterAvatarHeight' => 'height',
76 'lastPosterEmail' => 'email',
77 'lastPosterDisableAvatar' => 'disableAvatar',
78 'lastPosterEnableGravatar' => 'enableGravatar',
79 'lastPosterGravatarFileExtension' => 'gravatarFileExtension',
80 'lastPosterAvatarFileHash' => 'fileHash'
87 public function __get($name) {
88 $value = parent
::__get($name);
89 if ($value !== null) {
92 else if (array_key_exists($name, $this->object->data
)) {
96 /** @noinspection PhpVariableVariableInspection */
97 $value = $this->getUserProfile()->$name;
98 if ($value !== null) {
102 if (isset(static::$__lastUserAvatarPropertyMapping[$name])) {
103 return $this->getLastPosterProfile()->getAvatar()->{static::$__lastUserAvatarPropertyMapping[$name]};
110 * Returns the user profile object.
112 * @return UserProfile
114 public function getUserProfile() {
115 if ($this->userProfile
=== null) {
117 $this->userProfile
= UserProfileRuntimeCache
::getInstance()->getObject($this->userID
);
120 $this->userProfile
= UserProfile
::getGuestUserProfile($this->username
);
124 return $this->userProfile
;
128 * Returns the last poster's profile object.
130 * @return UserProfile
132 public function getLastPosterProfile() {
133 if ($this->lastPosterProfile
=== null) {
134 if ($this->lastPosterID
) {
135 $this->lastPosterProfile
= UserProfileRuntimeCache
::getInstance()->getObject($this->lastPosterID
);
138 $this->lastPosterProfile
= UserProfile
::getGuestUserProfile($this->lastPoster
);
142 return $this->lastPosterProfile
;
146 * Returns the number of pages in this conversation.
150 public function getPages() {
151 /** @noinspection PhpUndefinedFieldInspection */
152 if (WCF
::getUser()->conversationMessagesPerPage
) {
153 /** @noinspection PhpUndefinedFieldInspection */
154 $messagesPerPage = WCF
::getUser()->conversationMessagesPerPage
;
157 $messagesPerPage = CONVERSATION_MESSAGES_PER_PAGE
;
160 return intval(ceil(($this->replies +
1) / $messagesPerPage));
164 * Returns a summary of the participants.
168 public function getParticipantSummary() {
169 if ($this->__participantSummary
=== null) {
170 $this->__participantSummary
= [];
172 if ($this->participantSummary
) {
173 $data = unserialize($this->participantSummary
);
174 if ($data !== false) {
175 foreach ($data as $userData) {
176 $this->__participantSummary
[] = new User(null, [
177 'userID' => $userData['userID'],
178 'username' => $userData['username'],
179 'hideConversation' => $userData['hideConversation']
186 return $this->__participantSummary
;
190 * Returns the other participant's profile object.
192 * @return UserProfile
194 public function getOtherParticipantProfile() {
195 if ($this->otherParticipantProfile
=== null) {
196 if ($this->otherParticipantID
) {
197 $this->otherParticipantProfile
= UserProfileRuntimeCache
::getInstance()->getObject($this->otherParticipantID
);
200 $this->otherParticipantProfile
= UserProfile
::getGuestUserProfile($this->otherParticipant
);
204 return $this->otherParticipantProfile
;
210 * @param ConversationLabel $label
212 public function assignLabel(ConversationLabel
$label) {
213 $this->labels
[$label->labelID
] = $label;
217 * Returns a list of assigned labels.
219 * @return ConversationLabel[]
221 public function getAssignedLabels() {
222 return $this->labels
;
226 * Converts a conversation into a viewable conversation.
228 * @param Conversation $conversation
229 * @param ConversationLabelList $labelList
230 * @return ViewableConversation
232 public static function getViewableConversation(Conversation
$conversation, ConversationLabelList
$labelList = null) {
233 $conversation = new ViewableConversation($conversation);
235 if ($labelList === null) {
236 $labelList = ConversationLabel
::getLabelsByUser();
239 $labels = $labelList->getObjects();
240 if (!empty($labels)) {
241 $conditions = new PreparedStatementConditionBuilder();
242 $conditions->add("conversationID = ?", [$conversation->conversationID
]);
243 $conditions->add("labelID IN (?)", [array_keys($labels)]);
245 $sql = "SELECT labelID
246 FROM wcf".WCF_N
."_conversation_label_to_object
248 $statement = WCF
::getDB()->prepareStatement($sql);
249 $statement->execute($conditions->getParameters());
250 while ($row = $statement->fetchArray()) {
251 $conversation->assignLabel($labels[$row['labelID']]);
255 return $conversation;