1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2019 webtrees development team 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Statistics\Repository; 21 22use Fisharebest\Webtrees\Auth; 23use Fisharebest\Webtrees\Contracts\UserInterface; 24use Fisharebest\Webtrees\Http\RequestHandlers\MessagePage; 25use Fisharebest\Webtrees\I18N; 26use Fisharebest\Webtrees\Individual; 27use Fisharebest\Webtrees\Services\UserService; 28use Fisharebest\Webtrees\Statistics\Repository\Interfaces\UserRepositoryInterface; 29use Fisharebest\Webtrees\Tree; 30 31use function count; 32 33/** 34 * A repository providing methods for user related statistics. 35 */ 36class UserRepository implements UserRepositoryInterface 37{ 38 /** 39 * @var Tree 40 */ 41 private $tree; 42 /** 43 * @var UserService 44 */ 45 private $user_service; 46 47 /** 48 * Constructor. 49 * 50 * @param Tree $tree 51 * @param UserService $user_service 52 */ 53 public function __construct(Tree $tree, UserService $user_service) 54 { 55 $this->tree = $tree; 56 $this->user_service = $user_service; 57 } 58 59 /** 60 * Who is currently logged in? 61 * 62 * @param string $type "list" or "nolist" 63 * 64 * @return string 65 */ 66 private function usersLoggedInQuery($type = 'nolist'): string 67 { 68 $content = ''; 69 $anonymous = 0; 70 $logged_in = []; 71 72 foreach ($this->user_service->allLoggedIn() as $user) { 73 if (Auth::isAdmin() || $user->getPreference('visibleonline')) { 74 $logged_in[] = $user; 75 } else { 76 $anonymous++; 77 } 78 } 79 80 $count_logged_in = count($logged_in); 81 82 if ($count_logged_in === 0 && $anonymous === 0) { 83 $content .= I18N::translate('No signed-in and no anonymous users'); 84 } 85 86 if ($anonymous > 0) { 87 $content .= '<b>' . I18N::plural('%s anonymous signed-in user', '%s anonymous signed-in users', $anonymous, I18N::number($anonymous)) . '</b>'; 88 } 89 90 if ($count_logged_in > 0) { 91 if ($anonymous) { 92 if ($type === 'list') { 93 $content .= '<br><br>'; 94 } else { 95 $content .= ' ' . I18N::translate('and') . ' '; 96 } 97 } 98 $content .= '<b>' . I18N::plural('%s signed-in user', '%s signed-in users', $count_logged_in, I18N::number($count_logged_in)) . '</b>'; 99 if ($type === 'list') { 100 $content .= '<ul>'; 101 } else { 102 $content .= ': '; 103 } 104 } 105 106 if (Auth::check()) { 107 foreach ($logged_in as $user) { 108 if ($type === 'list') { 109 $content .= '<li>'; 110 } 111 112 $individual = Individual::getInstance($this->tree->getUserPreference($user, 'gedcomid'), $this->tree); 113 114 if ($individual instanceof Individual && $individual->canShow()) { 115 $content .= '<a href="' . e($individual->url()) . '">' . e($user->realName()) . '</a>'; 116 } else { 117 $content .= e($user->realName()); 118 } 119 120 $content .= ' - ' . e($user->userName()); 121 122 if (($user->getPreference('contactmethod') !== 'none') && (Auth::id() !== $user->id())) { 123 if ($type === 'list') { 124 $content .= '<br>'; 125 } 126 $content .= '<a href="' . e(route(MessagePage::class, ['to' => $user->userName(), 'tree' => $this->tree->name()])) . '" class="btn btn-link" title="' . I18N::translate('Send a message') . '">' . view('icons/email') . '</a>'; 127 } 128 129 if ($type === 'list') { 130 $content .= '</li>'; 131 } 132 } 133 } 134 135 if ($type === 'list') { 136 $content .= '</ul>'; 137 } 138 139 return $content; 140 } 141 142 /** 143 * @inheritDoc 144 */ 145 public function usersLoggedIn(): string 146 { 147 return $this->usersLoggedInQuery('nolist'); 148 } 149 150 /** 151 * @inheritDoc 152 */ 153 public function usersLoggedInList(): string 154 { 155 return $this->usersLoggedInQuery('list'); 156 } 157 158 /** 159 * Returns true if the given user is visible to others. 160 * 161 * @param UserInterface $user 162 * 163 * @return bool 164 */ 165 private function isUserVisible(UserInterface $user): bool 166 { 167 return Auth::isAdmin() || $user->getPreference('visibleonline'); 168 } 169 170 /** 171 * @inheritDoc 172 */ 173 public function usersLoggedInTotal(): int 174 { 175 return count($this->user_service->allLoggedIn()); 176 } 177 178 /** 179 * @inheritDoc 180 */ 181 public function usersLoggedInTotalAnon(): int 182 { 183 $anonymous = 0; 184 185 foreach ($this->user_service->allLoggedIn() as $user) { 186 if (!$this->isUserVisible($user)) { 187 ++$anonymous; 188 } 189 } 190 191 return $anonymous; 192 } 193 194 /** 195 * @inheritDoc 196 */ 197 public function usersLoggedInTotalVisible(): int 198 { 199 $visible = 0; 200 201 foreach ($this->user_service->allLoggedIn() as $user) { 202 if ($this->isUserVisible($user)) { 203 ++$visible; 204 } 205 } 206 207 return $visible; 208 } 209 210 /** 211 * @inheritDoc 212 */ 213 public function userId(): string 214 { 215 return (string) Auth::id(); 216 } 217 218 /** 219 * @inheritDoc 220 */ 221 public function userName(string $visitor_text = ''): string 222 { 223 if (Auth::check()) { 224 return e(Auth::user()->userName()); 225 } 226 227 // if #username:visitor# was specified, then "visitor" will be returned when the user is not logged in 228 return e($visitor_text); 229 } 230 231 /** 232 * @inheritDoc 233 */ 234 public function userFullName(): string 235 { 236 return Auth::check() ? '<span dir="auto">' . e(Auth::user()->realName()) . '</span>' : ''; 237 } 238 239 /** 240 * Returns the user count. 241 * 242 * @return int 243 */ 244 private function getUserCount(): int 245 { 246 return count($this->user_service->all()); 247 } 248 249 /** 250 * Returns the administrator count. 251 * 252 * @return int 253 */ 254 private function getAdminCount(): int 255 { 256 return count($this->user_service->administrators()); 257 } 258 259 /** 260 * @inheritDoc 261 */ 262 public function totalUsers(): string 263 { 264 return I18N::number($this->getUserCount()); 265 } 266 267 /** 268 * @inheritDoc 269 */ 270 public function totalAdmins(): string 271 { 272 return I18N::number($this->getAdminCount()); 273 } 274 275 /** 276 * @inheritDoc 277 */ 278 public function totalNonAdmins(): string 279 { 280 return I18N::number($this->getUserCount() - $this->getAdminCount()); 281 } 282} 283