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 */ 17declare(strict_types=1); 18 19namespace Fisharebest\Webtrees\Statistics\Repository; 20 21use function count; 22 23use Fisharebest\Webtrees\Auth; 24use Fisharebest\Webtrees\Contracts\UserInterface; 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 31/** 32 * A repository providing methods for user related statistics. 33 */ 34class UserRepository implements UserRepositoryInterface 35{ 36 /** 37 * @var Tree 38 */ 39 private $tree; 40 /** 41 * @var UserService 42 */ 43 private $user_service; 44 45 /** 46 * Constructor. 47 * 48 * @param Tree $tree 49 * @param UserService $user_service 50 */ 51 public function __construct(Tree $tree, UserService $user_service) 52 { 53 $this->tree = $tree; 54 $this->user_service = $user_service; 55 } 56 57 /** 58 * Who is currently logged in? 59 * 60 * @param string $type "list" or "nolist" 61 * 62 * @return string 63 */ 64 private function usersLoggedInQuery($type = 'nolist'): string 65 { 66 $content = ''; 67 $anonymous = 0; 68 $logged_in = []; 69 70 foreach ($this->user_service->allLoggedIn() as $user) { 71 if (Auth::isAdmin() || $user->getPreference('visibleonline')) { 72 $logged_in[] = $user; 73 } else { 74 $anonymous++; 75 } 76 } 77 78 $count_logged_in = count($logged_in); 79 80 if ($count_logged_in === 0 && $anonymous === 0) { 81 $content .= I18N::translate('No signed-in and no anonymous users'); 82 } 83 84 if ($anonymous > 0) { 85 $content .= '<b>' . I18N::plural('%s anonymous signed-in user', '%s anonymous signed-in users', $anonymous, I18N::number($anonymous)) . '</b>'; 86 } 87 88 if ($count_logged_in > 0) { 89 if ($anonymous) { 90 if ($type === 'list') { 91 $content .= '<br><br>'; 92 } else { 93 $content .= ' ' . I18N::translate('and') . ' '; 94 } 95 } 96 $content .= '<b>' . I18N::plural('%s signed-in user', '%s signed-in users', $count_logged_in, I18N::number($count_logged_in)) . '</b>'; 97 if ($type === 'list') { 98 $content .= '<ul>'; 99 } else { 100 $content .= ': '; 101 } 102 } 103 104 if (Auth::check()) { 105 foreach ($logged_in as $user) { 106 if ($type === 'list') { 107 $content .= '<li>'; 108 } 109 110 $individual = Individual::getInstance($this->tree->getUserPreference($user, 'gedcomid'), $this->tree); 111 112 if ($individual instanceof Individual && $individual->canShow()) { 113 $content .= '<a href="' . e($individual->url()) . '">' . e($user->realName()) . '</a>'; 114 } else { 115 $content .= e($user->realName()); 116 } 117 118 $content .= ' - ' . e($user->userName()); 119 120 if (($user->getPreference('contactmethod') !== 'none') 121 && (Auth::id() !== $user->id()) 122 ) { 123 if ($type === 'list') { 124 $content .= '<br>'; 125 } 126 $content .= '<a href="' . e(route('message', ['to' => $user->userName(), 'ged' => $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