xref: /webtrees/app/Module/LoggedInUsersModule.php (revision 3976b4703df669696105ed6b024b96d433c8fbdb)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17declare(strict_types=1);
18
19namespace Fisharebest\Webtrees\Module;
20
21use Fisharebest\Webtrees\I18N;
22use Fisharebest\Webtrees\Statistics;
23use Fisharebest\Webtrees\Tree;
24use Illuminate\Support\Str;
25
26/**
27 * Class LoggedInUsersModule
28 */
29class LoggedInUsersModule extends AbstractModule implements ModuleBlockInterface
30{
31    use ModuleBlockTrait;
32
33    /**
34     * How should this module be identified in the control panel, etc.?
35     *
36     * @return string
37     */
38    public function title(): string
39    {
40        /* I18N: Name of a module. (A list of users who are online now) */
41        return I18N::translate('Who is online');
42    }
43
44    /**
45     * A sentence describing what this module does.
46     *
47     * @return string
48     */
49    public function description(): string
50    {
51        /* I18N: Description of the “Who is online” module */
52        return I18N::translate('A list of users and visitors who are currently online.');
53    }
54
55    /**
56     * Generate the HTML content of this block.
57     *
58     * @param Tree     $tree
59     * @param int      $block_id
60     * @param string   $context
61     * @param string[] $config
62     *
63     * @return string
64     */
65    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
66    {
67        /** @var Statistics $statistics */
68        $statistics = app(Statistics::class);
69
70        $content = '<div class="logged_in_count">' . $statistics->usersLoggedInList() . '</div>';
71
72        if ($context !== self::CONTEXT_EMBED) {
73            return view('modules/block-template', [
74                'block'      => Str::kebab($this->name()),
75                'id'         => $block_id,
76                'config_url' => '',
77                'title'      => $this->title(),
78                'content'    => $content,
79            ]);
80        }
81
82        return $content;
83    }
84
85    /**
86     * Should this block load asynchronously using AJAX?
87     *
88     * Simple blocks are faster in-line, more complex ones can be loaded later.
89     *
90     * @return bool
91     */
92    public function loadAjax(): bool
93    {
94        return false;
95    }
96
97    /**
98     * Can this block be shown on the user’s home page?
99     *
100     * @return bool
101     */
102    public function isUserBlock(): bool
103    {
104        return true;
105    }
106
107    /**
108     * Can this block be shown on the tree’s home page?
109     *
110     * @return bool
111     */
112    public function isTreeBlock(): bool
113    {
114        return true;
115    }
116}
117