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