xref: /webtrees/app/Http/RequestHandlers/UserEditPage.php (revision ef881450b5a90d00aca3ca9b49dedfa04cfc36f7)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22use Fisharebest\Webtrees\Contracts\UserInterface;
23use Fisharebest\Webtrees\Http\Exceptions\HttpNotFoundException;
24use Fisharebest\Webtrees\Http\ViewResponseTrait;
25use Fisharebest\Webtrees\I18N;
26use Fisharebest\Webtrees\Module\ModuleLanguageInterface;
27use Fisharebest\Webtrees\Module\ModuleThemeInterface;
28use Fisharebest\Webtrees\Services\MessageService;
29use Fisharebest\Webtrees\Services\ModuleService;
30use Fisharebest\Webtrees\Services\TreeService;
31use Fisharebest\Webtrees\Services\UserService;
32use Psr\Http\Message\ResponseInterface;
33use Psr\Http\Message\ServerRequestInterface;
34use Psr\Http\Server\RequestHandlerInterface;
35
36/**
37 * Edit a user.
38 */
39class UserEditPage implements RequestHandlerInterface
40{
41    use ViewResponseTrait;
42
43    private MessageService $message_service;
44
45    private ModuleService $module_service;
46
47    private UserService $user_service;
48
49    private TreeService $tree_service;
50
51    /**
52     * UserEditPage constructor.
53     *
54     * @param MessageService $message_service
55     * @param ModuleService  $module_service
56     * @param TreeService    $tree_service
57     * @param UserService    $user_service
58     */
59    public function __construct(
60        MessageService $message_service,
61        ModuleService $module_service,
62        TreeService $tree_service,
63        UserService $user_service
64    ) {
65        $this->message_service = $message_service;
66        $this->module_service  = $module_service;
67        $this->tree_service    = $tree_service;
68        $this->user_service    = $user_service;
69    }
70
71    /**
72     * @param ServerRequestInterface $request
73     *
74     * @return ResponseInterface
75     */
76    public function handle(ServerRequestInterface $request): ResponseInterface
77    {
78        $this->layout = 'layouts/administration';
79
80        $user_id = (int) $request->getQueryParams()['user_id'];
81        $user    = $this->user_service->find($user_id);
82
83        if ($user === null) {
84            throw new HttpNotFoundException(I18N::translate('%1$s does not exist.', 'user_id:' . $user_id));
85        }
86
87        $languages = $this->module_service->findByInterface(ModuleLanguageInterface::class, true, true)
88            ->mapWithKeys(static function (ModuleLanguageInterface $module): array {
89                $locale = $module->locale();
90
91                return [$locale->languageTag() => $locale->endonym()];
92            });
93
94        $roles = [
95            /* I18N: Listbox entry; name of a role */
96            UserInterface::ROLE_VISITOR   => I18N::translate('Visitor'),
97            /* I18N: Listbox entry; name of a role */
98            UserInterface::ROLE_MEMBER    => I18N::translate('Member'),
99            /* I18N: Listbox entry; name of a role */
100            UserInterface::ROLE_EDITOR    => I18N::translate('Editor'),
101            /* I18N: Listbox entry; name of a role */
102            UserInterface::ROLE_MODERATOR => I18N::translate('Moderator'),
103            /* I18N: Listbox entry; name of a role */
104            UserInterface::ROLE_MANAGER   => I18N::translate('Manager'),
105        ];
106
107        $theme_options = $this->module_service
108            ->findByInterface(ModuleThemeInterface::class)
109            ->map($this->module_service->titleMapper())
110            ->prepend(I18N::translate('<default theme>'), '');
111
112        return $this->viewResponse('admin/users-edit', [
113            'contact_methods'  => $this->message_service->contactMethods(),
114            'default_language' => I18N::languageTag(),
115            'languages'        => $languages->all(),
116            'roles'            => $roles,
117            'trees'            => $this->tree_service->all(),
118            'theme_options'    => $theme_options,
119            'title'            => I18N::translate('Edit the user'),
120            'user'             => $user,
121        ]);
122    }
123}
124