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