xref: /webtrees/app/Http/RequestHandlers/UserListData.php (revision 6f4ec3cadc983f0a7294108c634bef48846b4311)
14c3563c0SGreg Roach<?php
24c3563c0SGreg Roach
34c3563c0SGreg Roach/**
44c3563c0SGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
64c3563c0SGreg Roach * This program is free software: you can redistribute it and/or modify
74c3563c0SGreg Roach * it under the terms of the GNU General Public License as published by
84c3563c0SGreg Roach * the Free Software Foundation, either version 3 of the License, or
94c3563c0SGreg Roach * (at your option) any later version.
104c3563c0SGreg Roach * This program is distributed in the hope that it will be useful,
114c3563c0SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
124c3563c0SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
134c3563c0SGreg Roach * GNU General Public License for more details.
144c3563c0SGreg Roach * 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/>.
164c3563c0SGreg Roach */
174c3563c0SGreg Roach
184c3563c0SGreg Roachdeclare(strict_types=1);
194c3563c0SGreg Roach
204c3563c0SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
214c3563c0SGreg Roach
224c3563c0SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
23*6f4ec3caSGreg Roachuse Fisharebest\Webtrees\DB;
244c3563c0SGreg Roachuse Fisharebest\Webtrees\I18N;
254c3563c0SGreg Roachuse Fisharebest\Webtrees\Module\ModuleLanguageInterface;
26d97083feSGreg Roachuse Fisharebest\Webtrees\Registry;
274c3563c0SGreg Roachuse Fisharebest\Webtrees\Services\DatatablesService;
284c3563c0SGreg Roachuse Fisharebest\Webtrees\Services\ModuleService;
294c3563c0SGreg Roachuse Fisharebest\Webtrees\Services\UserService;
30b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
314c3563c0SGreg Roachuse Illuminate\Database\Query\JoinClause;
324c3563c0SGreg Roachuse Psr\Http\Message\ResponseInterface;
334c3563c0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
344c3563c0SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
354c3563c0SGreg Roach
364c3563c0SGreg Roachuse function e;
374c3563c0SGreg Roach
384c3563c0SGreg Roach/**
394c3563c0SGreg Roach * List of users.
404c3563c0SGreg Roach */
414c3563c0SGreg Roachclass UserListData implements RequestHandlerInterface
424c3563c0SGreg Roach{
43c4943cffSGreg Roach    private DatatablesService $datatables_service;
444c3563c0SGreg Roach
45c4943cffSGreg Roach    private ModuleService $module_service;
464c3563c0SGreg Roach
47c4943cffSGreg Roach    private UserService $user_service;
484c3563c0SGreg Roach
494c3563c0SGreg Roach    /**
504c3563c0SGreg Roach     * @param DatatablesService $datatables_service
514c3563c0SGreg Roach     * @param ModuleService     $module_service
524c3563c0SGreg Roach     * @param UserService       $user_service
534c3563c0SGreg Roach     */
544c3563c0SGreg Roach    public function __construct(
554c3563c0SGreg Roach        DatatablesService $datatables_service,
564c3563c0SGreg Roach        ModuleService $module_service,
574c3563c0SGreg Roach        UserService $user_service
584c3563c0SGreg Roach    ) {
594c3563c0SGreg Roach        $this->datatables_service = $datatables_service;
604c3563c0SGreg Roach        $this->module_service     = $module_service;
614c3563c0SGreg Roach        $this->user_service       = $user_service;
624c3563c0SGreg Roach    }
634c3563c0SGreg Roach
644c3563c0SGreg Roach    /**
654c3563c0SGreg Roach     * @param ServerRequestInterface $request
664c3563c0SGreg Roach     *
674c3563c0SGreg Roach     * @return ResponseInterface
684c3563c0SGreg Roach     */
694c3563c0SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
704c3563c0SGreg Roach    {
71b55cbc6bSGreg Roach        $user = Validator::attributes($request)->user();
724c3563c0SGreg Roach
734c3563c0SGreg Roach        $languages = $this->module_service->findByInterface(ModuleLanguageInterface::class, true)
744c3563c0SGreg Roach            ->mapWithKeys(static function (ModuleLanguageInterface $module): array {
754c3563c0SGreg Roach                $locale = $module->locale();
764c3563c0SGreg Roach
774c3563c0SGreg Roach                return [$locale->languageTag() => $locale->endonym()];
784c3563c0SGreg Roach            });
794c3563c0SGreg Roach
804c3563c0SGreg Roach        $query = DB::table('user')
814c3563c0SGreg Roach            ->leftJoin('user_setting AS us1', static function (JoinClause $join): void {
824c3563c0SGreg Roach                $join
834c3563c0SGreg Roach                    ->on('us1.user_id', '=', 'user.user_id')
844c3563c0SGreg Roach                    ->where('us1.setting_name', '=', 'language');
854c3563c0SGreg Roach            })
864c3563c0SGreg Roach            ->leftJoin('user_setting AS us2', static function (JoinClause $join): void {
874c3563c0SGreg Roach                $join
884c3563c0SGreg Roach                    ->on('us2.user_id', '=', 'user.user_id')
894c3563c0SGreg Roach                    ->where('us2.setting_name', '=', UserInterface::PREF_TIMESTAMP_REGISTERED);
904c3563c0SGreg Roach            })
914c3563c0SGreg Roach            ->leftJoin('user_setting AS us3', static function (JoinClause $join): void {
924c3563c0SGreg Roach                $join
934c3563c0SGreg Roach                    ->on('us3.user_id', '=', 'user.user_id')
944c3563c0SGreg Roach                    ->where('us3.setting_name', '=', UserInterface::PREF_TIMESTAMP_ACTIVE);
954c3563c0SGreg Roach            })
964c3563c0SGreg Roach            ->leftJoin('user_setting AS us4', static function (JoinClause $join): void {
974c3563c0SGreg Roach                $join
984c3563c0SGreg Roach                    ->on('us4.user_id', '=', 'user.user_id')
994c3563c0SGreg Roach                    ->where('us4.setting_name', '=', UserInterface::PREF_IS_EMAIL_VERIFIED);
1004c3563c0SGreg Roach            })
1014c3563c0SGreg Roach            ->leftJoin('user_setting AS us5', static function (JoinClause $join): void {
1024c3563c0SGreg Roach                $join
1034c3563c0SGreg Roach                    ->on('us5.user_id', '=', 'user.user_id')
1044c3563c0SGreg Roach                    ->where('us5.setting_name', '=', UserInterface::PREF_IS_ACCOUNT_APPROVED);
1054c3563c0SGreg Roach            })
1064c3563c0SGreg Roach            ->where('user.user_id', '>', '0')
1074c3563c0SGreg Roach            ->select([
1084c3563c0SGreg Roach                'user.user_id AS edit_menu', // Hidden column
1094c3563c0SGreg Roach                'user.user_id',
1104c3563c0SGreg Roach                'user_name',
1114c3563c0SGreg Roach                'real_name',
1124c3563c0SGreg Roach                'email',
1134c3563c0SGreg Roach                'us1.setting_value AS language',
1144c3563c0SGreg Roach                'us2.setting_value AS registered_at_sort', // Hidden column
1154c3563c0SGreg Roach                'us2.setting_value AS registered_at',
1164c3563c0SGreg Roach                'us3.setting_value AS active_at_sort', // Hidden column
1174c3563c0SGreg Roach                'us3.setting_value AS active_at',
1184c3563c0SGreg Roach                'us4.setting_value AS verified',
1194c3563c0SGreg Roach                'us5.setting_value AS verified_by_admin',
1204c3563c0SGreg Roach            ]);
1214c3563c0SGreg Roach
1224c3563c0SGreg Roach        $search_columns = ['user_name', 'real_name', 'email'];
1234c3563c0SGreg Roach        $sort_columns   = [];
1244c3563c0SGreg Roach
125f70bcff5SGreg Roach        $callback = function (object $row) use ($languages, $user): array {
1264708b347SGreg Roach            $row_user = $this->user_service->find((int) $row->user_id);
1274c3563c0SGreg Roach            $datum = [
1284c3563c0SGreg Roach                view('admin/users-table-options', ['row' => $row, 'self' => $user, 'user' => $row_user]),
1294c3563c0SGreg Roach                $row->user_id,
130315eb316SGreg Roach                '<bdi>' . e($row->user_name) . '</bdi>',
131315eb316SGreg Roach                '<bdi>' . e($row->real_name) . '</bdi>',
1324c3563c0SGreg Roach                '<a href="mailto:' . e($row->email) . '">' . e($row->email) . '</a>',
1334c3563c0SGreg Roach                $languages->get($row->language, $row->language),
1344c3563c0SGreg Roach                $row->registered_at,
135d97083feSGreg Roach                $row->registered_at ? view('components/datetime-diff', ['timestamp' => Registry::timestampFactory()->make((int) $row->registered_at)]) : '',
1364c3563c0SGreg Roach                $row->active_at,
137d97083feSGreg Roach                $row->active_at ? view('components/datetime-diff', ['timestamp' => Registry::timestampFactory()->make((int) $row->active_at)]) : I18N::translate('Never'),
1384c3563c0SGreg Roach                $row->verified ? I18N::translate('yes') : I18N::translate('no'),
1394c3563c0SGreg Roach                $row->verified_by_admin ? I18N::translate('yes') : I18N::translate('no'),
1404c3563c0SGreg Roach            ];
1414c3563c0SGreg Roach
1424c3563c0SGreg Roach            // Highlight old registrations.
1434c3563c0SGreg Roach            if (!$datum[10] && date('U') - $datum[6] > 604800) {
1444c3563c0SGreg Roach                $datum[7] = '<span class="text-danger">' . $datum[7] . '</span>';
1454c3563c0SGreg Roach            }
1464c3563c0SGreg Roach
1474c3563c0SGreg Roach            return $datum;
1484c3563c0SGreg Roach        };
1494c3563c0SGreg Roach
1504c3563c0SGreg Roach        return $this->datatables_service->handleQuery($request, $query, $search_columns, $sort_columns, $callback);
1514c3563c0SGreg Roach    }
1524c3563c0SGreg Roach}
153