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