xref: /webtrees/app/Statistics/Repository/LatestUserRepository.php (revision 051c822d024c344f8050d1178cd077a864982c84)
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\Carbon;
22use Fisharebest\Webtrees\Contracts\UserInterface;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Services\UserService;
25use Fisharebest\Webtrees\Statistics\Repository\Interfaces\LatestUserRepositoryInterface;
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     * @inheritDoc
52     */
53    public function latestUserId(): string
54    {
55        return (string) $this->latestUserQuery()->id();
56    }
57
58    /**
59     * Find the newest user on the site.
60     * If no user has registered (i.e. all created by the admin), then
61     * return the current user.
62     *
63     * @return UserInterface
64     */
65    private function latestUserQuery(): UserInterface
66    {
67        static $user;
68
69        if ($user instanceof UserInterface) {
70            return $user;
71        }
72
73        $user_id = DB::table('user as u')
74            ->select(['u.user_id'])
75            ->leftJoin('user_setting as us', static function (JoinClause $join): void {
76                $join->on(static function (Builder $query): void {
77                    $query->whereColumn('u.user_id', '=', 'us.user_id')
78                        ->where('us.setting_name', '=', 'reg_timestamp');
79                });
80            })
81            ->orderByDesc('us.setting_value')
82            ->value('user_id');
83
84        $user = $this->user_service->find($user_id) ?? Auth::user();
85
86        return $user;
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        $timestamp = (int) $user->getPreference('reg_timestamp');
113
114        return Carbon::createFromTimestamp($timestamp)->format(strtr($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