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