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\User; 23 24/** 25 * Class LoggedInUsersModule 26 */ 27class LoggedInUsersModule extends AbstractModule implements ModuleBlockInterface { 28 /** {@inheritdoc} */ 29 public function getTitle() { 30 return /* I18N: Name of a module. (A list of users who are online now) */ I18N::translate('Who is online'); 31 } 32 33 /** {@inheritdoc} */ 34 public function getDescription() { 35 return /* I18N: Description of the “Who is online” module */ I18N::translate('A list of users and visitors who are currently online.'); 36 } 37 38 /** 39 * Generate the HTML content of this block. 40 * 41 * @param int $block_id 42 * @param bool $template 43 * @param string[] $cfg 44 * 45 * @return string 46 */ 47 public function getBlock($block_id, $template = true, $cfg = []): string { 48 global $WT_TREE; 49 50 $anonymous = 0; 51 $logged_in = []; 52 $content = ''; 53 foreach (User::allLoggedIn() as $user) { 54 if (Auth::isAdmin() || $user->getPreference('visibleonline')) { 55 $logged_in[] = $user; 56 } else { 57 $anonymous++; 58 } 59 } 60 $count_logged_in = count($logged_in); 61 $content .= '<div class="logged_in_count">'; 62 if ($anonymous) { 63 $content .= I18N::plural('%s anonymous signed-in user', '%s anonymous signed-in users', $anonymous, I18N::number($anonymous)); 64 if ($count_logged_in) { 65 $content .= ' | '; 66 } 67 } 68 if ($count_logged_in) { 69 $content .= I18N::plural('%s signed-in user', '%s signed-in users', $count_logged_in, I18N::number($count_logged_in)); 70 } 71 $content .= '</div>'; 72 $content .= '<div class="logged_in_list">'; 73 if (Auth::check()) { 74 foreach ($logged_in as $user) { 75 $individual = Individual::getInstance($WT_TREE->getUserPreference($user, 'gedcomid'), $WT_TREE); 76 77 $content .= '<div class="logged_in_name">'; 78 if ($individual) { 79 $content .= '<a href="' . e($individual->url()) . '">' . e($user->getRealName()) . '</a>'; 80 } else { 81 $content .= e($user->getRealName()); 82 } 83 $content .= ' - ' . e($user->getUserName()); 84 if (Auth::id() != $user->getUserId() && $user->getPreference('contactmethod') != 'none') { 85 $content .= FontAwesome::linkIcon('email', I18N::translate('Send a message'), ['class' => 'btn btn-link', 'href' => route('message', ['to' => $user->getUserName(), 'ged' => $WT_TREE->getNameUrl()])]); 86 } 87 $content .= '</div>'; 88 } 89 } 90 $content .= '</div>'; 91 92 if ($anonymous === 0 && $count_logged_in === 0) { 93 return ''; 94 } 95 96 if ($template) { 97 return view('blocks/template', [ 98 'block' => str_replace('_', '-', $this->getName()), 99 'id' => $block_id, 100 'config_url' => '', 101 'title' => $this->getTitle(), 102 'content' => $content, 103 ]); 104 } else { 105 return $content; 106 } 107 } 108 109 /** {@inheritdoc} */ 110 public function loadAjax(): bool { 111 return false; 112 } 113 114 /** {@inheritdoc} */ 115 public function isUserBlock(): bool { 116 return true; 117 } 118 119 /** {@inheritdoc} */ 120 public function isGedcomBlock(): bool { 121 return true; 122 } 123 124 /** 125 * An HTML form to edit block settings 126 * 127 * @param int $block_id 128 * 129 * @return void 130 */ 131 public function configureBlock($block_id) { 132 } 133} 134