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