xref: /webtrees/app/Module/LoggedInUsersModule.php (revision e5a6b4d4f6f6e7ff2fba7ae2cf27546ae68a79cc)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
48fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
58c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
68c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
78c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
88c2e8227SGreg Roach * (at your option) any later version.
98c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
108c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
118c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
128c2e8227SGreg Roach * GNU General Public License for more details.
138c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
148c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
158c2e8227SGreg Roach */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg Roach
1876692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
1976692c8bSGreg Roach
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\Individual;
23*e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Services\UserService;
24e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree;
25a45f9889SGreg Roachuse Symfony\Component\HttpFoundation\Request;
268c2e8227SGreg Roach
278c2e8227SGreg Roach/**
288c2e8227SGreg Roach * Class LoggedInUsersModule
298c2e8227SGreg Roach */
3037eb8894SGreg Roachclass LoggedInUsersModule extends AbstractModule implements ModuleBlockInterface
31c1010edaSGreg Roach{
3249a243cbSGreg Roach    use ModuleBlockTrait;
3349a243cbSGreg Roach
34961ec755SGreg Roach    /**
35*e5a6b4d4SGreg Roach     * @var UserService
36*e5a6b4d4SGreg Roach     */
37*e5a6b4d4SGreg Roach    private $user_service;
38*e5a6b4d4SGreg Roach
39*e5a6b4d4SGreg Roach    /**
40*e5a6b4d4SGreg Roach     * LoggedInUsersModule constructor.
41*e5a6b4d4SGreg Roach     *
42*e5a6b4d4SGreg Roach     * @param UserService $user_service
43*e5a6b4d4SGreg Roach     */
44*e5a6b4d4SGreg Roach    public function __construct(UserService $user_service) {
45*e5a6b4d4SGreg Roach        $this->user_service = $user_service;
46*e5a6b4d4SGreg Roach    }
47*e5a6b4d4SGreg Roach
48*e5a6b4d4SGreg Roach    /**
49961ec755SGreg Roach     * How should this module be labelled on tabs, menus, etc.?
50961ec755SGreg Roach     *
51961ec755SGreg Roach     * @return string
52961ec755SGreg Roach     */
5349a243cbSGreg Roach    public function title(): string
54c1010edaSGreg Roach    {
55bbb76c12SGreg Roach        /* I18N: Name of a module. (A list of users who are online now) */
56bbb76c12SGreg Roach        return I18N::translate('Who is online');
578c2e8227SGreg Roach    }
588c2e8227SGreg Roach
59961ec755SGreg Roach    /**
60961ec755SGreg Roach     * A sentence describing what this module does.
61961ec755SGreg Roach     *
62961ec755SGreg Roach     * @return string
63961ec755SGreg Roach     */
6449a243cbSGreg Roach    public function description(): string
65c1010edaSGreg Roach    {
66bbb76c12SGreg Roach        /* I18N: Description of the “Who is online” module */
67bbb76c12SGreg Roach        return I18N::translate('A list of users and visitors who are currently online.');
688c2e8227SGreg Roach    }
698c2e8227SGreg Roach
7076692c8bSGreg Roach    /**
7176692c8bSGreg Roach     * Generate the HTML content of this block.
7276692c8bSGreg Roach     *
73e490cd80SGreg Roach     * @param Tree     $tree
7476692c8bSGreg Roach     * @param int      $block_id
755f2ae573SGreg Roach     * @param string   $ctype
76727f238cSGreg Roach     * @param string[] $cfg
7776692c8bSGreg Roach     *
7876692c8bSGreg Roach     * @return string
7976692c8bSGreg Roach     */
805f2ae573SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string
81c1010edaSGreg Roach    {
828c2e8227SGreg Roach        $anonymous = 0;
8313abd6f3SGreg Roach        $logged_in = [];
848c2e8227SGreg Roach        $content   = '';
85*e5a6b4d4SGreg Roach        foreach ($this->user_service->allLoggedIn() as $user) {
868c2e8227SGreg Roach            if (Auth::isAdmin() || $user->getPreference('visibleonline')) {
878c2e8227SGreg Roach                $logged_in[] = $user;
888c2e8227SGreg Roach            } else {
898c2e8227SGreg Roach                $anonymous++;
908c2e8227SGreg Roach            }
918c2e8227SGreg Roach        }
928c2e8227SGreg Roach        $count_logged_in = count($logged_in);
938c2e8227SGreg Roach        $content .= '<div class="logged_in_count">';
948c2e8227SGreg Roach        if ($anonymous) {
95cdc90107SGreg Roach            $content .= I18N::plural('%s anonymous signed-in user', '%s anonymous signed-in users', $anonymous, I18N::number($anonymous));
968c2e8227SGreg Roach            if ($count_logged_in) {
978c2e8227SGreg Roach                $content .= '&nbsp;|&nbsp;';
988c2e8227SGreg Roach            }
998c2e8227SGreg Roach        }
1008c2e8227SGreg Roach        if ($count_logged_in) {
101cdc90107SGreg Roach            $content .= I18N::plural('%s signed-in user', '%s signed-in users', $count_logged_in, I18N::number($count_logged_in));
1028c2e8227SGreg Roach        }
1038c2e8227SGreg Roach        $content .= '</div>';
1048c2e8227SGreg Roach        $content .= '<div class="logged_in_list">';
1058c2e8227SGreg Roach        if (Auth::check()) {
1068c2e8227SGreg Roach            foreach ($logged_in as $user) {
107e490cd80SGreg Roach                $individual = Individual::getInstance($tree->getUserPreference($user, 'gedcomid'), $tree);
108e3fb86dfSGreg Roach
1098c2e8227SGreg Roach                $content .= '<div class="logged_in_name">';
110e3fb86dfSGreg Roach                if ($individual) {
111*e5a6b4d4SGreg Roach                    $content .= '<a href="' . e($individual->url()) . '">' . e($user->realName()) . '</a>';
112e3fb86dfSGreg Roach                } else {
113*e5a6b4d4SGreg Roach                    $content .= e($user->realName());
114e3fb86dfSGreg Roach                }
115*e5a6b4d4SGreg Roach                $content .= ' - ' . e($user->userName());
116895230eeSGreg Roach                if (Auth::id() !== $user->id() && $user->getPreference('contactmethod') !== 'none') {
117*e5a6b4d4SGreg Roach                    $content .= '<a href="' . e(route('message', ['to'  => $user->userName(), 'ged' => $tree->name()])) . '" class="btn btn-link" title="' . I18N::translate('Send a message') . '">' . view('icons/email') . '</a>';
1188c2e8227SGreg Roach                }
1198c2e8227SGreg Roach                $content .= '</div>';
1208c2e8227SGreg Roach            }
1218c2e8227SGreg Roach        }
1228c2e8227SGreg Roach        $content .= '</div>';
1238c2e8227SGreg Roach
1248c2e8227SGreg Roach        if ($anonymous === 0 && $count_logged_in === 0) {
1258c2e8227SGreg Roach            return '';
1268c2e8227SGreg Roach        }
1278c2e8227SGreg Roach
1286a8879feSGreg Roach        if ($ctype !== '') {
129147e99aaSGreg Roach            return view('modules/block-template', [
13026684e68SGreg Roach                'block'      => str_replace('_', '-', $this->name()),
1319c6524dcSGreg Roach                'id'         => $block_id,
1329c6524dcSGreg Roach                'config_url' => '',
13349a243cbSGreg Roach                'title'      => $this->title(),
1349c6524dcSGreg Roach                'content'    => $content,
1359c6524dcSGreg Roach            ]);
1368c2e8227SGreg Roach        }
137b2ce94c6SRico Sonntag
138b2ce94c6SRico Sonntag        return $content;
1398c2e8227SGreg Roach    }
1408c2e8227SGreg Roach
1418c2e8227SGreg Roach    /** {@inheritdoc} */
142c1010edaSGreg Roach    public function loadAjax(): bool
143c1010edaSGreg Roach    {
1448c2e8227SGreg Roach        return false;
1458c2e8227SGreg Roach    }
1468c2e8227SGreg Roach
1478c2e8227SGreg Roach    /** {@inheritdoc} */
148c1010edaSGreg Roach    public function isUserBlock(): bool
149c1010edaSGreg Roach    {
1508c2e8227SGreg Roach        return true;
1518c2e8227SGreg Roach    }
1528c2e8227SGreg Roach
1538c2e8227SGreg Roach    /** {@inheritdoc} */
15463276d8fSGreg Roach    public function isTreeBlock(): bool
155c1010edaSGreg Roach    {
1568c2e8227SGreg Roach        return true;
1578c2e8227SGreg Roach    }
1588c2e8227SGreg Roach
15976692c8bSGreg Roach    /**
160a45f9889SGreg Roach     * Update the configuration for a block.
161a45f9889SGreg Roach     *
162a45f9889SGreg Roach     * @param Request $request
163a45f9889SGreg Roach     * @param int     $block_id
164a45f9889SGreg Roach     *
165a45f9889SGreg Roach     * @return void
166a45f9889SGreg Roach     */
167a45f9889SGreg Roach    public function saveBlockConfiguration(Request $request, int $block_id)
168a45f9889SGreg Roach    {
169a45f9889SGreg Roach    }
170a45f9889SGreg Roach
171a45f9889SGreg Roach    /**
17276692c8bSGreg Roach     * An HTML form to edit block settings
17376692c8bSGreg Roach     *
174e490cd80SGreg Roach     * @param Tree $tree
17576692c8bSGreg Roach     * @param int  $block_id
176a9430be8SGreg Roach     *
177a9430be8SGreg Roach     * @return void
17876692c8bSGreg Roach     */
179a45f9889SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id)
180c1010edaSGreg Roach    {
1818c2e8227SGreg Roach    }
1828c2e8227SGreg Roach}
183