Merge branch '2.0' into 2.1
[GitHub/WoltLab/com.woltlab.wcf.conversation.git] / files / lib / data / conversation / ViewableConversation.class.php
1 <?php
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\system\database\util\PreparedStatementConditionBuilder;
9 use wcf\system\WCF;
10
11 /**
12 * Represents a viewable conversation.
13 *
14 * @author Marcel Werk
15 * @copyright 2001-2015 WoltLab GmbH
16 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
17 * @package com.woltlab.wcf.conversation
18 * @subpackage data.conversation
19 * @category Community Framework
20 */
21 class ViewableConversation extends DatabaseObjectDecorator {
22 /**
23 * participant summary
24 * @var string
25 */
26 protected $__participantSummary = null;
27
28 /**
29 * user profile object
30 * @var \wcf\data\user\UserProfile
31 */
32 protected $userProfile = null;
33
34 /**
35 * last poster's profile
36 * @var \wcf\data\user\UserProfile
37 */
38 protected $lastPosterProfile = null;
39
40 /**
41 * @see \wcf\data\DatabaseObjectDecorator::$baseClass
42 */
43 protected static $baseClass = 'wcf\data\conversation\Conversation';
44
45 /**
46 * list of assigned labels
47 * @var array<\wcf\data\conversation\label\ConversationLabel>
48 */
49 protected $labels = array();
50
51 /**
52 * Returns the user profile object.
53 *
54 * @return \wcf\data\user\UserProfile
55 */
56 public function getUserProfile() {
57 if ($this->userProfile === null) {
58 $this->userProfile = new UserProfile(new User(null, $this->getDecoratedObject()->data));
59 }
60
61 return $this->userProfile;
62 }
63
64 /**
65 * Returns the last poster's profile object.
66 *
67 * @return \wcf\data\user\UserProfile
68 */
69 public function getLastPosterProfile() {
70 if ($this->lastPosterProfile === null) {
71 if ($this->userID && $this->userID == $this->lastPosterID) {
72 $this->lastPosterProfile = $this->getUserProfile();
73 }
74 else {
75 $this->lastPosterProfile = new UserProfile(new User(null, array(
76 'userID' => $this->lastPosterID,
77 'username' => $this->lastPoster,
78 'avatarID' => $this->lastPosterAvatarID,
79 'avatarName' => $this->lastPosterAvatarName,
80 'avatarExtension' => $this->lastPosterAvatarExtension,
81 'width' => $this->lastPosterAvatarWidth,
82 'height' => $this->lastPosterAvatarHeight,
83 'email' => $this->lastPosterEmail,
84 'disableAvatar' => $this->lastPosterDisableAvatar,
85 'enableGravatar' => $this->lastPosterEnableGravatar,
86 'gravatarFileExtension' => $this->lastPosterGravatarFileExtension
87 )));
88 }
89 }
90
91 return $this->lastPosterProfile;
92 }
93
94 /**
95 * Returns the number of pages in this conversation.
96 *
97 * @return integer
98 */
99 public function getPages() {
100 if (WCF::getUser()->conversationMessagesPerPage) {
101 $messagesPerPage = WCF::getUser()->conversationMessagesPerPage;
102 }
103 else {
104 $messagesPerPage = CONVERSATION_MESSAGES_PER_PAGE;
105 }
106
107 return intval(ceil(($this->replies + 1) / $messagesPerPage));
108 }
109
110 /**
111 * Returns a summary of the participants.
112 *
113 * @return array<\wcf\data\user\User>
114 */
115 public function getParticipantSummary() {
116 if ($this->__participantSummary === null) {
117 $this->__participantSummary = array();
118
119 if ($this->participantSummary) {
120 $data = unserialize($this->participantSummary);
121 if ($data !== false) {
122 foreach ($data as $userData) {
123 $this->__participantSummary[] = new User(null, array(
124 'userID' => $userData['userID'],
125 'username' => $userData['username'],
126 'hideConversation' => $userData['hideConversation']
127 ));
128 }
129 }
130 }
131 }
132
133 return $this->__participantSummary;
134 }
135
136 /**
137 * Assigns a label.
138 *
139 * @param \wcf\data\conversation\label\ConversationLabel $label
140 */
141 public function assignLabel(ConversationLabel $label) {
142 $this->labels[$label->labelID] = $label;
143 }
144
145 /**
146 * Returns a list of assigned labels.
147 *
148 * @return array<\wcf\data\conversation\label\ConversationLabel>
149 */
150 public function getAssignedLabels() {
151 return $this->labels;
152 }
153
154 /**
155 * Converts a conversation into a viewable conversation.
156 *
157 * @param \wcf\data\conversation\Conversation $conversation
158 * @param \wcf\data\conversation\label\ConversationLabelList $labelList
159 * @return \wcf\data\conversation\ViewableConversation
160 */
161 public static function getViewableConversation(Conversation $conversation, ConversationLabelList $labelList = null) {
162 $conversation = new ViewableConversation($conversation);
163
164 if ($labelList === null) {
165 $labelList = ConversationLabel::getLabelsByUser();
166 }
167
168 $labels = $labelList->getObjects();
169 if (!empty($labels)) {
170 $conditions = new PreparedStatementConditionBuilder();
171 $conditions->add("conversationID = ?", array($conversation->conversationID));
172 $conditions->add("labelID IN (?)", array(array_keys($labels)));
173
174 $sql = "SELECT labelID
175 FROM wcf".WCF_N."_conversation_label_to_object
176 ".$conditions;
177 $statement = WCF::getDB()->prepareStatement($sql);
178 $statement->execute($conditions->getParameters());
179 $data = array();
180 while ($row = $statement->fetchArray()) {
181 $conversation->assignLabel($labels[$row['labelID']]);
182 }
183 }
184
185 return $conversation;
186 }
187 }