xref: /webtrees/app/Module/LoggedInUsersModule.php (revision 26684e686fb5ab50ecb57e7e6c6a0a55852d2203)
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;
23e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\User;
25a45f9889SGreg Roachuse Symfony\Component\HttpFoundation\Request;
268c2e8227SGreg Roach
278c2e8227SGreg Roach/**
288c2e8227SGreg Roach * Class LoggedInUsersModule
298c2e8227SGreg Roach */
3049a243cbSGreg Roachclass LoggedInUsersModule extends AbstractModule implements ModuleInterface, ModuleBlockInterface
31c1010edaSGreg Roach{
3249a243cbSGreg Roach    use ModuleBlockTrait;
3349a243cbSGreg Roach
34961ec755SGreg Roach    /**
35961ec755SGreg Roach     * How should this module be labelled on tabs, menus, etc.?
36961ec755SGreg Roach     *
37961ec755SGreg Roach     * @return string
38961ec755SGreg Roach     */
3949a243cbSGreg Roach    public function title(): string
40c1010edaSGreg Roach    {
41bbb76c12SGreg Roach        /* I18N: Name of a module. (A list of users who are online now) */
42bbb76c12SGreg Roach        return I18N::translate('Who is online');
438c2e8227SGreg Roach    }
448c2e8227SGreg Roach
45961ec755SGreg Roach    /**
46961ec755SGreg Roach     * A sentence describing what this module does.
47961ec755SGreg Roach     *
48961ec755SGreg Roach     * @return string
49961ec755SGreg Roach     */
5049a243cbSGreg Roach    public function description(): string
51c1010edaSGreg Roach    {
52bbb76c12SGreg Roach        /* I18N: Description of the “Who is online” module */
53bbb76c12SGreg Roach        return I18N::translate('A list of users and visitors who are currently online.');
548c2e8227SGreg Roach    }
558c2e8227SGreg Roach
5676692c8bSGreg Roach    /**
5776692c8bSGreg Roach     * Generate the HTML content of this block.
5876692c8bSGreg Roach     *
59e490cd80SGreg Roach     * @param Tree     $tree
6076692c8bSGreg Roach     * @param int      $block_id
615f2ae573SGreg Roach     * @param string   $ctype
62727f238cSGreg Roach     * @param string[] $cfg
6376692c8bSGreg Roach     *
6476692c8bSGreg Roach     * @return string
6576692c8bSGreg Roach     */
665f2ae573SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string
67c1010edaSGreg Roach    {
688c2e8227SGreg Roach        $anonymous = 0;
6913abd6f3SGreg Roach        $logged_in = [];
708c2e8227SGreg Roach        $content   = '';
718c2e8227SGreg Roach        foreach (User::allLoggedIn() as $user) {
728c2e8227SGreg Roach            if (Auth::isAdmin() || $user->getPreference('visibleonline')) {
738c2e8227SGreg Roach                $logged_in[] = $user;
748c2e8227SGreg Roach            } else {
758c2e8227SGreg Roach                $anonymous++;
768c2e8227SGreg Roach            }
778c2e8227SGreg Roach        }
788c2e8227SGreg Roach        $count_logged_in = count($logged_in);
798c2e8227SGreg Roach        $content .= '<div class="logged_in_count">';
808c2e8227SGreg Roach        if ($anonymous) {
81cdc90107SGreg Roach            $content .= I18N::plural('%s anonymous signed-in user', '%s anonymous signed-in users', $anonymous, I18N::number($anonymous));
828c2e8227SGreg Roach            if ($count_logged_in) {
838c2e8227SGreg Roach                $content .= '&nbsp;|&nbsp;';
848c2e8227SGreg Roach            }
858c2e8227SGreg Roach        }
868c2e8227SGreg Roach        if ($count_logged_in) {
87cdc90107SGreg Roach            $content .= I18N::plural('%s signed-in user', '%s signed-in users', $count_logged_in, I18N::number($count_logged_in));
888c2e8227SGreg Roach        }
898c2e8227SGreg Roach        $content .= '</div>';
908c2e8227SGreg Roach        $content .= '<div class="logged_in_list">';
918c2e8227SGreg Roach        if (Auth::check()) {
928c2e8227SGreg Roach            foreach ($logged_in as $user) {
93e490cd80SGreg Roach                $individual = Individual::getInstance($tree->getUserPreference($user, 'gedcomid'), $tree);
94e3fb86dfSGreg Roach
958c2e8227SGreg Roach                $content .= '<div class="logged_in_name">';
96e3fb86dfSGreg Roach                if ($individual) {
97f1aa52cbSGreg Roach                    $content .= '<a href="' . e($individual->url()) . '">' . e($user->getRealName()) . '</a>';
98e3fb86dfSGreg Roach                } else {
99f1aa52cbSGreg Roach                    $content .= e($user->getRealName());
100e3fb86dfSGreg Roach                }
101d53324c9SGreg Roach                $content .= ' - ' . e($user->getUserName());
102895230eeSGreg Roach                if (Auth::id() !== $user->id() && $user->getPreference('contactmethod') !== 'none') {
103aa6f03bbSGreg Roach                    $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>';
1048c2e8227SGreg Roach                }
1058c2e8227SGreg Roach                $content .= '</div>';
1068c2e8227SGreg Roach            }
1078c2e8227SGreg Roach        }
1088c2e8227SGreg Roach        $content .= '</div>';
1098c2e8227SGreg Roach
1108c2e8227SGreg Roach        if ($anonymous === 0 && $count_logged_in === 0) {
1118c2e8227SGreg Roach            return '';
1128c2e8227SGreg Roach        }
1138c2e8227SGreg Roach
1146a8879feSGreg Roach        if ($ctype !== '') {
115147e99aaSGreg Roach            return view('modules/block-template', [
116*26684e68SGreg Roach                'block'      => str_replace('_', '-', $this->name()),
1179c6524dcSGreg Roach                'id'         => $block_id,
1189c6524dcSGreg Roach                'config_url' => '',
11949a243cbSGreg Roach                'title'      => $this->title(),
1209c6524dcSGreg Roach                'content'    => $content,
1219c6524dcSGreg Roach            ]);
1228c2e8227SGreg Roach        }
123b2ce94c6SRico Sonntag
124b2ce94c6SRico Sonntag        return $content;
1258c2e8227SGreg Roach    }
1268c2e8227SGreg Roach
1278c2e8227SGreg Roach    /** {@inheritdoc} */
128c1010edaSGreg Roach    public function loadAjax(): bool
129c1010edaSGreg Roach    {
1308c2e8227SGreg Roach        return false;
1318c2e8227SGreg Roach    }
1328c2e8227SGreg Roach
1338c2e8227SGreg Roach    /** {@inheritdoc} */
134c1010edaSGreg Roach    public function isUserBlock(): bool
135c1010edaSGreg Roach    {
1368c2e8227SGreg Roach        return true;
1378c2e8227SGreg Roach    }
1388c2e8227SGreg Roach
1398c2e8227SGreg Roach    /** {@inheritdoc} */
140c1010edaSGreg Roach    public function isGedcomBlock(): bool
141c1010edaSGreg Roach    {
1428c2e8227SGreg Roach        return true;
1438c2e8227SGreg Roach    }
1448c2e8227SGreg Roach
14576692c8bSGreg Roach    /**
146a45f9889SGreg Roach     * Update the configuration for a block.
147a45f9889SGreg Roach     *
148a45f9889SGreg Roach     * @param Request $request
149a45f9889SGreg Roach     * @param int     $block_id
150a45f9889SGreg Roach     *
151a45f9889SGreg Roach     * @return void
152a45f9889SGreg Roach     */
153a45f9889SGreg Roach    public function saveBlockConfiguration(Request $request, int $block_id)
154a45f9889SGreg Roach    {
155a45f9889SGreg Roach    }
156a45f9889SGreg Roach
157a45f9889SGreg Roach    /**
15876692c8bSGreg Roach     * An HTML form to edit block settings
15976692c8bSGreg Roach     *
160e490cd80SGreg Roach     * @param Tree $tree
16176692c8bSGreg Roach     * @param int  $block_id
162a9430be8SGreg Roach     *
163a9430be8SGreg Roach     * @return void
16476692c8bSGreg Roach     */
165a45f9889SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id)
166c1010edaSGreg Roach    {
1678c2e8227SGreg Roach    }
1688c2e8227SGreg Roach}
169