xref: /webtrees/app/Http/RequestHandlers/AccountEdit.php (revision 7c4add84379afdbaa7c4c272763673edc20fb830)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 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\Functions\FunctionsEdit;
25use Fisharebest\Webtrees\Http\ViewResponseTrait;
26use Fisharebest\Webtrees\I18N;
27use Fisharebest\Webtrees\Individual;
28use Fisharebest\Webtrees\Tree;
29use Fisharebest\Webtrees\User;
30use Psr\Http\Message\ResponseInterface;
31use Psr\Http\Message\ServerRequestInterface;
32use Psr\Http\Server\RequestHandlerInterface;
33
34use function array_combine;
35
36/**
37 * Edit user account details.
38 */
39class AccountEdit implements RequestHandlerInterface
40{
41    use ViewResponseTrait;
42
43    /**
44     * @param ServerRequestInterface $request
45     *
46     * @return ResponseInterface
47     */
48    public function handle(ServerRequestInterface $request): ResponseInterface
49    {
50        $tree = $request->getAttribute('tree');
51
52        $user = $request->getAttribute('user');
53        assert($user instanceof User);
54
55        if ($tree instanceof Tree) {
56            $my_individual_record = Individual::getInstance($tree->getUserPreference(Auth::user(), User::PREF_TREE_ACCOUNT_XREF), $tree);
57            $default_individual   = Individual::getInstance($tree->getUserPreference(Auth::user(), User::PREF_TREE_DEFAULT_XREF), $tree);
58        } else {
59            $my_individual_record = null;
60            $default_individual   = null;
61        }
62
63        $show_delete_option = $user->getPreference(User::PREF_IS_ADMINISTRATOR) !== '1';
64        $timezone_ids       = DateTimeZone::listIdentifiers();
65        $timezones          = array_combine($timezone_ids, $timezone_ids);
66        $title              = I18N::translate('My account');
67
68        return $this->viewResponse('edit-account-page', [
69            'contact_methods'      => FunctionsEdit::optionsContactMethods(),
70            'default_individual'   => $default_individual,
71            'installed_languages'  => $this->installedLanguages(),
72            'my_individual_record' => $my_individual_record,
73            'show_delete_option'   => $show_delete_option,
74            'timezones'            => $timezones,
75            'title'                => $title,
76            'tree'                 => $tree,
77            'user'                 => $user,
78        ]);
79    }
80
81    /**
82     * A list of installed languages (e.g. for an edit control).
83     *
84     * @return string[]
85     */
86    private function installedLanguages(): array
87    {
88        $languages = [];
89        foreach (I18N::installedLocales() as $locale) {
90            $languages[$locale->languageTag()] = $locale->endonym();
91        }
92
93        return $languages;
94    }
95}
96