xref: /webtrees/app/Module/LoggedInUsersModule.php (revision 895230eed7521b5cd885b90d4f5310405ff0b69a)
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\Auth;
21use Fisharebest\Webtrees\I18N;
22use Fisharebest\Webtrees\Individual;
23use Fisharebest\Webtrees\Tree;
24use Fisharebest\Webtrees\User;
25use Symfony\Component\HttpFoundation\Request;
26
27/**
28 * Class LoggedInUsersModule
29 */
30class LoggedInUsersModule extends AbstractModule implements ModuleInterface, ModuleBlockInterface
31{
32    use ModuleBlockTrait;
33
34    /** {@inheritdoc} */
35    public function title(): string
36    {
37        /* I18N: Name of a module. (A list of users who are online now) */
38        return I18N::translate('Who is online');
39    }
40
41    /** {@inheritdoc} */
42    public function description(): string
43    {
44        /* I18N: Description of the “Who is online” module */
45        return I18N::translate('A list of users and visitors who are currently online.');
46    }
47
48    /**
49     * Generate the HTML content of this block.
50     *
51     * @param Tree     $tree
52     * @param int      $block_id
53     * @param string   $ctype
54     * @param string[] $cfg
55     *
56     * @return string
57     */
58    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string
59    {
60        $anonymous = 0;
61        $logged_in = [];
62        $content   = '';
63        foreach (User::allLoggedIn() as $user) {
64            if (Auth::isAdmin() || $user->getPreference('visibleonline')) {
65                $logged_in[] = $user;
66            } else {
67                $anonymous++;
68            }
69        }
70        $count_logged_in = count($logged_in);
71        $content .= '<div class="logged_in_count">';
72        if ($anonymous) {
73            $content .= I18N::plural('%s anonymous signed-in user', '%s anonymous signed-in users', $anonymous, I18N::number($anonymous));
74            if ($count_logged_in) {
75                $content .= '&nbsp;|&nbsp;';
76            }
77        }
78        if ($count_logged_in) {
79            $content .= I18N::plural('%s signed-in user', '%s signed-in users', $count_logged_in, I18N::number($count_logged_in));
80        }
81        $content .= '</div>';
82        $content .= '<div class="logged_in_list">';
83        if (Auth::check()) {
84            foreach ($logged_in as $user) {
85                $individual = Individual::getInstance($tree->getUserPreference($user, 'gedcomid'), $tree);
86
87                $content .= '<div class="logged_in_name">';
88                if ($individual) {
89                    $content .= '<a href="' . e($individual->url()) . '">' . e($user->getRealName()) . '</a>';
90                } else {
91                    $content .= e($user->getRealName());
92                }
93                $content .= ' - ' . e($user->getUserName());
94                if (Auth::id() !== $user->id() && $user->getPreference('contactmethod') !== 'none') {
95                    $content .= '<a href="' . e(route('message', ['to'  => $user->getUserName(), 'ged' => $tree->name()])) . '" class="btn btn-link" title="' . I18N::translate('Send a message') . '">' . view('icons/email') . '</a>';
96                }
97                $content .= '</div>';
98            }
99        }
100        $content .= '</div>';
101
102        if ($anonymous === 0 && $count_logged_in === 0) {
103            return '';
104        }
105
106        if ($ctype !== '') {
107            return view('modules/block-template', [
108                'block'      => str_replace('_', '-', $this->getName()),
109                'id'         => $block_id,
110                'config_url' => '',
111                'title'      => $this->title(),
112                'content'    => $content,
113            ]);
114        }
115
116        return $content;
117    }
118
119    /** {@inheritdoc} */
120    public function loadAjax(): bool
121    {
122        return false;
123    }
124
125    /** {@inheritdoc} */
126    public function isUserBlock(): bool
127    {
128        return true;
129    }
130
131    /** {@inheritdoc} */
132    public function isGedcomBlock(): bool
133    {
134        return true;
135    }
136
137    /**
138     * Update the configuration for a block.
139     *
140     * @param Request $request
141     * @param int     $block_id
142     *
143     * @return void
144     */
145    public function saveBlockConfiguration(Request $request, int $block_id)
146    {
147    }
148
149    /**
150     * An HTML form to edit block settings
151     *
152     * @param Tree $tree
153     * @param int  $block_id
154     *
155     * @return void
156     */
157    public function editBlockConfiguration(Tree $tree, int $block_id)
158    {
159    }
160}
161