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\user\User
;
9 use wcf\data\user\UserProfile
;
10 use wcf\system\cache\runtime\UserProfileRuntimeCache
;
11 use wcf\system\database\util\PreparedStatementConditionBuilder
;
15 * Represents a viewable conversation.
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
22 * @method Conversation getDecoratedObject()
24 * @property-read int|null $otherParticipantID
25 * @property-read string|null $otherParticipant
27 class ViewableConversation
extends DatabaseObjectDecorator
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 * Returns the user profile object.
69 public function getUserProfile()
71 if ($this->userProfile
=== null) {
73 $this->userProfile
= UserProfileRuntimeCache
::getInstance()->getObject($this->userID
);
75 $this->userProfile
= UserProfile
::getGuestUserProfile($this->username
);
79 return $this->userProfile
;
83 * Returns the last poster's profile object.
87 public function getLastPosterProfile()
89 if ($this->lastPosterProfile
=== null) {
90 if ($this->lastPosterID
) {
91 $this->lastPosterProfile
= UserProfileRuntimeCache
::getInstance()->getObject($this->lastPosterID
);
93 $this->lastPosterProfile
= UserProfile
::getGuestUserProfile($this->lastPoster
);
97 return $this->lastPosterProfile
;
101 * Returns the number of pages in this conversation.
105 public function getPages()
107 /** @noinspection PhpUndefinedFieldInspection */
108 if (WCF
::getUser()->conversationMessagesPerPage
) {
109 /** @noinspection PhpUndefinedFieldInspection */
110 $messagesPerPage = WCF
::getUser()->conversationMessagesPerPage
;
112 $messagesPerPage = CONVERSATION_MESSAGES_PER_PAGE
;
115 return \
intval(\
ceil(($this->replies +
1) / $messagesPerPage));
119 * Returns a summary of the participants.
123 public function getParticipantSummary()
125 if ($this->__participantSummary
=== null) {
126 $this->__participantSummary
= [];
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'],
142 return $this->__participantSummary
;
146 * Returns the other participant's profile object.
148 * @return UserProfile
150 public function getOtherParticipantProfile()
152 if ($this->otherParticipantProfile
=== null) {
153 if ($this->otherParticipantID
) {
154 $this->otherParticipantProfile
= UserProfileRuntimeCache
::getInstance()
155 ->getObject($this->otherParticipantID
);
157 $this->otherParticipantProfile
= UserProfile
::getGuestUserProfile($this->otherParticipant
);
161 return $this->otherParticipantProfile
;
167 * @param ConversationLabel $label
169 public function assignLabel(ConversationLabel
$label)
171 $this->labels
[$label->labelID
] = $label;
175 * Returns a list of assigned labels.
177 * @return ConversationLabel[]
179 public function getAssignedLabels()
181 return $this->labels
;
185 * Converts a conversation into a viewable conversation.
187 * @param Conversation $conversation
188 * @param ConversationLabelList $labelList
189 * @return ViewableConversation
191 public static function getViewableConversation(Conversation
$conversation, ?ConversationLabelList
$labelList = null)
193 $conversation = new self($conversation);
195 if ($labelList === null) {
196 $labelList = ConversationLabel
::getLabelsByUser();
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)]);
205 $sql = "SELECT labelID
206 FROM wcf" . WCF_N
. "_conversation_label_to_object
208 $statement = WCF
::getDB()->prepareStatement($sql);
209 $statement->execute($conditions->getParameters());
210 while ($row = $statement->fetchArray()) {
211 $conversation->assignLabel($labels[$row['labelID']]);
215 return $conversation;