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