Commit | Line | Data |
---|---|---|
542f62dd MW |
1 | <?php |
2 | namespace wcf\system\box; | |
542f62dd MW |
3 | use wcf\data\user\online\UsersOnlineList; |
4 | use wcf\data\user\UserProfile; | |
e88fd62b | 5 | use wcf\data\DatabaseObject; |
542f62dd MW |
6 | use wcf\system\cache\builder\WhoWasOnlineCacheBuilder; |
7 | use wcf\system\cache\runtime\UserProfileRuntimeCache; | |
8 | use wcf\system\event\EventHandler; | |
9 | use wcf\system\WCF; | |
af202eb1 | 10 | use 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 | |
16 | * @copyright 2001-2016 WoltLab GmbH | |
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 | */ | |
21 | class 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 |
542f62dd MW |
61 | ]); |
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 | ||
100 | $this->users = UserProfileRuntimeCache::getInstance()->getObjects($userIDs); | |
101 | foreach ($this->users as $key => $user) { | |
102 | // remove invisible users | |
103 | if (!UsersOnlineList::isVisible($user->userID, $user->canViewOnlineStatus)) { | |
104 | unset($this->users[$key]); | |
105 | } | |
106 | } | |
107 | ||
108 | // sort users | |
109 | if (!empty($this->users)) { | |
110 | DatabaseObject::sort($this->users, $this->sortField, $this->sortOrder); | |
111 | } | |
112 | } | |
113 | } | |
114 | } |