xref: /webtrees/app/Statistics/Repository/LatestUserRepository.php (revision 4e73f0bda96a34c06bd4b8e67501ef4955895fbf)
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\Functions\FunctionsDate;
22use Fisharebest\Webtrees\I18N;
23use Fisharebest\Webtrees\Services\UserService;
24use Fisharebest\Webtrees\Statistics\Repository\Interfaces\LatestUserRepositoryInterface;
25use Fisharebest\Webtrees\User;
26use Illuminate\Database\Capsule\Manager as DB;
27use Illuminate\Database\Query\Builder;
28use Illuminate\Database\Query\JoinClause;
29
30/**
31 * A repository providing methods for latest user related statistics.
32 */
33class LatestUserRepository implements LatestUserRepositoryInterface
34{
35    /**
36     * @var UserService
37     */
38    private $user_service;
39
40    /**
41     * LatestUserRepository constructor.
42     *
43     * @param UserService $user_service
44     */
45    public function __construct(UserService $user_service)
46    {
47        $this->user_service = $user_service;
48    }
49
50    /**
51     * Find the newest user on the site.
52     * If no user has registered (i.e. all created by the admin), then
53     * return the current user.
54     *
55     * @return User
56     */
57    private function latestUserQuery(): User
58    {
59        static $user;
60
61        if ($user instanceof User) {
62            return $user;
63        }
64
65        $user_id = DB::table('user as u')
66            ->select(['u.user_id'])
67            ->leftJoin('user_setting as us', function (JoinClause $join): void {
68                $join->on(function (Builder $query): void {
69                    $query->whereColumn('u.user_id', '=', 'us.user_id')
70                        ->where('us.setting_name', '=', 'reg_timestamp');
71                });
72            })
73            ->orderByDesc('us.setting_value')
74            ->value('user_id');
75
76        $user = $this->user_service->find($user_id) ?? Auth::user();
77
78        return $user;
79    }
80
81    /**
82     * @inheritDoc
83     */
84    public function latestUserId(): string
85    {
86        return (string) $this->latestUserQuery()->id();
87    }
88
89    /**
90     * @inheritDoc
91     */
92    public function latestUserName(): string
93    {
94        return e($this->latestUserQuery()->userName());
95    }
96
97    /**
98     * @inheritDoc
99     */
100    public function latestUserFullName(): string
101    {
102        return e($this->latestUserQuery()->realName());
103    }
104
105    /**
106     * @inheritDoc
107     */
108    public function latestUserRegDate(string $format = null): string
109    {
110        $format = $format ?? I18N::dateFormat();
111        $user   = $this->latestUserQuery();
112
113        return FunctionsDate::timestampToGedcomDate(
114            (int) $user->getPreference('reg_timestamp')
115        )->display(false, $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