xref: /webtrees/app/Http/RequestHandlers/AccountEdit.php (revision c82bd01cdd2eabcbf00ea99e1cddc578ce91b455)
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 <http://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22use DateTimeZone;
23use Fisharebest\Webtrees\Auth;
24use Fisharebest\Webtrees\Contracts\UserInterface;
25use Fisharebest\Webtrees\Http\ViewResponseTrait;
26use Fisharebest\Webtrees\I18N;
27use Fisharebest\Webtrees\Module\ModuleLanguageInterface;
28use Fisharebest\Webtrees\Registry;
29use Fisharebest\Webtrees\Services\MessageService;
30use Fisharebest\Webtrees\Services\ModuleService;
31use Fisharebest\Webtrees\Tree;
32use Fisharebest\Webtrees\User;
33use Psr\Http\Message\ResponseInterface;
34use Psr\Http\Message\ServerRequestInterface;
35use Psr\Http\Server\RequestHandlerInterface;
36
37use function array_combine;
38
39/**
40 * Edit user account details.
41 */
42class AccountEdit implements RequestHandlerInterface
43{
44    use ViewResponseTrait;
45
46    /** @var MessageService */
47    private $message_service;
48
49    /** @var ModuleService */
50    private $module_service;
51
52    /**
53     * AccountEdit constructor.
54     *
55     * @param MessageService $message_service
56     * @param ModuleService  $module_service
57     */
58    public function __construct(MessageService $message_service, ModuleService $module_service)
59    {
60        $this->message_service = $message_service;
61        $this->module_service = $module_service;
62    }
63
64    /**
65     * @param ServerRequestInterface $request
66     *
67     * @return ResponseInterface
68     */
69    public function handle(ServerRequestInterface $request): ResponseInterface
70    {
71        $tree = $request->getAttribute('tree');
72
73        $user = $request->getAttribute('user');
74        assert($user instanceof User);
75
76        if ($tree instanceof Tree) {
77            $my_individual_record = Registry::individualFactory()->make($tree->getUserPreference(Auth::user(), UserInterface::PREF_TREE_ACCOUNT_XREF), $tree);
78            $default_individual   = Registry::individualFactory()->make($tree->getUserPreference(Auth::user(), UserInterface::PREF_TREE_DEFAULT_XREF), $tree);
79        } else {
80            $my_individual_record = null;
81            $default_individual   = null;
82        }
83
84        $languages = $this->module_service->findByInterface(ModuleLanguageInterface::class, true, true)
85            ->mapWithKeys(static function (ModuleLanguageInterface $module): array {
86                $locale = $module->locale();
87
88                return [$locale->languageTag() => $locale->endonym()];
89            });
90
91        $show_delete_option = $user->getPreference(UserInterface::PREF_IS_ADMINISTRATOR) !== '1';
92        $timezone_ids       = DateTimeZone::listIdentifiers();
93        $timezones          = array_combine($timezone_ids, $timezone_ids);
94        $title              = I18N::translate('My account');
95
96        return $this->viewResponse('edit-account-page', [
97            'contact_methods'      => $this->message_service->contactMethods(),
98            'default_individual'   => $default_individual,
99            'languages'            => $languages->all(),
100            'my_individual_record' => $my_individual_record,
101            'show_delete_option'   => $show_delete_option,
102            'timezones'            => $timezones,
103            'title'                => $title,
104            'tree'                 => $tree,
105            'user'                 => $user,
106        ]);
107    }
108}
109