xref: /webtrees/app/Statistics/Repository/LatestUserRepository.php (revision 2c6f1bd538f46b93645991518398bb087011cb42)
18add1155SRico Sonntag<?php
23976b470SGreg Roach
38add1155SRico Sonntag/**
48add1155SRico Sonntag * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
68add1155SRico Sonntag * This program is free software: you can redistribute it and/or modify
78add1155SRico Sonntag * it under the terms of the GNU General Public License as published by
88add1155SRico Sonntag * the Free Software Foundation, either version 3 of the License, or
98add1155SRico Sonntag * (at your option) any later version.
108add1155SRico Sonntag * This program is distributed in the hope that it will be useful,
118add1155SRico Sonntag * but WITHOUT ANY WARRANTY; without even the implied warranty of
128add1155SRico Sonntag * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
138add1155SRico Sonntag * GNU General Public License for more details.
148add1155SRico Sonntag * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
168add1155SRico Sonntag */
17fcfa147eSGreg Roach
188add1155SRico Sonntagdeclare(strict_types=1);
198add1155SRico Sonntag
208add1155SRico Sonntagnamespace Fisharebest\Webtrees\Statistics\Repository;
218add1155SRico Sonntag
228add1155SRico Sonntaguse Fisharebest\Webtrees\Auth;
23e364afe4SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
246f4ec3caSGreg Roachuse Fisharebest\Webtrees\DB;
258add1155SRico Sonntaguse Fisharebest\Webtrees\I18N;
26d97083feSGreg Roachuse Fisharebest\Webtrees\Registry;
27e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Services\UserService;
288add1155SRico Sonntaguse Fisharebest\Webtrees\Statistics\Repository\Interfaces\LatestUserRepositoryInterface;
298add1155SRico Sonntaguse Illuminate\Database\Query\Builder;
308add1155SRico Sonntaguse Illuminate\Database\Query\JoinClause;
318add1155SRico Sonntag
324c78e066SGreg Roachuse function date;
334c78e066SGreg Roachuse function e;
344c78e066SGreg Roachuse function str_replace;
354c78e066SGreg Roach
368add1155SRico Sonntag/**
378add1155SRico Sonntag * A repository providing methods for latest user related statistics.
388add1155SRico Sonntag */
398add1155SRico Sonntagclass LatestUserRepository implements LatestUserRepositoryInterface
408add1155SRico Sonntag{
414c78e066SGreg Roach    private UserService $user_service;
42e5a6b4d4SGreg Roach
43e5a6b4d4SGreg Roach    /**
44e5a6b4d4SGreg Roach     * @param UserService $user_service
45e5a6b4d4SGreg Roach     */
46e2cbf57aSGreg Roach    public function __construct(UserService $user_service)
47e2cbf57aSGreg Roach    {
48e5a6b4d4SGreg Roach        $this->user_service = $user_service;
49e5a6b4d4SGreg Roach    }
50e5a6b4d4SGreg Roach
51e5a6b4d4SGreg Roach    /**
520dcd9387SGreg Roach     * @return string
53e364afe4SGreg Roach     */
54e364afe4SGreg Roach    public function latestUserId(): string
55e364afe4SGreg Roach    {
56e364afe4SGreg Roach        return (string) $this->latestUserQuery()->id();
57e364afe4SGreg Roach    }
58e364afe4SGreg Roach
59e364afe4SGreg Roach    /**
60e5a6b4d4SGreg Roach     * Find the newest user on the site.
618add1155SRico Sonntag     * If no user has registered (i.e. all created by the admin), then
628add1155SRico Sonntag     * return the current user.
638add1155SRico Sonntag     *
64e364afe4SGreg Roach     * @return UserInterface
658add1155SRico Sonntag     */
66e364afe4SGreg Roach    private function latestUserQuery(): UserInterface
678add1155SRico Sonntag    {
688add1155SRico Sonntag        static $user;
698add1155SRico Sonntag
70e364afe4SGreg Roach        if ($user instanceof UserInterface) {
718add1155SRico Sonntag            return $user;
728add1155SRico Sonntag        }
738add1155SRico Sonntag
748add1155SRico Sonntag        $user_id = DB::table('user as u')
758add1155SRico Sonntag            ->select(['u.user_id'])
760b5fd0a6SGreg Roach            ->leftJoin('user_setting as us', static function (JoinClause $join): void {
770b5fd0a6SGreg Roach                $join->on(static function (Builder $query): void {
788add1155SRico Sonntag                    $query->whereColumn('u.user_id', '=', 'us.user_id')
791fe542e9SGreg Roach                        ->where('us.setting_name', '=', UserInterface::PREF_TIMESTAMP_REGISTERED);
808add1155SRico Sonntag                });
818add1155SRico Sonntag            })
828add1155SRico Sonntag            ->orderByDesc('us.setting_value')
838add1155SRico Sonntag            ->value('user_id');
848add1155SRico Sonntag
8524f2a3afSGreg Roach        if ($user_id !== null) {
8624f2a3afSGreg Roach            $user_id = (int) $user_id;
8724f2a3afSGreg Roach        }
8824f2a3afSGreg Roach
89f78da678SGreg Roach        return $this->user_service->find($user_id) ?? Auth::user();
908add1155SRico Sonntag    }
918add1155SRico Sonntag
928add1155SRico Sonntag    /**
930dcd9387SGreg Roach     * @return string
948add1155SRico Sonntag     */
958add1155SRico Sonntag    public function latestUserName(): string
968add1155SRico Sonntag    {
97e5a6b4d4SGreg Roach        return e($this->latestUserQuery()->userName());
988add1155SRico Sonntag    }
998add1155SRico Sonntag
1008add1155SRico Sonntag    /**
1010dcd9387SGreg Roach     * @return string
1028add1155SRico Sonntag     */
1038add1155SRico Sonntag    public function latestUserFullName(): string
1048add1155SRico Sonntag    {
105e5a6b4d4SGreg Roach        return e($this->latestUserQuery()->realName());
1068add1155SRico Sonntag    }
1078add1155SRico Sonntag
1088add1155SRico Sonntag    /**
1090dcd9387SGreg Roach     * @param string|null $format
1100dcd9387SGreg Roach     *
1110dcd9387SGreg Roach     * @return string
1128add1155SRico Sonntag     */
113*2c6f1bd5SGreg Roach    public function latestUserRegDate(string|null $format = null): string
1148add1155SRico Sonntag    {
1153529c469SGreg Roach        $format ??= I18N::dateFormat();
1168add1155SRico Sonntag        $user      = $this->latestUserQuery();
1171fe542e9SGreg Roach        $timestamp = (int) $user->getPreference(UserInterface::PREF_TIMESTAMP_REGISTERED);
1188add1155SRico Sonntag
119d97083feSGreg Roach        return Registry::timestampFactory()->make($timestamp)->format(strtr($format, ['%' => '']));
1208add1155SRico Sonntag    }
1218add1155SRico Sonntag
1228add1155SRico Sonntag    /**
1230dcd9387SGreg Roach     * @param string|null $format
1240dcd9387SGreg Roach     *
1250dcd9387SGreg Roach     * @return string
1268add1155SRico Sonntag     */
127*2c6f1bd5SGreg Roach    public function latestUserRegTime(string|null $format = null): string
1288add1155SRico Sonntag    {
1293529c469SGreg Roach        $format ??= str_replace('%', '', I18N::timeFormat());
1308add1155SRico Sonntag        $user = $this->latestUserQuery();
1318add1155SRico Sonntag
1321fe542e9SGreg Roach        return date($format, (int) $user->getPreference(UserInterface::PREF_TIMESTAMP_REGISTERED));
1338add1155SRico Sonntag    }
1348add1155SRico Sonntag
1358add1155SRico Sonntag    /**
1360dcd9387SGreg Roach     * @param string|null $yes
1370dcd9387SGreg Roach     * @param string|null $no
1380dcd9387SGreg Roach     *
1390dcd9387SGreg Roach     * @return string
1408add1155SRico Sonntag     */
141*2c6f1bd5SGreg Roach    public function latestUserLoggedin(string|null $yes = null, string|null $no = null): string
1428add1155SRico Sonntag    {
1433529c469SGreg Roach        $yes ??= I18N::translate('yes');
1443529c469SGreg Roach        $no ??= I18N::translate('no');
1458add1155SRico Sonntag        $user = $this->latestUserQuery();
1468add1155SRico Sonntag
1478add1155SRico Sonntag        $is_logged_in = DB::table('session')
1488add1155SRico Sonntag            ->selectRaw('1')
1498add1155SRico Sonntag            ->where('user_id', '=', $user->id())
1508add1155SRico Sonntag            ->first();
1518add1155SRico Sonntag
152ef475b14SGreg Roach        return $is_logged_in !== null ? $yes : $no;
1538add1155SRico Sonntag    }
1548add1155SRico Sonntag}
155