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