xref: /webtrees/app/Module/LoggedInUsersModule.php (revision e2cbf57a1b80fdeccb7ccc9b79b7aa02707dbde2)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
48fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
58c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
68c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
78c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
88c2e8227SGreg Roach * (at your option) any later version.
98c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
108c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
118c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
128c2e8227SGreg Roach * GNU General Public License for more details.
138c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
148c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
158c2e8227SGreg Roach */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg Roach
1876692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
1976692c8bSGreg Roach
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\Individual;
23e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Services\UserService;
24e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree;
25a45f9889SGreg Roachuse Symfony\Component\HttpFoundation\Request;
268c2e8227SGreg Roach
278c2e8227SGreg Roach/**
288c2e8227SGreg Roach * Class LoggedInUsersModule
298c2e8227SGreg Roach */
3037eb8894SGreg Roachclass LoggedInUsersModule extends AbstractModule implements ModuleBlockInterface
31c1010edaSGreg Roach{
3249a243cbSGreg Roach    use ModuleBlockTrait;
3349a243cbSGreg Roach
34961ec755SGreg Roach    /**
35e5a6b4d4SGreg Roach     * @var UserService
36e5a6b4d4SGreg Roach     */
37e5a6b4d4SGreg Roach    private $user_service;
38e5a6b4d4SGreg Roach
39e5a6b4d4SGreg Roach    /**
40e5a6b4d4SGreg Roach     * LoggedInUsersModule constructor.
41e5a6b4d4SGreg Roach     *
42e5a6b4d4SGreg Roach     * @param UserService $user_service
43e5a6b4d4SGreg Roach     */
44*e2cbf57aSGreg Roach    public function __construct(UserService $user_service)
45*e2cbf57aSGreg Roach    {
46e5a6b4d4SGreg Roach        $this->user_service = $user_service;
47e5a6b4d4SGreg Roach    }
48e5a6b4d4SGreg Roach
49e5a6b4d4SGreg Roach    /**
50961ec755SGreg Roach     * How should this module be labelled on tabs, menus, etc.?
51961ec755SGreg Roach     *
52961ec755SGreg Roach     * @return string
53961ec755SGreg Roach     */
5449a243cbSGreg Roach    public function title(): string
55c1010edaSGreg Roach    {
56bbb76c12SGreg Roach        /* I18N: Name of a module. (A list of users who are online now) */
57bbb76c12SGreg Roach        return I18N::translate('Who is online');
588c2e8227SGreg Roach    }
598c2e8227SGreg Roach
60961ec755SGreg Roach    /**
61961ec755SGreg Roach     * A sentence describing what this module does.
62961ec755SGreg Roach     *
63961ec755SGreg Roach     * @return string
64961ec755SGreg Roach     */
6549a243cbSGreg Roach    public function description(): string
66c1010edaSGreg Roach    {
67bbb76c12SGreg Roach        /* I18N: Description of the “Who is online” module */
68bbb76c12SGreg Roach        return I18N::translate('A list of users and visitors who are currently online.');
698c2e8227SGreg Roach    }
708c2e8227SGreg Roach
7176692c8bSGreg Roach    /**
7276692c8bSGreg Roach     * Generate the HTML content of this block.
7376692c8bSGreg Roach     *
74e490cd80SGreg Roach     * @param Tree     $tree
7576692c8bSGreg Roach     * @param int      $block_id
765f2ae573SGreg Roach     * @param string   $ctype
77727f238cSGreg Roach     * @param string[] $cfg
7876692c8bSGreg Roach     *
7976692c8bSGreg Roach     * @return string
8076692c8bSGreg Roach     */
815f2ae573SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string
82c1010edaSGreg Roach    {
838c2e8227SGreg Roach        $anonymous = 0;
8413abd6f3SGreg Roach        $logged_in = [];
858c2e8227SGreg Roach        $content   = '';
86e5a6b4d4SGreg Roach        foreach ($this->user_service->allLoggedIn() as $user) {
878c2e8227SGreg Roach            if (Auth::isAdmin() || $user->getPreference('visibleonline')) {
888c2e8227SGreg Roach                $logged_in[] = $user;
898c2e8227SGreg Roach            } else {
908c2e8227SGreg Roach                $anonymous++;
918c2e8227SGreg Roach            }
928c2e8227SGreg Roach        }
938c2e8227SGreg Roach        $count_logged_in = count($logged_in);
948c2e8227SGreg Roach        $content .= '<div class="logged_in_count">';
958c2e8227SGreg Roach        if ($anonymous) {
96cdc90107SGreg Roach            $content .= I18N::plural('%s anonymous signed-in user', '%s anonymous signed-in users', $anonymous, I18N::number($anonymous));
978c2e8227SGreg Roach            if ($count_logged_in) {
988c2e8227SGreg Roach                $content .= '&nbsp;|&nbsp;';
998c2e8227SGreg Roach            }
1008c2e8227SGreg Roach        }
1018c2e8227SGreg Roach        if ($count_logged_in) {
102cdc90107SGreg Roach            $content .= I18N::plural('%s signed-in user', '%s signed-in users', $count_logged_in, I18N::number($count_logged_in));
1038c2e8227SGreg Roach        }
1048c2e8227SGreg Roach        $content .= '</div>';
1058c2e8227SGreg Roach        $content .= '<div class="logged_in_list">';
1068c2e8227SGreg Roach        if (Auth::check()) {
1078c2e8227SGreg Roach            foreach ($logged_in as $user) {
108e490cd80SGreg Roach                $individual = Individual::getInstance($tree->getUserPreference($user, 'gedcomid'), $tree);
109e3fb86dfSGreg Roach
1108c2e8227SGreg Roach                $content .= '<div class="logged_in_name">';
111e3fb86dfSGreg Roach                if ($individual) {
112e5a6b4d4SGreg Roach                    $content .= '<a href="' . e($individual->url()) . '">' . e($user->realName()) . '</a>';
113e3fb86dfSGreg Roach                } else {
114e5a6b4d4SGreg Roach                    $content .= e($user->realName());
115e3fb86dfSGreg Roach                }
116e5a6b4d4SGreg Roach                $content .= ' - ' . e($user->userName());
117895230eeSGreg Roach                if (Auth::id() !== $user->id() && $user->getPreference('contactmethod') !== 'none') {
118e5a6b4d4SGreg Roach                    $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>';
1198c2e8227SGreg Roach                }
1208c2e8227SGreg Roach                $content .= '</div>';
1218c2e8227SGreg Roach            }
1228c2e8227SGreg Roach        }
1238c2e8227SGreg Roach        $content .= '</div>';
1248c2e8227SGreg Roach
1258c2e8227SGreg Roach        if ($anonymous === 0 && $count_logged_in === 0) {
1268c2e8227SGreg Roach            return '';
1278c2e8227SGreg Roach        }
1288c2e8227SGreg Roach
1296a8879feSGreg Roach        if ($ctype !== '') {
130147e99aaSGreg Roach            return view('modules/block-template', [
13126684e68SGreg Roach                'block'      => str_replace('_', '-', $this->name()),
1329c6524dcSGreg Roach                'id'         => $block_id,
1339c6524dcSGreg Roach                'config_url' => '',
13449a243cbSGreg Roach                'title'      => $this->title(),
1359c6524dcSGreg Roach                'content'    => $content,
1369c6524dcSGreg Roach            ]);
1378c2e8227SGreg Roach        }
138b2ce94c6SRico Sonntag
139b2ce94c6SRico Sonntag        return $content;
1408c2e8227SGreg Roach    }
1418c2e8227SGreg Roach
1428c2e8227SGreg Roach    /** {@inheritdoc} */
143c1010edaSGreg Roach    public function loadAjax(): bool
144c1010edaSGreg Roach    {
1458c2e8227SGreg Roach        return false;
1468c2e8227SGreg Roach    }
1478c2e8227SGreg Roach
1488c2e8227SGreg Roach    /** {@inheritdoc} */
149c1010edaSGreg Roach    public function isUserBlock(): bool
150c1010edaSGreg Roach    {
1518c2e8227SGreg Roach        return true;
1528c2e8227SGreg Roach    }
1538c2e8227SGreg Roach
1548c2e8227SGreg Roach    /** {@inheritdoc} */
15563276d8fSGreg Roach    public function isTreeBlock(): bool
156c1010edaSGreg Roach    {
1578c2e8227SGreg Roach        return true;
1588c2e8227SGreg Roach    }
1598c2e8227SGreg Roach
16076692c8bSGreg Roach    /**
161a45f9889SGreg Roach     * Update the configuration for a block.
162a45f9889SGreg Roach     *
163a45f9889SGreg Roach     * @param Request $request
164a45f9889SGreg Roach     * @param int     $block_id
165a45f9889SGreg Roach     *
166a45f9889SGreg Roach     * @return void
167a45f9889SGreg Roach     */
168a45f9889SGreg Roach    public function saveBlockConfiguration(Request $request, int $block_id)
169a45f9889SGreg Roach    {
170a45f9889SGreg Roach    }
171a45f9889SGreg Roach
172a45f9889SGreg Roach    /**
17376692c8bSGreg Roach     * An HTML form to edit block settings
17476692c8bSGreg Roach     *
175e490cd80SGreg Roach     * @param Tree $tree
17676692c8bSGreg Roach     * @param int  $block_id
177a9430be8SGreg Roach     *
178a9430be8SGreg Roach     * @return void
17976692c8bSGreg Roach     */
180a45f9889SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id)
181c1010edaSGreg Roach    {
1828c2e8227SGreg Roach    }
1838c2e8227SGreg Roach}
184