xref: /webtrees/app/Module/LoggedInUsersModule.php (revision 873953697c930fadbf3243d2b8c0029fd684da0e)
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\I18N;
20use Fisharebest\Webtrees\Individual;
21use Fisharebest\Webtrees\Tree;
22use Fisharebest\Webtrees\User;
23use Symfony\Component\HttpFoundation\Request;
24
25/**
26 * Class LoggedInUsersModule
27 */
28class LoggedInUsersModule extends AbstractModule implements ModuleBlockInterface
29{
30    /** {@inheritdoc} */
31    public function getTitle(): string
32    {
33        /* I18N: Name of a module. (A list of users who are online now) */
34        return I18N::translate('Who is online');
35    }
36
37    /** {@inheritdoc} */
38    public function getDescription(): string
39    {
40        /* I18N: Description of the “Who is online” module */
41        return 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 .= '<a href="' . e(route('message', ['to'  => $user->getUserName(), 'ged' => $tree->getName()])) . '" class="btn btn-link" title="' . I18N::translate('Send a message') . '">' . view('icons/email') . '</a>';
92                }
93                $content .= '</div>';
94            }
95        }
96        $content .= '</div>';
97
98        if ($anonymous === 0 && $count_logged_in === 0) {
99            return '';
100        }
101
102        if ($template) {
103            return view('modules/block-template', [
104                'block'      => str_replace('_', '-', $this->getName()),
105                'id'         => $block_id,
106                'config_url' => '',
107                'title'      => $this->getTitle(),
108                'content'    => $content,
109            ]);
110        } else {
111            return $content;
112        }
113    }
114
115    /** {@inheritdoc} */
116    public function loadAjax(): bool
117    {
118        return false;
119    }
120
121    /** {@inheritdoc} */
122    public function isUserBlock(): bool
123    {
124        return true;
125    }
126
127    /** {@inheritdoc} */
128    public function isGedcomBlock(): bool
129    {
130        return true;
131    }
132
133    /**
134     * Update the configuration for a block.
135     *
136     * @param Request $request
137     * @param int     $block_id
138     *
139     * @return void
140     */
141    public function saveBlockConfiguration(Request $request, int $block_id)
142    {
143    }
144
145    /**
146     * An HTML form to edit block settings
147     *
148     * @param Tree $tree
149     * @param int  $block_id
150     *
151     * @return void
152     */
153    public function editBlockConfiguration(Tree $tree, int $block_id)
154    {
155    }
156}
157