1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2019 webtrees development team 5 * This program is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation, either version 3 of the License, or 8 * (at your option) any later version. 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. 15 */ 16declare(strict_types=1); 17 18namespace Fisharebest\Webtrees\Statistics\Repository; 19 20use Fisharebest\Webtrees\Auth; 21use Fisharebest\Webtrees\Carbon; 22use Fisharebest\Webtrees\Functions\FunctionsDate; 23use Fisharebest\Webtrees\I18N; 24use Fisharebest\Webtrees\Services\UserService; 25use Fisharebest\Webtrees\Statistics\Repository\Interfaces\LatestUserRepositoryInterface; 26use Fisharebest\Webtrees\User; 27use Illuminate\Database\Capsule\Manager as DB; 28use Illuminate\Database\Query\Builder; 29use Illuminate\Database\Query\JoinClause; 30 31/** 32 * A repository providing methods for latest user related statistics. 33 */ 34class LatestUserRepository implements LatestUserRepositoryInterface 35{ 36 /** 37 * @var UserService 38 */ 39 private $user_service; 40 41 /** 42 * LatestUserRepository constructor. 43 * 44 * @param UserService $user_service 45 */ 46 public function __construct(UserService $user_service) 47 { 48 $this->user_service = $user_service; 49 } 50 51 /** 52 * Find the newest user on the site. 53 * If no user has registered (i.e. all created by the admin), then 54 * return the current user. 55 * 56 * @return User 57 */ 58 private function latestUserQuery(): User 59 { 60 static $user; 61 62 if ($user instanceof User) { 63 return $user; 64 } 65 66 $user_id = DB::table('user as u') 67 ->select(['u.user_id']) 68 ->leftJoin('user_setting as us', function (JoinClause $join): void { 69 $join->on(function (Builder $query): void { 70 $query->whereColumn('u.user_id', '=', 'us.user_id') 71 ->where('us.setting_name', '=', 'reg_timestamp'); 72 }); 73 }) 74 ->orderByDesc('us.setting_value') 75 ->value('user_id'); 76 77 $user = $this->user_service->find($user_id) ?? Auth::user(); 78 79 return $user; 80 } 81 82 /** 83 * @inheritDoc 84 */ 85 public function latestUserId(): string 86 { 87 return (string) $this->latestUserQuery()->id(); 88 } 89 90 /** 91 * @inheritDoc 92 */ 93 public function latestUserName(): string 94 { 95 return e($this->latestUserQuery()->userName()); 96 } 97 98 /** 99 * @inheritDoc 100 */ 101 public function latestUserFullName(): string 102 { 103 return e($this->latestUserQuery()->realName()); 104 } 105 106 /** 107 * @inheritDoc 108 */ 109 public function latestUserRegDate(string $format = null): string 110 { 111 $format = $format ?? I18N::dateFormat(); 112 $user = $this->latestUserQuery(); 113 $timestamp = (int) $user->getPreference('reg_timestamp'); 114 115 return Carbon::createFromTimestamp($timestamp)->format(strtr($format, ['%' => ''])); 116 } 117 118 /** 119 * @inheritDoc 120 */ 121 public function latestUserRegTime(string $format = null): string 122 { 123 $format = $format ?? str_replace('%', '', I18N::timeFormat()); 124 $user = $this->latestUserQuery(); 125 126 return date($format, (int) $user->getPreference('reg_timestamp')); 127 } 128 129 /** 130 * @inheritDoc 131 */ 132 public function latestUserLoggedin(string $yes = null, string $no = null): string 133 { 134 $yes = $yes ?? I18N::translate('yes'); 135 $no = $no ?? I18N::translate('no'); 136 $user = $this->latestUserQuery(); 137 138 $is_logged_in = DB::table('session') 139 ->selectRaw('1') 140 ->where('user_id', '=', $user->id()) 141 ->first(); 142 143 return $is_logged_in ? $yes : $no; 144 } 145} 146