xref: /webtrees/app/Statistics/Repository/LatestUserRepository.php (revision 0b5fd0a636fa959f5279ee28ebd2f27e921c091e)
18add1155SRico Sonntag<?php
28add1155SRico Sonntag/**
38add1155SRico Sonntag * webtrees: online genealogy
4242a7862SGreg Roach * Copyright (C) 2019 webtrees development team
58add1155SRico Sonntag * This program is free software: you can redistribute it and/or modify
68add1155SRico Sonntag * it under the terms of the GNU General Public License as published by
78add1155SRico Sonntag * the Free Software Foundation, either version 3 of the License, or
88add1155SRico Sonntag * (at your option) any later version.
98add1155SRico Sonntag * This program is distributed in the hope that it will be useful,
108add1155SRico Sonntag * but WITHOUT ANY WARRANTY; without even the implied warranty of
118add1155SRico Sonntag * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
128add1155SRico Sonntag * GNU General Public License for more details.
138add1155SRico Sonntag * You should have received a copy of the GNU General Public License
148add1155SRico Sonntag * along with this program. If not, see <http://www.gnu.org/licenses/>.
158add1155SRico Sonntag */
168add1155SRico Sonntagdeclare(strict_types=1);
178add1155SRico Sonntag
188add1155SRico Sonntagnamespace Fisharebest\Webtrees\Statistics\Repository;
198add1155SRico Sonntag
208add1155SRico Sonntaguse Fisharebest\Webtrees\Auth;
214459dc9aSGreg Roachuse Fisharebest\Webtrees\Carbon;
22e364afe4SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
238add1155SRico Sonntaguse Fisharebest\Webtrees\I18N;
24e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Services\UserService;
258add1155SRico Sonntaguse Fisharebest\Webtrees\Statistics\Repository\Interfaces\LatestUserRepositoryInterface;
268add1155SRico Sonntaguse Illuminate\Database\Capsule\Manager as DB;
278add1155SRico Sonntaguse Illuminate\Database\Query\Builder;
288add1155SRico Sonntaguse Illuminate\Database\Query\JoinClause;
298add1155SRico Sonntag
308add1155SRico Sonntag/**
318add1155SRico Sonntag * A repository providing methods for latest user related statistics.
328add1155SRico Sonntag */
338add1155SRico Sonntagclass LatestUserRepository implements LatestUserRepositoryInterface
348add1155SRico Sonntag{
358add1155SRico Sonntag    /**
36e5a6b4d4SGreg Roach     * @var UserService
37e5a6b4d4SGreg Roach     */
38e5a6b4d4SGreg Roach    private $user_service;
39e5a6b4d4SGreg Roach
40e5a6b4d4SGreg Roach    /**
41e5a6b4d4SGreg Roach     * LatestUserRepository constructor.
428add1155SRico Sonntag     *
43e5a6b4d4SGreg Roach     * @param UserService $user_service
44e5a6b4d4SGreg Roach     */
45e2cbf57aSGreg Roach    public function __construct(UserService $user_service)
46e2cbf57aSGreg Roach    {
47e5a6b4d4SGreg Roach        $this->user_service = $user_service;
48e5a6b4d4SGreg Roach    }
49e5a6b4d4SGreg Roach
50e5a6b4d4SGreg Roach    /**
51e364afe4SGreg Roach     * @inheritDoc
52e364afe4SGreg Roach     */
53e364afe4SGreg Roach    public function latestUserId(): string
54e364afe4SGreg Roach    {
55e364afe4SGreg Roach        return (string) $this->latestUserQuery()->id();
56e364afe4SGreg Roach    }
57e364afe4SGreg Roach
58e364afe4SGreg Roach    /**
59e5a6b4d4SGreg Roach     * Find the newest user on the site.
608add1155SRico Sonntag     * If no user has registered (i.e. all created by the admin), then
618add1155SRico Sonntag     * return the current user.
628add1155SRico Sonntag     *
63e364afe4SGreg Roach     * @return UserInterface
648add1155SRico Sonntag     */
65e364afe4SGreg Roach    private function latestUserQuery(): UserInterface
668add1155SRico Sonntag    {
678add1155SRico Sonntag        static $user;
688add1155SRico Sonntag
69e364afe4SGreg Roach        if ($user instanceof UserInterface) {
708add1155SRico Sonntag            return $user;
718add1155SRico Sonntag        }
728add1155SRico Sonntag
738add1155SRico Sonntag        $user_id = DB::table('user as u')
748add1155SRico Sonntag            ->select(['u.user_id'])
75*0b5fd0a6SGreg Roach            ->leftJoin('user_setting as us', static function (JoinClause $join): void {
76*0b5fd0a6SGreg Roach                $join->on(static function (Builder $query): void {
778add1155SRico Sonntag                    $query->whereColumn('u.user_id', '=', 'us.user_id')
788add1155SRico Sonntag                        ->where('us.setting_name', '=', 'reg_timestamp');
798add1155SRico Sonntag                });
808add1155SRico Sonntag            })
818add1155SRico Sonntag            ->orderByDesc('us.setting_value')
828add1155SRico Sonntag            ->value('user_id');
838add1155SRico Sonntag
84e5a6b4d4SGreg Roach        $user = $this->user_service->find($user_id) ?? Auth::user();
858add1155SRico Sonntag
868add1155SRico Sonntag        return $user;
878add1155SRico Sonntag    }
888add1155SRico Sonntag
898add1155SRico Sonntag    /**
908add1155SRico Sonntag     * @inheritDoc
918add1155SRico Sonntag     */
928add1155SRico Sonntag    public function latestUserName(): string
938add1155SRico Sonntag    {
94e5a6b4d4SGreg Roach        return e($this->latestUserQuery()->userName());
958add1155SRico Sonntag    }
968add1155SRico Sonntag
978add1155SRico Sonntag    /**
988add1155SRico Sonntag     * @inheritDoc
998add1155SRico Sonntag     */
1008add1155SRico Sonntag    public function latestUserFullName(): string
1018add1155SRico Sonntag    {
102e5a6b4d4SGreg Roach        return e($this->latestUserQuery()->realName());
1038add1155SRico Sonntag    }
1048add1155SRico Sonntag
1058add1155SRico Sonntag    /**
1068add1155SRico Sonntag     * @inheritDoc
1078add1155SRico Sonntag     */
1088add1155SRico Sonntag    public function latestUserRegDate(string $format = null): string
1098add1155SRico Sonntag    {
1108add1155SRico Sonntag        $format    = $format ?? I18N::dateFormat();
1118add1155SRico Sonntag        $user      = $this->latestUserQuery();
1124459dc9aSGreg Roach        $timestamp = (int) $user->getPreference('reg_timestamp');
1138add1155SRico Sonntag
1144459dc9aSGreg Roach        return Carbon::createFromTimestamp($timestamp)->format(strtr($format, ['%' => '']));
1158add1155SRico Sonntag    }
1168add1155SRico Sonntag
1178add1155SRico Sonntag    /**
1188add1155SRico Sonntag     * @inheritDoc
1198add1155SRico Sonntag     */
1208add1155SRico Sonntag    public function latestUserRegTime(string $format = null): string
1218add1155SRico Sonntag    {
1228add1155SRico Sonntag        $format = $format ?? str_replace('%', '', I18N::timeFormat());
1238add1155SRico Sonntag        $user   = $this->latestUserQuery();
1248add1155SRico Sonntag
1258add1155SRico Sonntag        return date($format, (int) $user->getPreference('reg_timestamp'));
1268add1155SRico Sonntag    }
1278add1155SRico Sonntag
1288add1155SRico Sonntag    /**
1298add1155SRico Sonntag     * @inheritDoc
1308add1155SRico Sonntag     */
1318add1155SRico Sonntag    public function latestUserLoggedin(string $yes = null, string $no = null): string
1328add1155SRico Sonntag    {
1338add1155SRico Sonntag        $yes  = $yes ?? I18N::translate('yes');
1348add1155SRico Sonntag        $no   = $no ?? I18N::translate('no');
1358add1155SRico Sonntag        $user = $this->latestUserQuery();
1368add1155SRico Sonntag
1378add1155SRico Sonntag        $is_logged_in = DB::table('session')
1388add1155SRico Sonntag            ->selectRaw('1')
1398add1155SRico Sonntag            ->where('user_id', '=', $user->id())
1408add1155SRico Sonntag            ->first();
1418add1155SRico Sonntag
1428add1155SRico Sonntag        return $is_logged_in ? $yes : $no;
1438add1155SRico Sonntag    }
1448add1155SRico Sonntag}
145