Add permission to restrict hiding online status (#3363)
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / system / box / WhoWasOnlineBoxController.class.php
CommitLineData
542f62dd
MW
1<?php
2namespace wcf\system\box;
542f62dd
MW
3use wcf\data\user\online\UsersOnlineList;
4use wcf\data\user\UserProfile;
e88fd62b 5use wcf\data\DatabaseObject;
542f62dd
MW
6use wcf\system\cache\builder\WhoWasOnlineCacheBuilder;
7use wcf\system\cache\runtime\UserProfileRuntimeCache;
8use wcf\system\event\EventHandler;
9use wcf\system\WCF;
af202eb1 10use wcf\util\DateUtil;
542f62dd
MW
11
12/**
13 * Box controller for a list of registered users that visited the website in last 24 hours.
14 *
15 * @author Marcel Werk
7b7b9764 16 * @copyright 2001-2019 WoltLab GmbH
542f62dd
MW
17 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
18 * @package WoltLabSuite\Core\System\Box
19 * @since 3.0
20 */
21class WhoWasOnlineBoxController extends AbstractDatabaseObjectListBoxController {
22 /**
23 * @inheritDoc
24 */
25 protected static $supportedPositions = ['footerBoxes', 'sidebarLeft', 'sidebarRight'];
26
27 /**
28 * @inheritDoc
29 */
868d21bb 30 protected $sortFieldLanguageItemPrefix = 'wcf.user.sortField';
542f62dd
MW
31
32 /**
33 * @inheritDoc
34 */
35 public $validSortFields = [
36 'username',
37 'lastActivityTime'
38 ];
39
40 /**
41 * users loaded from cache
42 * @var UserProfile[]
43 */
44 public $users = [];
45
46 /**
47 * @inheritDoc
48 */
49 protected function getObjectList() {
50 return null;
51 }
52
53 /**
54 * @inheritDoc
55 */
56 protected function getTemplate() {
57 return WCF::getTPL()->fetch('boxWhoWasOnline', 'wcf', [
58 'whoWasOnlineList' => $this->users,
59 'boxPosition' => $this->box->position,
af202eb1 60 'whoWasOnlineTimeFormat' => DateUtil::TIME_FORMAT
cfd71477 61 ], true);
542f62dd
MW
62 }
63
64 /**
65 * @inheritDoc
66 */
67 public function hasContent() {
68 if (!MODULE_USERS_ONLINE || !WCF::getSession()->getPermission('user.profile.canViewUsersOnlineList')) {
69 return false;
70 }
71
72 parent::hasContent();
73
74 return count($this->users) > 0;
75 }
76
77 /**
78 * @inheritDoc
79 */
80 protected function loadContent() {
81 $this->readObjects();
82
83 $this->content = $this->getTemplate();
84 }
85
86 /**
87 * @inheritDoc
88 */
89 protected function readObjects() {
90 EventHandler::getInstance()->fireAction($this, 'readObjects');
91
92 $userIDs = WhoWasOnlineCacheBuilder::getInstance()->getData();
93
94 if (!empty($userIDs)) {
95 if (WCF::getUser()->userID && !in_array(WCF::getUser()->userID, $userIDs)) {
96 // current user is missing in cache -> reset cache
97 WhoWasOnlineCacheBuilder::getInstance()->reset();
98 }
99
92263ae3
MW
100 $this->users = array_filter(UserProfileRuntimeCache::getInstance()->getObjects($userIDs), function($user) {
101 return $user !== null;
102 });
542f62dd
MW
103 foreach ($this->users as $key => $user) {
104 // remove invisible users
f990a9cc 105 if (!UsersOnlineList::isVisibleUser($user)) {
542f62dd
MW
106 unset($this->users[$key]);
107 }
108 }
109
110 // sort users
111 if (!empty($this->users)) {
112 DatabaseObject::sort($this->users, $this->sortField, $this->sortOrder);
113 }
114 }
115 }
116}