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