xref: /webtrees/app/Http/RequestHandlers/UserListData.php (revision b55cbc6b43247e8b2ad14af6f6d24dc6747195ff)
14c3563c0SGreg Roach<?php
24c3563c0SGreg Roach
34c3563c0SGreg Roach/**
44c3563c0SGreg Roach * webtrees: online genealogy
54c3563c0SGreg Roach * Copyright (C) 2021 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;
234c3563c0SGreg Roachuse Fisharebest\Webtrees\I18N;
244c3563c0SGreg Roachuse Fisharebest\Webtrees\Module\ModuleLanguageInterface;
25d97083feSGreg Roachuse Fisharebest\Webtrees\Registry;
264c3563c0SGreg Roachuse Fisharebest\Webtrees\Services\DatatablesService;
274c3563c0SGreg Roachuse Fisharebest\Webtrees\Services\ModuleService;
284c3563c0SGreg Roachuse Fisharebest\Webtrees\Services\UserService;
29*b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
304c3563c0SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
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     * UserListData constructor.
514c3563c0SGreg Roach     *
524c3563c0SGreg Roach     * @param DatatablesService $datatables_service
534c3563c0SGreg Roach     * @param ModuleService     $module_service
544c3563c0SGreg Roach     * @param UserService       $user_service
554c3563c0SGreg Roach     */
564c3563c0SGreg Roach    public function __construct(
574c3563c0SGreg Roach        DatatablesService $datatables_service,
584c3563c0SGreg Roach        ModuleService $module_service,
594c3563c0SGreg Roach        UserService $user_service
604c3563c0SGreg Roach    ) {
614c3563c0SGreg Roach        $this->datatables_service = $datatables_service;
624c3563c0SGreg Roach        $this->module_service     = $module_service;
634c3563c0SGreg Roach        $this->user_service       = $user_service;
644c3563c0SGreg Roach    }
654c3563c0SGreg Roach
664c3563c0SGreg Roach    /**
674c3563c0SGreg Roach     * @param ServerRequestInterface $request
684c3563c0SGreg Roach     *
694c3563c0SGreg Roach     * @return ResponseInterface
704c3563c0SGreg Roach     */
714c3563c0SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
724c3563c0SGreg Roach    {
73*b55cbc6bSGreg Roach        $user = Validator::attributes($request)->user();
744c3563c0SGreg Roach
754c3563c0SGreg Roach        $languages = $this->module_service->findByInterface(ModuleLanguageInterface::class, true)
764c3563c0SGreg Roach            ->mapWithKeys(static function (ModuleLanguageInterface $module): array {
774c3563c0SGreg Roach                $locale = $module->locale();
784c3563c0SGreg Roach
794c3563c0SGreg Roach                return [$locale->languageTag() => $locale->endonym()];
804c3563c0SGreg Roach            });
814c3563c0SGreg Roach
824c3563c0SGreg Roach        $query = DB::table('user')
834c3563c0SGreg Roach            ->leftJoin('user_setting AS us1', static function (JoinClause $join): void {
844c3563c0SGreg Roach                $join
854c3563c0SGreg Roach                    ->on('us1.user_id', '=', 'user.user_id')
864c3563c0SGreg Roach                    ->where('us1.setting_name', '=', 'language');
874c3563c0SGreg Roach            })
884c3563c0SGreg Roach            ->leftJoin('user_setting AS us2', static function (JoinClause $join): void {
894c3563c0SGreg Roach                $join
904c3563c0SGreg Roach                    ->on('us2.user_id', '=', 'user.user_id')
914c3563c0SGreg Roach                    ->where('us2.setting_name', '=', UserInterface::PREF_TIMESTAMP_REGISTERED);
924c3563c0SGreg Roach            })
934c3563c0SGreg Roach            ->leftJoin('user_setting AS us3', static function (JoinClause $join): void {
944c3563c0SGreg Roach                $join
954c3563c0SGreg Roach                    ->on('us3.user_id', '=', 'user.user_id')
964c3563c0SGreg Roach                    ->where('us3.setting_name', '=', UserInterface::PREF_TIMESTAMP_ACTIVE);
974c3563c0SGreg Roach            })
984c3563c0SGreg Roach            ->leftJoin('user_setting AS us4', static function (JoinClause $join): void {
994c3563c0SGreg Roach                $join
1004c3563c0SGreg Roach                    ->on('us4.user_id', '=', 'user.user_id')
1014c3563c0SGreg Roach                    ->where('us4.setting_name', '=', UserInterface::PREF_IS_EMAIL_VERIFIED);
1024c3563c0SGreg Roach            })
1034c3563c0SGreg Roach            ->leftJoin('user_setting AS us5', static function (JoinClause $join): void {
1044c3563c0SGreg Roach                $join
1054c3563c0SGreg Roach                    ->on('us5.user_id', '=', 'user.user_id')
1064c3563c0SGreg Roach                    ->where('us5.setting_name', '=', UserInterface::PREF_IS_ACCOUNT_APPROVED);
1074c3563c0SGreg Roach            })
1084c3563c0SGreg Roach            ->where('user.user_id', '>', '0')
1094c3563c0SGreg Roach            ->select([
1104c3563c0SGreg Roach                'user.user_id AS edit_menu', // Hidden column
1114c3563c0SGreg Roach                'user.user_id',
1124c3563c0SGreg Roach                'user_name',
1134c3563c0SGreg Roach                'real_name',
1144c3563c0SGreg Roach                'email',
1154c3563c0SGreg Roach                'us1.setting_value AS language',
1164c3563c0SGreg Roach                'us2.setting_value AS registered_at_sort', // Hidden column
1174c3563c0SGreg Roach                'us2.setting_value AS registered_at',
1184c3563c0SGreg Roach                'us3.setting_value AS active_at_sort', // Hidden column
1194c3563c0SGreg Roach                'us3.setting_value AS active_at',
1204c3563c0SGreg Roach                'us4.setting_value AS verified',
1214c3563c0SGreg Roach                'us5.setting_value AS verified_by_admin',
1224c3563c0SGreg Roach            ]);
1234c3563c0SGreg Roach
1244c3563c0SGreg Roach        $search_columns = ['user_name', 'real_name', 'email'];
1254c3563c0SGreg Roach        $sort_columns   = [];
1264c3563c0SGreg Roach
127f70bcff5SGreg Roach        $callback = function (object $row) use ($languages, $user): array {
1284708b347SGreg Roach            $row_user = $this->user_service->find((int) $row->user_id);
1294c3563c0SGreg Roach            $datum = [
1304c3563c0SGreg Roach                view('admin/users-table-options', ['row' => $row, 'self' => $user, 'user' => $row_user]),
1314c3563c0SGreg Roach                $row->user_id,
132315eb316SGreg Roach                '<bdi>' . e($row->user_name) . '</bdi>',
133315eb316SGreg Roach                '<bdi>' . e($row->real_name) . '</bdi>',
1344c3563c0SGreg Roach                '<a href="mailto:' . e($row->email) . '">' . e($row->email) . '</a>',
1354c3563c0SGreg Roach                $languages->get($row->language, $row->language),
1364c3563c0SGreg Roach                $row->registered_at,
137d97083feSGreg Roach                $row->registered_at ? view('components/datetime-diff', ['timestamp' => Registry::timestampFactory()->make((int) $row->registered_at)]) : '',
1384c3563c0SGreg Roach                $row->active_at,
139d97083feSGreg Roach                $row->active_at ? view('components/datetime-diff', ['timestamp' => Registry::timestampFactory()->make((int) $row->active_at)]) : I18N::translate('Never'),
1404c3563c0SGreg Roach                $row->verified ? I18N::translate('yes') : I18N::translate('no'),
1414c3563c0SGreg Roach                $row->verified_by_admin ? I18N::translate('yes') : I18N::translate('no'),
1424c3563c0SGreg Roach            ];
1434c3563c0SGreg Roach
1444c3563c0SGreg Roach            // Highlight old registrations.
1454c3563c0SGreg Roach            if (!$datum[10] && date('U') - $datum[6] > 604800) {
1464c3563c0SGreg Roach                $datum[7] = '<span class="text-danger">' . $datum[7] . '</span>';
1474c3563c0SGreg Roach            }
1484c3563c0SGreg Roach
1494c3563c0SGreg Roach            return $datum;
1504c3563c0SGreg Roach        };
1514c3563c0SGreg Roach
1524c3563c0SGreg Roach        return $this->datatables_service->handleQuery($request, $query, $search_columns, $sort_columns, $callback);
1534c3563c0SGreg Roach    }
1544c3563c0SGreg Roach}
155