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