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