xref: /webtrees/app/Statistics/Repository/LatestUserRepository.php (revision 1fe542e96f8f7eedeebc278fae1e0ab0d9e74d95)
18add1155SRico Sonntag<?php
23976b470SGreg Roach
38add1155SRico Sonntag/**
48add1155SRico Sonntag * webtrees: online genealogy
5*1fe542e9SGreg Roach * Copyright (C) 2021 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
158add1155SRico Sonntag * along with this program. If not, see <http://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;
234459dc9aSGreg Roachuse Fisharebest\Webtrees\Carbon;
24e364afe4SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
258add1155SRico Sonntaguse Fisharebest\Webtrees\I18N;
26e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Services\UserService;
278add1155SRico Sonntaguse Fisharebest\Webtrees\Statistics\Repository\Interfaces\LatestUserRepositoryInterface;
288add1155SRico Sonntaguse Illuminate\Database\Capsule\Manager as DB;
298add1155SRico Sonntaguse Illuminate\Database\Query\Builder;
308add1155SRico Sonntaguse Illuminate\Database\Query\JoinClause;
318add1155SRico Sonntag
328add1155SRico Sonntag/**
338add1155SRico Sonntag * A repository providing methods for latest user related statistics.
348add1155SRico Sonntag */
358add1155SRico Sonntagclass LatestUserRepository implements LatestUserRepositoryInterface
368add1155SRico Sonntag{
378add1155SRico Sonntag    /**
38e5a6b4d4SGreg Roach     * @var UserService
39e5a6b4d4SGreg Roach     */
40e5a6b4d4SGreg Roach    private $user_service;
41e5a6b4d4SGreg Roach
42e5a6b4d4SGreg Roach    /**
43e5a6b4d4SGreg Roach     * LatestUserRepository constructor.
448add1155SRico Sonntag     *
45e5a6b4d4SGreg Roach     * @param UserService $user_service
46e5a6b4d4SGreg Roach     */
47e2cbf57aSGreg Roach    public function __construct(UserService $user_service)
48e2cbf57aSGreg Roach    {
49e5a6b4d4SGreg Roach        $this->user_service = $user_service;
50e5a6b4d4SGreg Roach    }
51e5a6b4d4SGreg Roach
52e5a6b4d4SGreg Roach    /**
530dcd9387SGreg Roach     * @return string
54e364afe4SGreg Roach     */
55e364afe4SGreg Roach    public function latestUserId(): string
56e364afe4SGreg Roach    {
57e364afe4SGreg Roach        return (string) $this->latestUserQuery()->id();
58e364afe4SGreg Roach    }
59e364afe4SGreg Roach
60e364afe4SGreg Roach    /**
61e5a6b4d4SGreg Roach     * Find the newest user on the site.
628add1155SRico Sonntag     * If no user has registered (i.e. all created by the admin), then
638add1155SRico Sonntag     * return the current user.
648add1155SRico Sonntag     *
65e364afe4SGreg Roach     * @return UserInterface
668add1155SRico Sonntag     */
67e364afe4SGreg Roach    private function latestUserQuery(): UserInterface
688add1155SRico Sonntag    {
698add1155SRico Sonntag        static $user;
708add1155SRico Sonntag
71e364afe4SGreg Roach        if ($user instanceof UserInterface) {
728add1155SRico Sonntag            return $user;
738add1155SRico Sonntag        }
748add1155SRico Sonntag
758add1155SRico Sonntag        $user_id = DB::table('user as u')
768add1155SRico Sonntag            ->select(['u.user_id'])
770b5fd0a6SGreg Roach            ->leftJoin('user_setting as us', static function (JoinClause $join): void {
780b5fd0a6SGreg Roach                $join->on(static function (Builder $query): void {
798add1155SRico Sonntag                    $query->whereColumn('u.user_id', '=', 'us.user_id')
80*1fe542e9SGreg Roach                        ->where('us.setting_name', '=', UserInterface::PREF_TIMESTAMP_REGISTERED);
818add1155SRico Sonntag                });
828add1155SRico Sonntag            })
838add1155SRico Sonntag            ->orderByDesc('us.setting_value')
848add1155SRico Sonntag            ->value('user_id');
858add1155SRico Sonntag
86e5a6b4d4SGreg Roach        $user = $this->user_service->find($user_id) ?? Auth::user();
878add1155SRico Sonntag
888add1155SRico Sonntag        return $user;
898add1155SRico Sonntag    }
908add1155SRico Sonntag
918add1155SRico Sonntag    /**
920dcd9387SGreg Roach     * @return string
938add1155SRico Sonntag     */
948add1155SRico Sonntag    public function latestUserName(): string
958add1155SRico Sonntag    {
96e5a6b4d4SGreg Roach        return e($this->latestUserQuery()->userName());
978add1155SRico Sonntag    }
988add1155SRico Sonntag
998add1155SRico Sonntag    /**
1000dcd9387SGreg Roach     * @return string
1018add1155SRico Sonntag     */
1028add1155SRico Sonntag    public function latestUserFullName(): string
1038add1155SRico Sonntag    {
104e5a6b4d4SGreg Roach        return e($this->latestUserQuery()->realName());
1058add1155SRico Sonntag    }
1068add1155SRico Sonntag
1078add1155SRico Sonntag    /**
1080dcd9387SGreg Roach     * @param string|null $format
1090dcd9387SGreg Roach     *
1100dcd9387SGreg Roach     * @return string
1118add1155SRico Sonntag     */
1128add1155SRico Sonntag    public function latestUserRegDate(string $format = null): string
1138add1155SRico Sonntag    {
1148add1155SRico Sonntag        $format    = $format ?? I18N::dateFormat();
1158add1155SRico Sonntag        $user      = $this->latestUserQuery();
116*1fe542e9SGreg Roach        $timestamp = (int) $user->getPreference(UserInterface::PREF_TIMESTAMP_REGISTERED);
1178add1155SRico Sonntag
1184459dc9aSGreg Roach        return Carbon::createFromTimestamp($timestamp)->format(strtr($format, ['%' => '']));
1198add1155SRico Sonntag    }
1208add1155SRico Sonntag
1218add1155SRico Sonntag    /**
1220dcd9387SGreg Roach     * @param string|null $format
1230dcd9387SGreg Roach     *
1240dcd9387SGreg Roach     * @return string
1258add1155SRico Sonntag     */
1268add1155SRico Sonntag    public function latestUserRegTime(string $format = null): string
1278add1155SRico Sonntag    {
1288add1155SRico Sonntag        $format = $format ?? str_replace('%', '', I18N::timeFormat());
1298add1155SRico Sonntag        $user   = $this->latestUserQuery();
1308add1155SRico Sonntag
131*1fe542e9SGreg Roach        return date($format, (int) $user->getPreference(UserInterface::PREF_TIMESTAMP_REGISTERED));
1328add1155SRico Sonntag    }
1338add1155SRico Sonntag
1348add1155SRico Sonntag    /**
1350dcd9387SGreg Roach     * @param string|null $yes
1360dcd9387SGreg Roach     * @param string|null $no
1370dcd9387SGreg Roach     *
1380dcd9387SGreg Roach     * @return string
1398add1155SRico Sonntag     */
1408add1155SRico Sonntag    public function latestUserLoggedin(string $yes = null, string $no = null): string
1418add1155SRico Sonntag    {
1428add1155SRico Sonntag        $yes  = $yes ?? I18N::translate('yes');
1438add1155SRico Sonntag        $no   = $no ?? I18N::translate('no');
1448add1155SRico Sonntag        $user = $this->latestUserQuery();
1458add1155SRico Sonntag
1468add1155SRico Sonntag        $is_logged_in = DB::table('session')
1478add1155SRico Sonntag            ->selectRaw('1')
1488add1155SRico Sonntag            ->where('user_id', '=', $user->id())
1498add1155SRico Sonntag            ->first();
1508add1155SRico Sonntag
1518add1155SRico Sonntag        return $is_logged_in ? $yes : $no;
1528add1155SRico Sonntag    }
1538add1155SRico Sonntag}
154