xref: /webtrees/app/Http/RequestHandlers/AccountEdit.php (revision 150f35ad305455c4048b1a46fd58875614272023)
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;
28*150f35adSGreg Roachuse Fisharebest\Webtrees\Module\ModuleLanguageInterface;
29*150f35adSGreg Roachuse Fisharebest\Webtrees\Services\ModuleService;
30a49d0e3fSGreg Roachuse Fisharebest\Webtrees\Tree;
317c4add84SGreg Roachuse Fisharebest\Webtrees\User;
32a49d0e3fSGreg Roachuse Psr\Http\Message\ResponseInterface;
33a49d0e3fSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
34a49d0e3fSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
35a49d0e3fSGreg Roach
36a49d0e3fSGreg Roachuse function array_combine;
37a49d0e3fSGreg Roach
38a49d0e3fSGreg Roach/**
39a49d0e3fSGreg Roach * Edit user account details.
40a49d0e3fSGreg Roach */
41a49d0e3fSGreg Roachclass AccountEdit implements RequestHandlerInterface
42a49d0e3fSGreg Roach{
43a49d0e3fSGreg Roach    use ViewResponseTrait;
44a49d0e3fSGreg Roach
45*150f35adSGreg Roach    /** @var ModuleService */
46*150f35adSGreg Roach    private $module_service;
47*150f35adSGreg Roach
48*150f35adSGreg Roach    /**
49*150f35adSGreg Roach     * AccountEdit constructor.
50*150f35adSGreg Roach     *
51*150f35adSGreg Roach     * @param ModuleService $module_service
52*150f35adSGreg Roach     */
53*150f35adSGreg Roach    public function __construct(ModuleService $module_service)
54*150f35adSGreg Roach    {
55*150f35adSGreg Roach        $this->module_service = $module_service;
56*150f35adSGreg Roach    }
57*150f35adSGreg Roach
58a49d0e3fSGreg Roach    /**
59a49d0e3fSGreg Roach     * @param ServerRequestInterface $request
60a49d0e3fSGreg Roach     *
61a49d0e3fSGreg Roach     * @return ResponseInterface
62a49d0e3fSGreg Roach     */
63a49d0e3fSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
64a49d0e3fSGreg Roach    {
65a49d0e3fSGreg Roach        $tree = $request->getAttribute('tree');
667c4add84SGreg Roach
67a49d0e3fSGreg Roach        $user = $request->getAttribute('user');
687c4add84SGreg Roach        assert($user instanceof User);
69a49d0e3fSGreg Roach
70a49d0e3fSGreg Roach        if ($tree instanceof Tree) {
717c4add84SGreg Roach            $my_individual_record = Individual::getInstance($tree->getUserPreference(Auth::user(), User::PREF_TREE_ACCOUNT_XREF), $tree);
727c4add84SGreg Roach            $default_individual   = Individual::getInstance($tree->getUserPreference(Auth::user(), User::PREF_TREE_DEFAULT_XREF), $tree);
73a49d0e3fSGreg Roach        } else {
74a49d0e3fSGreg Roach            $my_individual_record = null;
75a49d0e3fSGreg Roach            $default_individual   = null;
76a49d0e3fSGreg Roach        }
77a49d0e3fSGreg Roach
78*150f35adSGreg Roach        $languages = $this->module_service->findByInterface(ModuleLanguageInterface::class, true, true)
79*150f35adSGreg Roach            ->mapWithKeys(static function (ModuleLanguageInterface $module): array {
80*150f35adSGreg Roach                $locale = $module->locale();
81*150f35adSGreg Roach
82*150f35adSGreg Roach                return [$locale->languageTag() => $locale->endonym()];
83*150f35adSGreg Roach            });
84*150f35adSGreg Roach
857c4add84SGreg Roach        $show_delete_option = $user->getPreference(User::PREF_IS_ADMINISTRATOR) !== '1';
86a49d0e3fSGreg Roach        $timezone_ids       = DateTimeZone::listIdentifiers();
87a49d0e3fSGreg Roach        $timezones          = array_combine($timezone_ids, $timezone_ids);
88a49d0e3fSGreg Roach        $title              = I18N::translate('My account');
89a49d0e3fSGreg Roach
90a49d0e3fSGreg Roach        return $this->viewResponse('edit-account-page', [
91a49d0e3fSGreg Roach            'contact_methods'      => FunctionsEdit::optionsContactMethods(),
92a49d0e3fSGreg Roach            'default_individual'   => $default_individual,
93*150f35adSGreg Roach            'languages'            => $languages->all(),
94a49d0e3fSGreg Roach            'my_individual_record' => $my_individual_record,
95a49d0e3fSGreg Roach            'show_delete_option'   => $show_delete_option,
96a49d0e3fSGreg Roach            'timezones'            => $timezones,
97a49d0e3fSGreg Roach            'title'                => $title,
98a49d0e3fSGreg Roach            'tree'                 => $tree,
99a49d0e3fSGreg Roach            'user'                 => $user,
100a49d0e3fSGreg Roach        ]);
101a49d0e3fSGreg Roach    }
102a49d0e3fSGreg Roach}
103