Added users online list pagination
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / page / UsersOnlineListPage.class.php
1 <?php
2 namespace wcf\page;
3 use wcf\data\page\PageCache;
4 use wcf\data\user\online\UserOnline;
5 use wcf\data\user\online\UsersOnlineList;
6 use wcf\system\page\handler\IOnlineLocationPageHandler;
7 use wcf\system\page\PageLocationManager;
8 use wcf\system\request\LinkHandler;
9 use wcf\system\WCF;
10 use wcf\util\HeaderUtil;
11
12 /**
13 * Shows page which lists all users who are online.
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
19 * @subpackage page
20 * @category Community Framework
21 *
22 * @property UsersOnlineList $objectList
23 */
24 class UsersOnlineListPage extends SortablePage {
25 /**
26 * @inheritDoc
27 */
28 public $neededPermissions = ['user.profile.canViewUsersOnlineList'];
29
30 /**
31 * @inheritDoc
32 */
33 public $enableTracking = true;
34
35 /**
36 * @inheritDoc
37 */
38 public $itemsPerPage = 100;
39
40 /**
41 * @inheritDoc
42 */
43 public $defaultSortField = USERS_ONLINE_DEFAULT_SORT_FIELD;
44
45 /**
46 * @inheritDoc
47 */
48 public $defaultSortOrder = USERS_ONLINE_DEFAULT_SORT_ORDER;
49
50 /**
51 * @inheritDoc
52 */
53 public $validSortFields = ['username', 'lastActivityTime', 'requestURI'];
54
55 /**
56 * @inheritDoc
57 */
58 public $objectListClassName = UsersOnlineList::class;
59
60 /**
61 * page locations
62 * @var array
63 */
64 public $locations = [];
65
66 /**
67 * @inheritDoc
68 */
69 public function readParameters() {
70 parent::readParameters();
71
72 if (WCF::getSession()->getPermission('admin.user.canViewIpAddress')) {
73 $this->validSortFields[] = 'ipAddress';
74 $this->validSortFields[] = 'userAgent';
75 }
76
77 if (!empty($_POST)) {
78 HeaderUtil::redirect(LinkHandler::getInstance()->getLink('UsersOnlineList', [], 'sortField=' . $this->sortField . '&sortOrder=' . $this->sortOrder));
79 exit;
80 }
81 }
82
83 /**
84 * @inheritDoc
85 */
86 protected function initObjectList() {
87 parent::initObjectList();
88 $this->objectList->readStats();
89 $this->objectList->checkRecord();
90
91 if (!USERS_ONLINE_SHOW_ROBOTS) {
92 $this->objectList->getConditionBuilder()->add('session.spiderID IS NULL');
93 }
94 if (!USERS_ONLINE_SHOW_GUESTS) {
95 if (USERS_ONLINE_SHOW_ROBOTS) {
96 $this->objectList->getConditionBuilder()->add('(session.userID IS NOT NULL OR session.spiderID IS NOT NULL)');
97 }
98 else {
99 $this->objectList->getConditionBuilder()->add('session.userID IS NOT NULL');
100 }
101 }
102 }
103
104 /**
105 * @inheritDoc
106 */
107 public function readData() {
108 parent::readData();
109
110 // add breadcrumbs
111 if (MODULE_MEMBERS_LIST) PageLocationManager::getInstance()->addParentLocation('com.woltlab.wcf.MembersList');
112
113 // cache all necessary data for showing locations
114 foreach ($this->objectList as $userOnline) {
115 if ($userOnline->controller) {
116 $page = PageCache::getInstance()->getPageByController($userOnline->controller);
117 if ($page !== null && $page->getHandler() !== null && $page->getHandler() instanceof IOnlineLocationPageHandler) {
118 $page->getHandler()->prepareOnlineLocation($page, $userOnline);
119 }
120 }
121 }
122
123 // set locations
124 /** @var UserOnline $userOnline */
125 foreach ($this->objectList as $userOnline) {
126 $userOnline->setLocation();
127 }
128 }
129
130 /**
131 * @inheritDoc
132 */
133 public function assignVariables() {
134 parent::assignVariables();
135
136 WCF::getTPL()->assign([
137 'allowSpidersToIndexThisPage' => true
138 ]);
139 }
140
141 /**
142 * @inheritDoc
143 */
144 protected function readObjects() {
145 if ($this->sqlOrderBy) $this->sqlOrderBy = ($this->sortField == 'lastActivityTime' ? 'session.' : '').$this->sqlOrderBy;
146 parent::readObjects();
147 }
148 }