Skip to content

Commit c6a929e

Browse files
authored
[5.4] Convert mod_whosonline to service provider (joomla#45775)
mod_whosonline converted to service provider
1 parent 530b271 commit c6a929e

File tree

5 files changed

+210
-44
lines changed

5 files changed

+210
-44
lines changed

modules/mod_whosonline/mod_whosonline.php

Lines changed: 0 additions & 31 deletions
This file was deleted.

modules/mod_whosonline/mod_whosonline.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<description>MOD_WHOSONLINE_XML_DESCRIPTION</description>
1212
<namespace path="src">Joomla\Module\Whosonline</namespace>
1313
<files>
14-
<filename module="mod_whosonline">mod_whosonline.php</filename>
14+
<folder module="mod_whosonline">services</folder>
1515
<folder>src</folder>
1616
<folder>tmpl</folder>
1717
</files>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
/**
4+
* @package Joomla.Site
5+
* @subpackage mod_whosonline
6+
*
7+
* @copyright (C) 2025 Open Source Matters, Inc. <https://www.joomla.org>
8+
* @license GNU General Public License version 2 or later; see LICENSE.txt
9+
*/
10+
11+
\defined('_JEXEC') or die;
12+
13+
use Joomla\CMS\Extension\Service\Provider\HelperFactory;
14+
use Joomla\CMS\Extension\Service\Provider\Module;
15+
use Joomla\CMS\Extension\Service\Provider\ModuleDispatcherFactory;
16+
use Joomla\DI\Container;
17+
use Joomla\DI\ServiceProviderInterface;
18+
19+
/**
20+
* The who's online module service provider.
21+
*
22+
* @since __DEPLOY_VERSION__
23+
*/
24+
return new class () implements ServiceProviderInterface {
25+
/**
26+
* Registers the service provider with a DI container.
27+
*
28+
* @param Container $container The DI container.
29+
*
30+
* @return void
31+
*
32+
* @since __DEPLOY_VERSION__
33+
*/
34+
public function register(Container $container): void
35+
{
36+
$container->registerServiceProvider(new ModuleDispatcherFactory('\\Joomla\\Module\\Whosonline'));
37+
$container->registerServiceProvider(new HelperFactory('\\Joomla\\Module\\Whosonline\\Site\\Helper'));
38+
39+
$container->registerServiceProvider(new Module());
40+
}
41+
};
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
3+
/**
4+
* @package Joomla.Site
5+
* @subpackage mod_whosonline
6+
*
7+
* @copyright (C) 2025 Open Source Matters, Inc. <https://www.joomla.org>
8+
* @license GNU General Public License version 2 or later; see LICENSE.txt
9+
*/
10+
11+
namespace Joomla\Module\Whosonline\Site\Dispatcher;
12+
13+
use Joomla\CMS\Dispatcher\AbstractModuleDispatcher;
14+
use Joomla\CMS\Helper\HelperFactoryAwareInterface;
15+
use Joomla\CMS\Helper\HelperFactoryAwareTrait;
16+
use Joomla\CMS\Helper\ModuleHelper;
17+
18+
// phpcs:disable PSR1.Files.SideEffects
19+
\defined('_JEXEC') or die;
20+
// phpcs:enable PSR1.Files.SideEffects
21+
22+
/**
23+
* Dispatcher class for mod_whosonline
24+
*
25+
* @since __DEPLOY_VERSION__
26+
*/
27+
class Dispatcher extends AbstractModuleDispatcher implements HelperFactoryAwareInterface
28+
{
29+
use HelperFactoryAwareTrait;
30+
31+
/**
32+
* Runs the dispatcher.
33+
*
34+
* @return void
35+
*
36+
* @since __DEPLOY_VERSION__
37+
*/
38+
public function dispatch()
39+
{
40+
$this->loadLanguage();
41+
42+
$displayData = $this->getLayoutData();
43+
44+
// Stop when display data is false
45+
if ($displayData === false) {
46+
return;
47+
}
48+
49+
// Execute the layout without the module context
50+
$loader = static function (array $displayData) {
51+
// If $displayData doesn't exist in extracted data, unset the variable.
52+
if (!\array_key_exists('displayData', $displayData)) {
53+
extract($displayData);
54+
unset($displayData);
55+
} else {
56+
extract($displayData);
57+
}
58+
59+
/**
60+
* Extracted variables
61+
* -----------------
62+
* @var \stdClass $module
63+
* @var Registry $params
64+
*/
65+
66+
if ($app->get('session_metadata', true)) {
67+
require ModuleHelper::getLayoutPath('mod_whosonline', $params->get('layout', 'default'));
68+
} else {
69+
require ModuleHelper::getLayoutPath('mod_whosonline', 'disabled');
70+
}
71+
};
72+
73+
$loader($displayData);
74+
}
75+
76+
/**
77+
* Returns the layout data.
78+
*
79+
* @return array
80+
*
81+
* @since __DEPLOY_VERSION__
82+
*/
83+
protected function getLayoutData(): array
84+
{
85+
$data = parent::getLayoutData();
86+
$helper = $this->getHelperFactory()->getHelper('WhosonlineHelper');
87+
88+
// Check if session metadata tracking is enabled
89+
if ($data['app']->get('session_metadata', true)) {
90+
$data['showmode'] = $data['params']->get('showmode', 0);
91+
92+
if ($data['showmode'] == 0 || $data['showmode'] == 2) {
93+
$data['count'] = $helper->getOnlineUsersCount($data['app']);
94+
}
95+
96+
if ($data['showmode'] > 0) {
97+
$data['names'] = $helper->fetchOnlineUserNames($data['app'], $data['params']);
98+
}
99+
}
100+
101+
return $data;
102+
}
103+
}

modules/mod_whosonline/src/Helper/WhosonlineHelper.php

Lines changed: 65 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010

1111
namespace Joomla\Module\Whosonline\Site\Helper;
1212

13+
use Joomla\CMS\Application\CMSApplicationInterface;
1314
use Joomla\CMS\Factory;
15+
use Joomla\Database\DatabaseAwareInterface;
16+
use Joomla\Database\DatabaseAwareTrait;
17+
use Joomla\Registry\Registry;
1418

1519
// phpcs:disable PSR1.Files.SideEffects
1620
\defined('_JEXEC') or die;
@@ -21,25 +25,29 @@
2125
*
2226
* @since 1.5
2327
*/
24-
class WhosonlineHelper
28+
class WhosonlineHelper implements DatabaseAwareInterface
2529
{
30+
use DatabaseAwareTrait;
31+
2632
/**
2733
* Show online count
2834
*
35+
* @param CMSApplicationInterface $app The application instance
36+
*
2937
* @return array The number of Users and Guests online.
3038
*
31-
* @since 1.5
39+
* @since __DEPLOY_VERSION__
3240
**/
33-
public static function getOnlineCount()
41+
public function getOnlineUsersCount(CMSApplicationInterface $app): array
3442
{
35-
$db = Factory::getDbo();
43+
$db = $this->getDatabase();
3644

3745
// Calculate number of guests and users
3846
$result = [];
3947
$user_array = 0;
4048
$guest_array = 0;
4149

42-
$whereCondition = Factory::getApplication()->get('shared_session', '0') ? 'IS NULL' : '= 0';
50+
$whereCondition = $app->get('shared_session', '0') ? 'IS NULL' : '= 0';
4351

4452
$query = $db->getQuery(true)
4553
->select('guest, client_id')
@@ -74,27 +82,28 @@ public static function getOnlineCount()
7482
}
7583

7684
/**
77-
* Show online member names
85+
* Fetch online user names
7886
*
79-
* @param mixed $params The parameters
87+
* @param CMSApplicationInterface $app The application instance
88+
* @param Registry $params The parameters
8089
*
8190
* @return array (array) $db->loadObjectList() The names of the online users.
8291
*
83-
* @since 1.5
92+
* @since __DEPLOY_VERSION__
8493
**/
85-
public static function getOnlineUserNames($params)
94+
public function fetchOnlineUserNames(CMSApplicationInterface $app, Registry $params): array
8695
{
87-
$whereCondition = Factory::getApplication()->get('shared_session', '0') ? 'IS NULL' : '= 0';
96+
$whereCondition = $app->get('shared_session', '0') ? 'IS NULL' : '= 0';
8897

89-
$db = Factory::getDbo();
98+
$db = $this->getDatabase();
9099
$query = $db->getQuery(true)
91100
->select($db->quoteName(['a.username', 'a.userid', 'a.client_id']))
92101
->from($db->quoteName('#__session', 'a'))
93102
->where($db->quoteName('a.userid') . ' != 0')
94103
->where($db->quoteName('a.client_id') . ' ' . $whereCondition)
95104
->group($db->quoteName(['a.username', 'a.userid', 'a.client_id']));
96105

97-
$user = Factory::getUser();
106+
$user = $app->getIdentity();
98107

99108
if (!$user->authorise('core.admin') && $params->get('filter_groups', 0) == 1) {
100109
$groups = $user->getAuthorisedGroups();
@@ -117,4 +126,48 @@ public static function getOnlineUserNames($params)
117126
return [];
118127
}
119128
}
129+
130+
/**
131+
* Show online count
132+
*
133+
* @return array The number of Users and Guests online.
134+
*
135+
* @since 1.5
136+
*
137+
* @deprecated __DEPLOY_VERSION__ will be removed in 7.0
138+
* Use the non-static method getOnlineUsersCount
139+
* Example: Factory::getApplication()->bootModule('mod_whosonline', 'site')
140+
* ->getHelper('WhosonlineHelper')
141+
* ->getOnlineUsersCount(Factory::getApplication())
142+
**/
143+
public static function getOnlineCount()
144+
{
145+
$app = Factory::getApplication();
146+
return $app->bootModule('mod_whosonline', 'site')
147+
->getHelper('WhosonlineHelper')
148+
->getOnlineUsersCount($app);
149+
}
150+
151+
/**
152+
* Show online member names
153+
*
154+
* @param mixed $params The parameters
155+
*
156+
* @return array (array) $db->loadObjectList() The names of the online users.
157+
*
158+
* @since 1.5
159+
*
160+
* @deprecated __DEPLOY_VERSION__ will be removed in 7.0
161+
* Use the non-static method fetchOnlineUserNames
162+
* Example: Factory::getApplication()->bootModule('mod_whosonline', 'site')
163+
* ->getHelper('WhosonlineHelper')
164+
* ->fetchOnlineUserNames(Factory::getApplication(), $params)
165+
**/
166+
public static function getOnlineUserNames($params)
167+
{
168+
$app = Factory::getApplication();
169+
return $app->bootModule('mod_whosonline', 'site')
170+
->getHelper('WhosonlineHelper')
171+
->fetchOnlineUserNames($app, $params);
172+
}
120173
}

0 commit comments

Comments
 (0)