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 $user = $this->user_service->find($user_id) ?? Auth::user(); 87 88 return $user; 89 } 90 91 /** 92 * @return string 93 */ 94 public function latestUserName(): string 95 { 96 return e($this->latestUserQuery()->userName()); 97 } 98 99 /** 100 * @return string 101 */ 102 public function latestUserFullName(): string 103 { 104 return e($this->latestUserQuery()->realName()); 105 } 106 107 /** 108 * @param string|null $format 109 * 110 * @return string 111 */ 112 public function latestUserRegDate(string $format = null): string 113 { 114 $format = $format ?? I18N::dateFormat(); 115 $user = $this->latestUserQuery(); 116 $timestamp = (int) $user->getPreference(UserInterface::PREF_TIMESTAMP_REGISTERED); 117 118 return Carbon::createFromTimestamp($timestamp)->format(strtr($format, ['%' => ''])); 119 } 120 121 /** 122 * @param string|null $format 123 * 124 * @return string 125 */ 126 public function latestUserRegTime(string $format = null): string 127 { 128 $format = $format ?? str_replace('%', '', I18N::timeFormat()); 129 $user = $this->latestUserQuery(); 130 131 return date($format, (int) $user->getPreference(UserInterface::PREF_TIMESTAMP_REGISTERED)); 132 } 133 134 /** 135 * @param string|null $yes 136 * @param string|null $no 137 * 138 * @return string 139 */ 140 public function latestUserLoggedin(string $yes = null, string $no = null): string 141 { 142 $yes = $yes ?? I18N::translate('yes'); 143 $no = $no ?? I18N::translate('no'); 144 $user = $this->latestUserQuery(); 145 146 $is_logged_in = DB::table('session') 147 ->selectRaw('1') 148 ->where('user_id', '=', $user->id()) 149 ->first(); 150 151 return $is_logged_in ? $yes : $no; 152 } 153} 154