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