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