xref: /webtrees/app/Http/RequestHandlers/UserEditPage.php (revision 24931b29a0237a5f5f1b8620af661ea530451af0)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 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 Fisharebest\Webtrees\Validator;
33use Psr\Http\Message\ResponseInterface;
34use Psr\Http\Message\ServerRequestInterface;
35use Psr\Http\Server\RequestHandlerInterface;
36
37/**
38 * Edit a user.
39 */
40class UserEditPage implements RequestHandlerInterface
41{
42    use ViewResponseTrait;
43
44    private MessageService $message_service;
45
46    private ModuleService $module_service;
47
48    private UserService $user_service;
49
50    private TreeService $tree_service;
51
52    /**
53     * @param MessageService $message_service
54     * @param ModuleService  $module_service
55     * @param TreeService    $tree_service
56     * @param UserService    $user_service
57     */
58    public function __construct(
59        MessageService $message_service,
60        ModuleService $module_service,
61        TreeService $tree_service,
62        UserService $user_service
63    ) {
64        $this->message_service = $message_service;
65        $this->module_service  = $module_service;
66        $this->tree_service    = $tree_service;
67        $this->user_service    = $user_service;
68    }
69
70    /**
71     * @param ServerRequestInterface $request
72     *
73     * @return ResponseInterface
74     */
75    public function handle(ServerRequestInterface $request): ResponseInterface
76    {
77        $this->layout = 'layouts/administration';
78
79        $user_id = Validator::queryParams($request)->integer('user_id');
80        $user    = $this->user_service->find($user_id);
81
82        if ($user === null) {
83            throw new HttpNotFoundException(I18N::translate('%s does not exist.', 'user_id:' . $user_id));
84        }
85
86        $languages = $this->module_service->findByInterface(ModuleLanguageInterface::class, true, true)
87            ->mapWithKeys(static function (ModuleLanguageInterface $module): array {
88                $locale = $module->locale();
89
90                return [$locale->languageTag() => $locale->endonym()];
91            });
92
93        $roles = [
94            /* I18N: Listbox entry; name of a role */
95            UserInterface::ROLE_VISITOR   => I18N::translate('Visitor'),
96            /* I18N: Listbox entry; name of a role */
97            UserInterface::ROLE_MEMBER    => I18N::translate('Member'),
98            /* I18N: Listbox entry; name of a role */
99            UserInterface::ROLE_EDITOR    => I18N::translate('Editor'),
100            /* I18N: Listbox entry; name of a role */
101            UserInterface::ROLE_MODERATOR => I18N::translate('Moderator'),
102            /* I18N: Listbox entry; name of a role */
103            UserInterface::ROLE_MANAGER   => I18N::translate('Manager'),
104        ];
105
106        $theme_options = $this->module_service
107            ->findByInterface(ModuleThemeInterface::class)
108            ->map($this->module_service->titleMapper())
109            ->prepend(I18N::translate('<default theme>'), '');
110
111        return $this->viewResponse('admin/users-edit', [
112            'contact_methods'  => $this->message_service->contactMethods(),
113            'default_language' => I18N::languageTag(),
114            'languages'        => $languages->all(),
115            'roles'            => $roles,
116            'trees'            => $this->tree_service->all(),
117            'theme_options'    => $theme_options,
118            'title'            => I18N::translate('Edit the user'),
119            'user'             => $user,
120        ]);
121    }
122}
123