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