xref: /webtrees/app/Http/RequestHandlers/UserEditPage.php (revision 5bfc689774bb9a6401271c4ed15a6d50652c991b)
14c3563c0SGreg Roach<?php
24c3563c0SGreg Roach
34c3563c0SGreg Roach/**
44c3563c0SGreg Roach * webtrees: online genealogy
5*5bfc6897SGreg Roach * Copyright (C) 2022 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;
2381b729d3SGreg Roachuse Fisharebest\Webtrees\Http\Exceptions\HttpNotFoundException;
244c3563c0SGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
254c3563c0SGreg Roachuse Fisharebest\Webtrees\I18N;
264c3563c0SGreg Roachuse Fisharebest\Webtrees\Module\ModuleLanguageInterface;
274c3563c0SGreg Roachuse Fisharebest\Webtrees\Module\ModuleThemeInterface;
284c3563c0SGreg Roachuse Fisharebest\Webtrees\Services\MessageService;
294c3563c0SGreg Roachuse Fisharebest\Webtrees\Services\ModuleService;
304c3563c0SGreg Roachuse Fisharebest\Webtrees\Services\TreeService;
314c3563c0SGreg Roachuse Fisharebest\Webtrees\Services\UserService;
324c3563c0SGreg Roachuse Psr\Http\Message\ResponseInterface;
334c3563c0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
344c3563c0SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
354c3563c0SGreg Roach
364c3563c0SGreg Roach/**
374c3563c0SGreg Roach * Edit a user.
384c3563c0SGreg Roach */
394c3563c0SGreg Roachclass UserEditPage implements RequestHandlerInterface
404c3563c0SGreg Roach{
414c3563c0SGreg Roach    use ViewResponseTrait;
424c3563c0SGreg Roach
43c4943cffSGreg Roach    private MessageService $message_service;
444c3563c0SGreg Roach
45c4943cffSGreg Roach    private ModuleService $module_service;
464c3563c0SGreg Roach
47c4943cffSGreg Roach    private UserService $user_service;
484c3563c0SGreg Roach
49c4943cffSGreg Roach    private TreeService $tree_service;
504c3563c0SGreg Roach
514c3563c0SGreg Roach    /**
524c3563c0SGreg Roach     * UserEditPage constructor.
534c3563c0SGreg Roach     *
544c3563c0SGreg Roach     * @param MessageService $message_service
554c3563c0SGreg Roach     * @param ModuleService  $module_service
564c3563c0SGreg Roach     * @param TreeService    $tree_service
574c3563c0SGreg Roach     * @param UserService    $user_service
584c3563c0SGreg Roach     */
594c3563c0SGreg Roach    public function __construct(
604c3563c0SGreg Roach        MessageService $message_service,
614c3563c0SGreg Roach        ModuleService $module_service,
624c3563c0SGreg Roach        TreeService $tree_service,
634c3563c0SGreg Roach        UserService $user_service
644c3563c0SGreg Roach    ) {
654c3563c0SGreg Roach        $this->message_service = $message_service;
664c3563c0SGreg Roach        $this->module_service  = $module_service;
674c3563c0SGreg Roach        $this->tree_service    = $tree_service;
684c3563c0SGreg Roach        $this->user_service    = $user_service;
694c3563c0SGreg Roach    }
704c3563c0SGreg Roach
714c3563c0SGreg Roach    /**
724c3563c0SGreg Roach     * @param ServerRequestInterface $request
734c3563c0SGreg Roach     *
744c3563c0SGreg Roach     * @return ResponseInterface
754c3563c0SGreg Roach     */
764c3563c0SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
774c3563c0SGreg Roach    {
784c3563c0SGreg Roach        $this->layout = 'layouts/administration';
794c3563c0SGreg Roach
804c3563c0SGreg Roach        $user_id = (int) $request->getQueryParams()['user_id'];
814c3563c0SGreg Roach        $user    = $this->user_service->find($user_id);
824c3563c0SGreg Roach
834c3563c0SGreg Roach        if ($user === null) {
844566681eSGreg Roach            throw new HttpNotFoundException(I18N::translate('%s does not exist.', 'user_id:' . $user_id));
854c3563c0SGreg Roach        }
864c3563c0SGreg Roach
874c3563c0SGreg Roach        $languages = $this->module_service->findByInterface(ModuleLanguageInterface::class, true, true)
884c3563c0SGreg Roach            ->mapWithKeys(static function (ModuleLanguageInterface $module): array {
894c3563c0SGreg Roach                $locale = $module->locale();
904c3563c0SGreg Roach
914c3563c0SGreg Roach                return [$locale->languageTag() => $locale->endonym()];
924c3563c0SGreg Roach            });
934c3563c0SGreg Roach
944c3563c0SGreg Roach        $roles = [
954c3563c0SGreg Roach            /* I18N: Listbox entry; name of a role */
964c3563c0SGreg Roach            UserInterface::ROLE_VISITOR   => I18N::translate('Visitor'),
974c3563c0SGreg Roach            /* I18N: Listbox entry; name of a role */
984c3563c0SGreg Roach            UserInterface::ROLE_MEMBER    => I18N::translate('Member'),
994c3563c0SGreg Roach            /* I18N: Listbox entry; name of a role */
1004c3563c0SGreg Roach            UserInterface::ROLE_EDITOR    => I18N::translate('Editor'),
1014c3563c0SGreg Roach            /* I18N: Listbox entry; name of a role */
1024c3563c0SGreg Roach            UserInterface::ROLE_MODERATOR => I18N::translate('Moderator'),
1034c3563c0SGreg Roach            /* I18N: Listbox entry; name of a role */
1044c3563c0SGreg Roach            UserInterface::ROLE_MANAGER   => I18N::translate('Manager'),
1054c3563c0SGreg Roach        ];
1064c3563c0SGreg Roach
1074c3563c0SGreg Roach        $theme_options = $this->module_service
1084c3563c0SGreg Roach            ->findByInterface(ModuleThemeInterface::class)
1094c3563c0SGreg Roach            ->map($this->module_service->titleMapper())
1104c3563c0SGreg Roach            ->prepend(I18N::translate('<default theme>'), '');
1114c3563c0SGreg Roach
1124c3563c0SGreg Roach        return $this->viewResponse('admin/users-edit', [
1134c3563c0SGreg Roach            'contact_methods'  => $this->message_service->contactMethods(),
1144c3563c0SGreg Roach            'default_language' => I18N::languageTag(),
1154c3563c0SGreg Roach            'languages'        => $languages->all(),
1164c3563c0SGreg Roach            'roles'            => $roles,
1174c3563c0SGreg Roach            'trees'            => $this->tree_service->all(),
1184c3563c0SGreg Roach            'theme_options'    => $theme_options,
1194c3563c0SGreg Roach            'title'            => I18N::translate('Edit the user'),
1204c3563c0SGreg Roach            'user'             => $user,
1214c3563c0SGreg Roach        ]);
1224c3563c0SGreg Roach    }
1234c3563c0SGreg Roach}
124