18c2e8227SGreg Roach<?php 28c2e8227SGreg Roach/** 38c2e8227SGreg Roach * webtrees: online genealogy 41062a142SGreg Roach * Copyright (C) 2018 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 */ 1676692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module; 1776692c8bSGreg Roach 180e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth; 1915d603e7SGreg Roachuse Fisharebest\Webtrees\FontAwesome; 200e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 210e62c4b8SGreg Roachuse Fisharebest\Webtrees\Individual; 22e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree; 230e62c4b8SGreg Roachuse Fisharebest\Webtrees\User; 24*a45f9889SGreg Roachuse Symfony\Component\HttpFoundation\Request; 258c2e8227SGreg Roach 268c2e8227SGreg Roach/** 278c2e8227SGreg Roach * Class LoggedInUsersModule 288c2e8227SGreg Roach */ 29c1010edaSGreg Roachclass LoggedInUsersModule extends AbstractModule implements ModuleBlockInterface 30c1010edaSGreg Roach{ 318c2e8227SGreg Roach /** {@inheritdoc} */ 32c1010edaSGreg Roach public function getTitle() 33c1010edaSGreg Roach { 34bbb76c12SGreg Roach /* I18N: Name of a module. (A list of users who are online now) */ 35bbb76c12SGreg Roach return I18N::translate('Who is online'); 368c2e8227SGreg Roach } 378c2e8227SGreg Roach 388c2e8227SGreg Roach /** {@inheritdoc} */ 39c1010edaSGreg Roach public function getDescription() 40c1010edaSGreg Roach { 41bbb76c12SGreg Roach /* I18N: Description of the “Who is online” module */ 42bbb76c12SGreg Roach return I18N::translate('A list of users and visitors who are currently online.'); 438c2e8227SGreg Roach } 448c2e8227SGreg Roach 4576692c8bSGreg Roach /** 4676692c8bSGreg Roach * Generate the HTML content of this block. 4776692c8bSGreg Roach * 48e490cd80SGreg Roach * @param Tree $tree 4976692c8bSGreg Roach * @param int $block_id 5076692c8bSGreg Roach * @param bool $template 51727f238cSGreg Roach * @param string[] $cfg 5276692c8bSGreg Roach * 5376692c8bSGreg Roach * @return string 5476692c8bSGreg Roach */ 55c1010edaSGreg Roach public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string 56c1010edaSGreg Roach { 578c2e8227SGreg Roach $anonymous = 0; 5813abd6f3SGreg Roach $logged_in = []; 598c2e8227SGreg Roach $content = ''; 608c2e8227SGreg Roach foreach (User::allLoggedIn() as $user) { 618c2e8227SGreg Roach if (Auth::isAdmin() || $user->getPreference('visibleonline')) { 628c2e8227SGreg Roach $logged_in[] = $user; 638c2e8227SGreg Roach } else { 648c2e8227SGreg Roach $anonymous++; 658c2e8227SGreg Roach } 668c2e8227SGreg Roach } 678c2e8227SGreg Roach $count_logged_in = count($logged_in); 688c2e8227SGreg Roach $content .= '<div class="logged_in_count">'; 698c2e8227SGreg Roach if ($anonymous) { 70cdc90107SGreg Roach $content .= I18N::plural('%s anonymous signed-in user', '%s anonymous signed-in users', $anonymous, I18N::number($anonymous)); 718c2e8227SGreg Roach if ($count_logged_in) { 728c2e8227SGreg Roach $content .= ' | '; 738c2e8227SGreg Roach } 748c2e8227SGreg Roach } 758c2e8227SGreg Roach if ($count_logged_in) { 76cdc90107SGreg Roach $content .= I18N::plural('%s signed-in user', '%s signed-in users', $count_logged_in, I18N::number($count_logged_in)); 778c2e8227SGreg Roach } 788c2e8227SGreg Roach $content .= '</div>'; 798c2e8227SGreg Roach $content .= '<div class="logged_in_list">'; 808c2e8227SGreg Roach if (Auth::check()) { 818c2e8227SGreg Roach foreach ($logged_in as $user) { 82e490cd80SGreg Roach $individual = Individual::getInstance($tree->getUserPreference($user, 'gedcomid'), $tree); 83e3fb86dfSGreg Roach 848c2e8227SGreg Roach $content .= '<div class="logged_in_name">'; 85e3fb86dfSGreg Roach if ($individual) { 86f1aa52cbSGreg Roach $content .= '<a href="' . e($individual->url()) . '">' . e($user->getRealName()) . '</a>'; 87e3fb86dfSGreg Roach } else { 88f1aa52cbSGreg Roach $content .= e($user->getRealName()); 89e3fb86dfSGreg Roach } 90d53324c9SGreg Roach $content .= ' - ' . e($user->getUserName()); 91210dfef2SGreg Roach if (true || Auth::id() !== $user->getUserId() && $user->getPreference('contactmethod') !== 'none') { 92210dfef2SGreg Roach $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>'; 938c2e8227SGreg Roach } 948c2e8227SGreg Roach $content .= '</div>'; 958c2e8227SGreg Roach } 968c2e8227SGreg Roach } 978c2e8227SGreg Roach $content .= '</div>'; 988c2e8227SGreg Roach 998c2e8227SGreg Roach if ($anonymous === 0 && $count_logged_in === 0) { 1008c2e8227SGreg Roach return ''; 1018c2e8227SGreg Roach } 1028c2e8227SGreg Roach 1038c2e8227SGreg Roach if ($template) { 104147e99aaSGreg Roach return view('modules/block-template', [ 1059c6524dcSGreg Roach 'block' => str_replace('_', '-', $this->getName()), 1069c6524dcSGreg Roach 'id' => $block_id, 1079c6524dcSGreg Roach 'config_url' => '', 1089c6524dcSGreg Roach 'title' => $this->getTitle(), 1099c6524dcSGreg Roach 'content' => $content, 1109c6524dcSGreg Roach ]); 1118c2e8227SGreg Roach } else { 1128c2e8227SGreg Roach return $content; 1138c2e8227SGreg Roach } 1148c2e8227SGreg Roach } 1158c2e8227SGreg Roach 1168c2e8227SGreg Roach /** {@inheritdoc} */ 117c1010edaSGreg Roach public function loadAjax(): bool 118c1010edaSGreg Roach { 1198c2e8227SGreg Roach return false; 1208c2e8227SGreg Roach } 1218c2e8227SGreg Roach 1228c2e8227SGreg Roach /** {@inheritdoc} */ 123c1010edaSGreg Roach public function isUserBlock(): bool 124c1010edaSGreg Roach { 1258c2e8227SGreg Roach return true; 1268c2e8227SGreg Roach } 1278c2e8227SGreg Roach 1288c2e8227SGreg Roach /** {@inheritdoc} */ 129c1010edaSGreg Roach public function isGedcomBlock(): bool 130c1010edaSGreg Roach { 1318c2e8227SGreg Roach return true; 1328c2e8227SGreg Roach } 1338c2e8227SGreg Roach 13476692c8bSGreg Roach /** 135*a45f9889SGreg Roach * Update the configuration for a block. 136*a45f9889SGreg Roach * 137*a45f9889SGreg Roach * @param Request $request 138*a45f9889SGreg Roach * @param int $block_id 139*a45f9889SGreg Roach * 140*a45f9889SGreg Roach * @return void 141*a45f9889SGreg Roach */ 142*a45f9889SGreg Roach public function saveBlockConfiguration(Request $request, int $block_id) 143*a45f9889SGreg Roach { 144*a45f9889SGreg Roach } 145*a45f9889SGreg Roach 146*a45f9889SGreg Roach /** 14776692c8bSGreg Roach * An HTML form to edit block settings 14876692c8bSGreg Roach * 149e490cd80SGreg Roach * @param Tree $tree 15076692c8bSGreg Roach * @param int $block_id 151a9430be8SGreg Roach * 152a9430be8SGreg Roach * @return void 15376692c8bSGreg Roach */ 154*a45f9889SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id) 155c1010edaSGreg Roach { 1568c2e8227SGreg Roach } 1578c2e8227SGreg Roach} 158