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\Carbon; 24use Fisharebest\Webtrees\Contracts\UserInterface; 25use Fisharebest\Webtrees\I18N; 26use Fisharebest\Webtrees\Services\UserService; 27use Fisharebest\Webtrees\Statistics\Repository\Interfaces\LatestUserRepositoryInterface; 28use Illuminate\Database\Capsule\Manager as DB; 29use Illuminate\Database\Query\Builder; 30use Illuminate\Database\Query\JoinClause; 31 32/** 33 * A repository providing methods for latest user related statistics. 34 */ 35class LatestUserRepository implements LatestUserRepositoryInterface 36{ 37 /** 38 * @var UserService 39 */ 40 private $user_service; 41 42 /** 43 * LatestUserRepository constructor. 44 * 45 * @param UserService $user_service 46 */ 47 public function __construct(UserService $user_service) 48 { 49 $this->user_service = $user_service; 50 } 51 52 /** 53 * @return string 54 */ 55 public function latestUserId(): string 56 { 57 return (string) $this->latestUserQuery()->id(); 58 } 59 60 /** 61 * Find the newest user on the site. 62 * If no user has registered (i.e. all created by the admin), then 63 * return the current user. 64 * 65 * @return UserInterface 66 */ 67 private function latestUserQuery(): UserInterface 68 { 69 static $user; 70 71 if ($user instanceof UserInterface) { 72 return $user; 73 } 74 75 $user_id = DB::table('user as u') 76 ->select(['u.user_id']) 77 ->leftJoin('user_setting as us', static function (JoinClause $join): void { 78 $join->on(static function (Builder $query): void { 79 $query->whereColumn('u.user_id', '=', 'us.user_id') 80 ->where('us.setting_name', '=', UserInterface::PREF_TIMESTAMP_REGISTERED); 81 }); 82 }) 83 ->orderByDesc('us.setting_value') 84 ->value('user_id'); 85 86 if ($user_id !== null) { 87 $user_id = (int) $user_id; 88 } 89 90 $user = $this->user_service->find($user_id) ?? Auth::user(); 91 92 return $user; 93 } 94 95 /** 96 * @return string 97 */ 98 public function latestUserName(): string 99 { 100 return e($this->latestUserQuery()->userName()); 101 } 102 103 /** 104 * @return string 105 */ 106 public function latestUserFullName(): string 107 { 108 return e($this->latestUserQuery()->realName()); 109 } 110 111 /** 112 * @param string|null $format 113 * 114 * @return string 115 */ 116 public function latestUserRegDate(string $format = null): string 117 { 118 $format = $format ?? I18N::dateFormat(); 119 $user = $this->latestUserQuery(); 120 $timestamp = (int) $user->getPreference(UserInterface::PREF_TIMESTAMP_REGISTERED); 121 122 return Carbon::createFromTimestamp($timestamp)->format(strtr($format, ['%' => ''])); 123 } 124 125 /** 126 * @param string|null $format 127 * 128 * @return string 129 */ 130 public function latestUserRegTime(string $format = null): string 131 { 132 $format = $format ?? str_replace('%', '', I18N::timeFormat()); 133 $user = $this->latestUserQuery(); 134 135 return date($format, (int) $user->getPreference(UserInterface::PREF_TIMESTAMP_REGISTERED)); 136 } 137 138 /** 139 * @param string|null $yes 140 * @param string|null $no 141 * 142 * @return string 143 */ 144 public function latestUserLoggedin(string $yes = null, string $no = null): string 145 { 146 $yes = $yes ?? I18N::translate('yes'); 147 $no = $no ?? I18N::translate('no'); 148 $user = $this->latestUserQuery(); 149 150 $is_logged_in = DB::table('session') 151 ->selectRaw('1') 152 ->where('user_id', '=', $user->id()) 153 ->first(); 154 155 return $is_logged_in ? $yes : $no; 156 } 157} 158