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