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 348c2e8227SGreg Roach /** {@inheritdoc} */ 3549a243cbSGreg Roach public function title(): string 36c1010edaSGreg Roach { 37bbb76c12SGreg Roach /* I18N: Name of a module. (A list of users who are online now) */ 38bbb76c12SGreg Roach return I18N::translate('Who is online'); 398c2e8227SGreg Roach } 408c2e8227SGreg Roach 418c2e8227SGreg Roach /** {@inheritdoc} */ 4249a243cbSGreg Roach public function description(): string 43c1010edaSGreg Roach { 44bbb76c12SGreg Roach /* I18N: Description of the “Who is online” module */ 45bbb76c12SGreg Roach return I18N::translate('A list of users and visitors who are currently online.'); 468c2e8227SGreg Roach } 478c2e8227SGreg Roach 4876692c8bSGreg Roach /** 4976692c8bSGreg Roach * Generate the HTML content of this block. 5076692c8bSGreg Roach * 51e490cd80SGreg Roach * @param Tree $tree 5276692c8bSGreg Roach * @param int $block_id 535f2ae573SGreg Roach * @param string $ctype 54727f238cSGreg Roach * @param string[] $cfg 5576692c8bSGreg Roach * 5676692c8bSGreg Roach * @return string 5776692c8bSGreg Roach */ 585f2ae573SGreg Roach public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string 59c1010edaSGreg Roach { 608c2e8227SGreg Roach $anonymous = 0; 6113abd6f3SGreg Roach $logged_in = []; 628c2e8227SGreg Roach $content = ''; 638c2e8227SGreg Roach foreach (User::allLoggedIn() as $user) { 648c2e8227SGreg Roach if (Auth::isAdmin() || $user->getPreference('visibleonline')) { 658c2e8227SGreg Roach $logged_in[] = $user; 668c2e8227SGreg Roach } else { 678c2e8227SGreg Roach $anonymous++; 688c2e8227SGreg Roach } 698c2e8227SGreg Roach } 708c2e8227SGreg Roach $count_logged_in = count($logged_in); 718c2e8227SGreg Roach $content .= '<div class="logged_in_count">'; 728c2e8227SGreg Roach if ($anonymous) { 73cdc90107SGreg Roach $content .= I18N::plural('%s anonymous signed-in user', '%s anonymous signed-in users', $anonymous, I18N::number($anonymous)); 748c2e8227SGreg Roach if ($count_logged_in) { 758c2e8227SGreg Roach $content .= ' | '; 768c2e8227SGreg Roach } 778c2e8227SGreg Roach } 788c2e8227SGreg Roach if ($count_logged_in) { 79cdc90107SGreg Roach $content .= I18N::plural('%s signed-in user', '%s signed-in users', $count_logged_in, I18N::number($count_logged_in)); 808c2e8227SGreg Roach } 818c2e8227SGreg Roach $content .= '</div>'; 828c2e8227SGreg Roach $content .= '<div class="logged_in_list">'; 838c2e8227SGreg Roach if (Auth::check()) { 848c2e8227SGreg Roach foreach ($logged_in as $user) { 85e490cd80SGreg Roach $individual = Individual::getInstance($tree->getUserPreference($user, 'gedcomid'), $tree); 86e3fb86dfSGreg Roach 878c2e8227SGreg Roach $content .= '<div class="logged_in_name">'; 88e3fb86dfSGreg Roach if ($individual) { 89f1aa52cbSGreg Roach $content .= '<a href="' . e($individual->url()) . '">' . e($user->getRealName()) . '</a>'; 90e3fb86dfSGreg Roach } else { 91f1aa52cbSGreg Roach $content .= e($user->getRealName()); 92e3fb86dfSGreg Roach } 93d53324c9SGreg Roach $content .= ' - ' . e($user->getUserName()); 94*895230eeSGreg Roach if (Auth::id() !== $user->id() && $user->getPreference('contactmethod') !== 'none') { 95aa6f03bbSGreg 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>'; 968c2e8227SGreg Roach } 978c2e8227SGreg Roach $content .= '</div>'; 988c2e8227SGreg Roach } 998c2e8227SGreg Roach } 1008c2e8227SGreg Roach $content .= '</div>'; 1018c2e8227SGreg Roach 1028c2e8227SGreg Roach if ($anonymous === 0 && $count_logged_in === 0) { 1038c2e8227SGreg Roach return ''; 1048c2e8227SGreg Roach } 1058c2e8227SGreg Roach 1066a8879feSGreg Roach if ($ctype !== '') { 107147e99aaSGreg Roach return view('modules/block-template', [ 1089c6524dcSGreg Roach 'block' => str_replace('_', '-', $this->getName()), 1099c6524dcSGreg Roach 'id' => $block_id, 1109c6524dcSGreg Roach 'config_url' => '', 11149a243cbSGreg Roach 'title' => $this->title(), 1129c6524dcSGreg Roach 'content' => $content, 1139c6524dcSGreg Roach ]); 1148c2e8227SGreg Roach } 115b2ce94c6SRico Sonntag 116b2ce94c6SRico Sonntag return $content; 1178c2e8227SGreg Roach } 1188c2e8227SGreg Roach 1198c2e8227SGreg Roach /** {@inheritdoc} */ 120c1010edaSGreg Roach public function loadAjax(): bool 121c1010edaSGreg Roach { 1228c2e8227SGreg Roach return false; 1238c2e8227SGreg Roach } 1248c2e8227SGreg Roach 1258c2e8227SGreg Roach /** {@inheritdoc} */ 126c1010edaSGreg Roach public function isUserBlock(): bool 127c1010edaSGreg Roach { 1288c2e8227SGreg Roach return true; 1298c2e8227SGreg Roach } 1308c2e8227SGreg Roach 1318c2e8227SGreg Roach /** {@inheritdoc} */ 132c1010edaSGreg Roach public function isGedcomBlock(): bool 133c1010edaSGreg Roach { 1348c2e8227SGreg Roach return true; 1358c2e8227SGreg Roach } 1368c2e8227SGreg Roach 13776692c8bSGreg Roach /** 138a45f9889SGreg Roach * Update the configuration for a block. 139a45f9889SGreg Roach * 140a45f9889SGreg Roach * @param Request $request 141a45f9889SGreg Roach * @param int $block_id 142a45f9889SGreg Roach * 143a45f9889SGreg Roach * @return void 144a45f9889SGreg Roach */ 145a45f9889SGreg Roach public function saveBlockConfiguration(Request $request, int $block_id) 146a45f9889SGreg Roach { 147a45f9889SGreg Roach } 148a45f9889SGreg Roach 149a45f9889SGreg Roach /** 15076692c8bSGreg Roach * An HTML form to edit block settings 15176692c8bSGreg Roach * 152e490cd80SGreg Roach * @param Tree $tree 15376692c8bSGreg Roach * @param int $block_id 154a9430be8SGreg Roach * 155a9430be8SGreg Roach * @return void 15676692c8bSGreg Roach */ 157a45f9889SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id) 158c1010edaSGreg Roach { 1598c2e8227SGreg Roach } 1608c2e8227SGreg Roach} 161