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