Add $objectList @property to page classes
[GitHub/WoltLab/com.woltlab.wcf.conversation.git] / files / lib / page / ConversationListPage.class.php
1 <?php
2 namespace wcf\page;
3 use wcf\data\conversation\label\ConversationLabel;
4 use wcf\data\conversation\label\ConversationLabelList;
5 use wcf\data\conversation\UserConversationList;
6 use wcf\system\clipboard\ClipboardHandler;
7 use wcf\system\exception\IllegalLinkException;
8 use wcf\system\page\PageLocationManager;
9 use wcf\system\WCF;
10 use wcf\util\ArrayUtil;
11
12 /**
13 * Shows a list of conversations.
14 *
15 * @author Marcel Werk
16 * @copyright 2001-2016 WoltLab GmbH
17 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
18 * @package com.woltlab.wcf.conversation
19 * @subpackage page
20 * @category Community Framework
21 *
22 * @property UserConversationList $objectList
23 */
24 class ConversationListPage extends SortablePage {
25 /**
26 * @inheritDoc
27 */
28 public $enableTracking = true;
29
30 /**
31 * @inheritDoc
32 */
33 public $defaultSortField = CONVERSATION_LIST_DEFAULT_SORT_FIELD;
34
35 /**
36 * @inheritDoc
37 */
38 public $defaultSortOrder = CONVERSATION_LIST_DEFAULT_SORT_ORDER;
39
40 /**
41 * @inheritDoc
42 */
43 public $validSortFields = ['subject', 'time', 'username', 'lastPostTime', 'replies', 'participants'];
44
45 /**
46 * @inheritDoc
47 */
48 public $itemsPerPage = CONVERSATIONS_PER_PAGE;
49
50 /**
51 * @inheritDoc
52 */
53 public $loginRequired = true;
54
55 /**
56 * @inheritDoc
57 */
58 public $neededModules = ['MODULE_CONVERSATION'];
59
60 /**
61 * @inheritDoc
62 */
63 public $neededPermissions = ['user.conversation.canUseConversation'];
64
65 /**
66 * list filter
67 * @var string
68 */
69 public $filter = '';
70
71 /**
72 * label id
73 * @var integer
74 */
75 public $labelID = 0;
76
77 /**
78 * label list object
79 * @var ConversationLabelList
80 */
81 public $labelList = null;
82
83 /**
84 * number of conversations (no filter)
85 * @var integer
86 */
87 public $conversationCount = 0;
88
89 /**
90 * number of drafts
91 * @var integer
92 */
93 public $draftCount = 0;
94
95 /**
96 * number of hidden conversations
97 * @var integer
98 */
99 public $hiddenCount = 0;
100
101 /**
102 * number of sent conversations
103 * @var integer
104 */
105 public $outboxCount = 0;
106
107 /**
108 * participant that
109 * @var string[]
110 */
111 public $participants = [];
112
113 /**
114 * @inheritDoc
115 */
116 public function readParameters() {
117 parent::readParameters();
118
119 if (isset($_REQUEST['filter'])) $this->filter = $_REQUEST['filter'];
120 if (!in_array($this->filter, UserConversationList::$availableFilters)) $this->filter = '';
121
122 // user settings
123 if (WCF::getUser()->conversationsPerPage) $this->itemsPerPage = WCF::getUser()->conversationsPerPage;
124
125 // labels
126 $this->labelList = ConversationLabel::getLabelsByUser();
127 if (isset($_REQUEST['labelID'])) {
128 $this->labelID = intval($_REQUEST['labelID']);
129
130 $validLabel = false;
131 foreach ($this->labelList as $label) {
132 if ($label->labelID == $this->labelID) {
133 $validLabel = true;
134 break;
135 }
136 }
137
138 if (!$validLabel) {
139 throw new IllegalLinkException();
140 }
141 }
142
143 if (isset($_REQUEST['participants'])) $this->participants = ArrayUtil::trim(explode(',', $_REQUEST['participants']));
144 }
145
146 /**
147 * @inheritDoc
148 */
149 protected function initObjectList() {
150 $this->objectList = new UserConversationList(WCF::getUser()->userID, $this->filter, $this->labelID);
151 $this->objectList->setLabelList($this->labelList);
152
153 if (!empty($this->participants)) {
154 $this->objectList->getConditionBuilder()->add('conversation.conversationID IN (SELECT conversationID FROM wcf'.WCF_N.'_conversation_to_user WHERE username IN (?) GROUP BY conversationID HAVING COUNT(conversationID) = ?)', [
155 $this->participants,
156 count($this->participants)
157 ]);
158 }
159 }
160
161 /**
162 * @inheritDoc
163 */
164 public function readData() {
165 parent::readData();
166
167 if ($this->filter != '') {
168 // add breadcrumbs
169 // TODO: this is not working at the moment as PageLocationManager already sets this as the current page based on request data which is later discarded
170 PageLocationManager::getInstance()->addParentLocation('com.woltlab.wcf.conversation.ConversationList');
171 }
172
173 // read stats
174 if (!$this->labelID && empty($this->participants)) {
175 switch ($this->filter) {
176 case '':
177 $this->conversationCount = $this->items;
178 break;
179
180 case 'draft':
181 $this->draftCount = $this->items;
182 break;
183
184 case 'hidden':
185 $this->hiddenCount = $this->items;
186 break;
187
188 case 'outbox':
189 $this->outboxCount = $this->items;
190 break;
191 }
192 }
193
194 if ($this->filter != '' || $this->labelID || !empty($this->participants)) {
195 $conversationList = new UserConversationList(WCF::getUser()->userID, '');
196 $this->conversationCount = $conversationList->countObjects();
197 }
198 if ($this->filter != 'draft' || $this->labelID || !empty($this->participants)) {
199 $conversationList = new UserConversationList(WCF::getUser()->userID, 'draft');
200 $this->draftCount = $conversationList->countObjects();
201 }
202 if ($this->filter != 'hidden' || $this->labelID || !empty($this->participants)) {
203 $conversationList = new UserConversationList(WCF::getUser()->userID, 'hidden');
204 $this->hiddenCount = $conversationList->countObjects();
205 }
206 if ($this->filter != 'outbox' || $this->labelID || !empty($this->participants)) {
207 $conversationList = new UserConversationList(WCF::getUser()->userID, 'outbox');
208 $this->outboxCount = $conversationList->countObjects();
209 }
210 }
211
212 /**
213 * @inheritDoc
214 */
215 public function assignVariables() {
216 parent::assignVariables();
217
218 WCF::getTPL()->assign([
219 'filter' => $this->filter,
220 'hasMarkedItems' => ClipboardHandler::getInstance()->hasMarkedItems(ClipboardHandler::getInstance()->getObjectTypeID('com.woltlab.wcf.conversation.conversation')),
221 'labelID' => $this->labelID,
222 'labelList' => $this->labelList,
223 'conversationCount' => $this->conversationCount,
224 'draftCount' => $this->draftCount,
225 'hiddenCount' => $this->hiddenCount,
226 'outboxCount' => $this->outboxCount,
227 'participants' => $this->participants
228 ]);
229 }
230 }