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