xref: /webtrees/app/Http/RequestHandlers/AccountEdit.php (revision 7c4add84379afdbaa7c4c272763673edc20fb830)
1a49d0e3fSGreg Roach<?php
2a49d0e3fSGreg Roach
3a49d0e3fSGreg Roach/**
4a49d0e3fSGreg Roach * webtrees: online genealogy
5a49d0e3fSGreg Roach * Copyright (C) 2019 webtrees development team
6a49d0e3fSGreg Roach * This program is free software: you can redistribute it and/or modify
7a49d0e3fSGreg Roach * it under the terms of the GNU General Public License as published by
8a49d0e3fSGreg Roach * the Free Software Foundation, either version 3 of the License, or
9a49d0e3fSGreg Roach * (at your option) any later version.
10a49d0e3fSGreg Roach * This program is distributed in the hope that it will be useful,
11a49d0e3fSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12a49d0e3fSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13a49d0e3fSGreg Roach * GNU General Public License for more details.
14a49d0e3fSGreg Roach * You should have received a copy of the GNU General Public License
15a49d0e3fSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
16a49d0e3fSGreg Roach */
17a49d0e3fSGreg Roach
18a49d0e3fSGreg Roachdeclare(strict_types=1);
19a49d0e3fSGreg Roach
20a49d0e3fSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
21a49d0e3fSGreg Roach
22a49d0e3fSGreg Roachuse DateTimeZone;
23a49d0e3fSGreg Roachuse Fisharebest\Webtrees\Auth;
24a49d0e3fSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsEdit;
25a49d0e3fSGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
26a49d0e3fSGreg Roachuse Fisharebest\Webtrees\I18N;
27a49d0e3fSGreg Roachuse Fisharebest\Webtrees\Individual;
28a49d0e3fSGreg Roachuse Fisharebest\Webtrees\Tree;
29*7c4add84SGreg Roachuse Fisharebest\Webtrees\User;
30a49d0e3fSGreg Roachuse Psr\Http\Message\ResponseInterface;
31a49d0e3fSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
32a49d0e3fSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
33a49d0e3fSGreg Roach
34a49d0e3fSGreg Roachuse function array_combine;
35a49d0e3fSGreg Roach
36a49d0e3fSGreg Roach/**
37a49d0e3fSGreg Roach * Edit user account details.
38a49d0e3fSGreg Roach */
39a49d0e3fSGreg Roachclass AccountEdit implements RequestHandlerInterface
40a49d0e3fSGreg Roach{
41a49d0e3fSGreg Roach    use ViewResponseTrait;
42a49d0e3fSGreg Roach
43a49d0e3fSGreg Roach    /**
44a49d0e3fSGreg Roach     * @param ServerRequestInterface $request
45a49d0e3fSGreg Roach     *
46a49d0e3fSGreg Roach     * @return ResponseInterface
47a49d0e3fSGreg Roach     */
48a49d0e3fSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
49a49d0e3fSGreg Roach    {
50a49d0e3fSGreg Roach        $tree = $request->getAttribute('tree');
51*7c4add84SGreg Roach
52a49d0e3fSGreg Roach        $user = $request->getAttribute('user');
53*7c4add84SGreg Roach        assert($user instanceof User);
54a49d0e3fSGreg Roach
55a49d0e3fSGreg Roach        if ($tree instanceof Tree) {
56*7c4add84SGreg Roach            $my_individual_record = Individual::getInstance($tree->getUserPreference(Auth::user(), User::PREF_TREE_ACCOUNT_XREF), $tree);
57*7c4add84SGreg Roach            $default_individual   = Individual::getInstance($tree->getUserPreference(Auth::user(), User::PREF_TREE_DEFAULT_XREF), $tree);
58a49d0e3fSGreg Roach        } else {
59a49d0e3fSGreg Roach            $my_individual_record = null;
60a49d0e3fSGreg Roach            $default_individual   = null;
61a49d0e3fSGreg Roach        }
62a49d0e3fSGreg Roach
63*7c4add84SGreg Roach        $show_delete_option = $user->getPreference(User::PREF_IS_ADMINISTRATOR) !== '1';
64a49d0e3fSGreg Roach        $timezone_ids       = DateTimeZone::listIdentifiers();
65a49d0e3fSGreg Roach        $timezones          = array_combine($timezone_ids, $timezone_ids);
66a49d0e3fSGreg Roach        $title              = I18N::translate('My account');
67a49d0e3fSGreg Roach
68a49d0e3fSGreg Roach        return $this->viewResponse('edit-account-page', [
69a49d0e3fSGreg Roach            'contact_methods'      => FunctionsEdit::optionsContactMethods(),
70a49d0e3fSGreg Roach            'default_individual'   => $default_individual,
71a49d0e3fSGreg Roach            'installed_languages'  => $this->installedLanguages(),
72a49d0e3fSGreg Roach            'my_individual_record' => $my_individual_record,
73a49d0e3fSGreg Roach            'show_delete_option'   => $show_delete_option,
74a49d0e3fSGreg Roach            'timezones'            => $timezones,
75a49d0e3fSGreg Roach            'title'                => $title,
76a49d0e3fSGreg Roach            'tree'                 => $tree,
77a49d0e3fSGreg Roach            'user'                 => $user,
78a49d0e3fSGreg Roach        ]);
79a49d0e3fSGreg Roach    }
80a49d0e3fSGreg Roach
81a49d0e3fSGreg Roach    /**
82a49d0e3fSGreg Roach     * A list of installed languages (e.g. for an edit control).
83a49d0e3fSGreg Roach     *
84a49d0e3fSGreg Roach     * @return string[]
85a49d0e3fSGreg Roach     */
86a49d0e3fSGreg Roach    private function installedLanguages(): array
87a49d0e3fSGreg Roach    {
88a49d0e3fSGreg Roach        $languages = [];
89a49d0e3fSGreg Roach        foreach (I18N::installedLocales() as $locale) {
90a49d0e3fSGreg Roach            $languages[$locale->languageTag()] = $locale->endonym();
91a49d0e3fSGreg Roach        }
92a49d0e3fSGreg Roach
93a49d0e3fSGreg Roach        return $languages;
94a49d0e3fSGreg Roach    }
95a49d0e3fSGreg Roach}
96