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