1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2020 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\Registry; 25use Fisharebest\Webtrees\Http\RequestHandlers\MessagePage; 26use Fisharebest\Webtrees\I18N; 27use Fisharebest\Webtrees\Individual; 28use Fisharebest\Webtrees\Services\UserService; 29use Fisharebest\Webtrees\Statistics\Repository\Interfaces\UserRepositoryInterface; 30use Fisharebest\Webtrees\Tree; 31use Fisharebest\Webtrees\User; 32 33use function count; 34 35/** 36 * A repository providing methods for user related statistics. 37 */ 38class UserRepository implements UserRepositoryInterface 39{ 40 /** 41 * @var Tree 42 */ 43 private $tree; 44 /** 45 * @var UserService 46 */ 47 private $user_service; 48 49 /** 50 * Constructor. 51 * 52 * @param Tree $tree 53 * @param UserService $user_service 54 */ 55 public function __construct(Tree $tree, UserService $user_service) 56 { 57 $this->tree = $tree; 58 $this->user_service = $user_service; 59 } 60 61 /** 62 * Who is currently logged in? 63 * 64 * @param string $type "list" or "nolist" 65 * 66 * @return string 67 */ 68 private function usersLoggedInQuery($type = 'nolist'): string 69 { 70 $content = ''; 71 $anonymous = 0; 72 $logged_in = []; 73 74 foreach ($this->user_service->allLoggedIn() as $user) { 75 if (Auth::isAdmin() || $user->getPreference(User::PREF_IS_VISIBLE_ONLINE) === '1') { 76 $logged_in[] = $user; 77 } else { 78 $anonymous++; 79 } 80 } 81 82 $count_logged_in = count($logged_in); 83 84 if ($count_logged_in === 0 && $anonymous === 0) { 85 $content .= I18N::translate('No signed-in and no anonymous users'); 86 } 87 88 if ($anonymous > 0) { 89 $content .= '<b>' . I18N::plural('%s anonymous signed-in user', '%s anonymous signed-in users', $anonymous, I18N::number($anonymous)) . '</b>'; 90 } 91 92 if ($count_logged_in > 0) { 93 if ($anonymous) { 94 if ($type === 'list') { 95 $content .= '<br><br>'; 96 } else { 97 $content .= ' ' . I18N::translate('and') . ' '; 98 } 99 } 100 $content .= '<b>' . I18N::plural('%s signed-in user', '%s signed-in users', $count_logged_in, I18N::number($count_logged_in)) . '</b>'; 101 if ($type === 'list') { 102 $content .= '<ul>'; 103 } else { 104 $content .= ': '; 105 } 106 } 107 108 if (Auth::check()) { 109 foreach ($logged_in as $user) { 110 if ($type === 'list') { 111 $content .= '<li>'; 112 } 113 114 $individual = Registry::individualFactory()->make($this->tree->getUserPreference($user, User::PREF_TREE_ACCOUNT_XREF), $this->tree); 115 116 if ($individual instanceof Individual && $individual->canShow()) { 117 $content .= '<a href="' . e($individual->url()) . '">' . e($user->realName()) . '</a>'; 118 } else { 119 $content .= e($user->realName()); 120 } 121 122 $content .= ' - ' . e($user->userName()); 123 124 if (($user->getPreference(User::PREF_CONTACT_METHOD) !== 'none') && (Auth::id() !== $user->id())) { 125 if ($type === 'list') { 126 $content .= '<br>'; 127 } 128 $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>'; 129 } 130 131 if ($type === 'list') { 132 $content .= '</li>'; 133 } 134 } 135 } 136 137 if ($type === 'list') { 138 $content .= '</ul>'; 139 } 140 141 return $content; 142 } 143 144 /** 145 * @return string 146 */ 147 public function usersLoggedIn(): string 148 { 149 return $this->usersLoggedInQuery('nolist'); 150 } 151 152 /** 153 * @return string 154 */ 155 public function usersLoggedInList(): string 156 { 157 return $this->usersLoggedInQuery('list'); 158 } 159 160 /** 161 * Returns true if the given user is visible to others. 162 * 163 * @param UserInterface $user 164 * 165 * @return bool 166 */ 167 private function isUserVisible(UserInterface $user): bool 168 { 169 return Auth::isAdmin() || $user->getPreference(User::PREF_IS_VISIBLE_ONLINE) === '1'; 170 } 171 172 /** 173 * @return int 174 */ 175 public function usersLoggedInTotal(): int 176 { 177 return count($this->user_service->allLoggedIn()); 178 } 179 180 /** 181 * @return int 182 */ 183 public function usersLoggedInTotalAnon(): int 184 { 185 $anonymous = 0; 186 187 foreach ($this->user_service->allLoggedIn() as $user) { 188 if (!$this->isUserVisible($user)) { 189 ++$anonymous; 190 } 191 } 192 193 return $anonymous; 194 } 195 196 /** 197 * @return int 198 */ 199 public function usersLoggedInTotalVisible(): int 200 { 201 $visible = 0; 202 203 foreach ($this->user_service->allLoggedIn() as $user) { 204 if ($this->isUserVisible($user)) { 205 ++$visible; 206 } 207 } 208 209 return $visible; 210 } 211 212 /** 213 * @return string 214 */ 215 public function userId(): string 216 { 217 return (string) Auth::id(); 218 } 219 220 /** 221 * @param string $visitor_text 222 * 223 * @return string 224 */ 225 public function userName(string $visitor_text = ''): string 226 { 227 if (Auth::check()) { 228 return e(Auth::user()->userName()); 229 } 230 231 // if #username:visitor# was specified, then "visitor" will be returned when the user is not logged in 232 return e($visitor_text); 233 } 234 235 /** 236 * @return string 237 */ 238 public function userFullName(): string 239 { 240 return Auth::check() ? '<span dir="auto">' . e(Auth::user()->realName()) . '</span>' : ''; 241 } 242 243 /** 244 * Returns the user count. 245 * 246 * @return int 247 */ 248 private function getUserCount(): int 249 { 250 return count($this->user_service->all()); 251 } 252 253 /** 254 * Returns the administrator count. 255 * 256 * @return int 257 */ 258 private function getAdminCount(): int 259 { 260 return count($this->user_service->administrators()); 261 } 262 263 /** 264 * @return string 265 */ 266 public function totalUsers(): string 267 { 268 return I18N::number($this->getUserCount()); 269 } 270 271 /** 272 * @return string 273 */ 274 public function totalAdmins(): string 275 { 276 return I18N::number($this->getAdminCount()); 277 } 278 279 /** 280 * @return string 281 */ 282 public function totalNonAdmins(): string 283 { 284 return I18N::number($this->getUserCount() - $this->getAdminCount()); 285 } 286} 287