1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2019 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 */ 16declare(strict_types=1); 17 18namespace Fisharebest\Webtrees\Module; 19 20use Fisharebest\Webtrees\Auth; 21use Fisharebest\Webtrees\I18N; 22use Fisharebest\Webtrees\Individual; 23use Fisharebest\Webtrees\Services\UserService; 24use Fisharebest\Webtrees\Tree; 25use Symfony\Component\HttpFoundation\Request; 26 27/** 28 * Class LoggedInUsersModule 29 */ 30class LoggedInUsersModule extends AbstractModule implements ModuleBlockInterface 31{ 32 use ModuleBlockTrait; 33 34 /** 35 * @var UserService 36 */ 37 private $user_service; 38 39 /** 40 * LoggedInUsersModule constructor. 41 * 42 * @param UserService $user_service 43 */ 44 public function __construct(UserService $user_service) { 45 $this->user_service = $user_service; 46 } 47 48 /** 49 * How should this module be labelled on tabs, menus, etc.? 50 * 51 * @return string 52 */ 53 public function title(): string 54 { 55 /* I18N: Name of a module. (A list of users who are online now) */ 56 return I18N::translate('Who is online'); 57 } 58 59 /** 60 * A sentence describing what this module does. 61 * 62 * @return string 63 */ 64 public function description(): string 65 { 66 /* I18N: Description of the “Who is online” module */ 67 return I18N::translate('A list of users and visitors who are currently online.'); 68 } 69 70 /** 71 * Generate the HTML content of this block. 72 * 73 * @param Tree $tree 74 * @param int $block_id 75 * @param string $ctype 76 * @param string[] $cfg 77 * 78 * @return string 79 */ 80 public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string 81 { 82 $anonymous = 0; 83 $logged_in = []; 84 $content = ''; 85 foreach ($this->user_service->allLoggedIn() as $user) { 86 if (Auth::isAdmin() || $user->getPreference('visibleonline')) { 87 $logged_in[] = $user; 88 } else { 89 $anonymous++; 90 } 91 } 92 $count_logged_in = count($logged_in); 93 $content .= '<div class="logged_in_count">'; 94 if ($anonymous) { 95 $content .= I18N::plural('%s anonymous signed-in user', '%s anonymous signed-in users', $anonymous, I18N::number($anonymous)); 96 if ($count_logged_in) { 97 $content .= ' | '; 98 } 99 } 100 if ($count_logged_in) { 101 $content .= I18N::plural('%s signed-in user', '%s signed-in users', $count_logged_in, I18N::number($count_logged_in)); 102 } 103 $content .= '</div>'; 104 $content .= '<div class="logged_in_list">'; 105 if (Auth::check()) { 106 foreach ($logged_in as $user) { 107 $individual = Individual::getInstance($tree->getUserPreference($user, 'gedcomid'), $tree); 108 109 $content .= '<div class="logged_in_name">'; 110 if ($individual) { 111 $content .= '<a href="' . e($individual->url()) . '">' . e($user->realName()) . '</a>'; 112 } else { 113 $content .= e($user->realName()); 114 } 115 $content .= ' - ' . e($user->userName()); 116 if (Auth::id() !== $user->id() && $user->getPreference('contactmethod') !== 'none') { 117 $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>'; 118 } 119 $content .= '</div>'; 120 } 121 } 122 $content .= '</div>'; 123 124 if ($anonymous === 0 && $count_logged_in === 0) { 125 return ''; 126 } 127 128 if ($ctype !== '') { 129 return view('modules/block-template', [ 130 'block' => str_replace('_', '-', $this->name()), 131 'id' => $block_id, 132 'config_url' => '', 133 'title' => $this->title(), 134 'content' => $content, 135 ]); 136 } 137 138 return $content; 139 } 140 141 /** {@inheritdoc} */ 142 public function loadAjax(): bool 143 { 144 return false; 145 } 146 147 /** {@inheritdoc} */ 148 public function isUserBlock(): bool 149 { 150 return true; 151 } 152 153 /** {@inheritdoc} */ 154 public function isTreeBlock(): bool 155 { 156 return true; 157 } 158 159 /** 160 * Update the configuration for a block. 161 * 162 * @param Request $request 163 * @param int $block_id 164 * 165 * @return void 166 */ 167 public function saveBlockConfiguration(Request $request, int $block_id) 168 { 169 } 170 171 /** 172 * An HTML form to edit block settings 173 * 174 * @param Tree $tree 175 * @param int $block_id 176 * 177 * @return void 178 */ 179 public function editBlockConfiguration(Tree $tree, int $block_id) 180 { 181 } 182} 183