xref: /webtrees/app/Http/RequestHandlers/AccountEdit.php (revision 1fe542e96f8f7eedeebc278fae1e0ab0d9e74d95)
1a49d0e3fSGreg Roach<?php
2a49d0e3fSGreg Roach
3a49d0e3fSGreg Roach/**
4a49d0e3fSGreg Roach * webtrees: online genealogy
5*1fe542e9SGreg Roach * Copyright (C) 2021 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;
24*1fe542e9SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
25a49d0e3fSGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
26a49d0e3fSGreg Roachuse Fisharebest\Webtrees\I18N;
27150f35adSGreg Roachuse Fisharebest\Webtrees\Module\ModuleLanguageInterface;
286b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
29f91b18ebSGreg Roachuse Fisharebest\Webtrees\Services\MessageService;
30150f35adSGreg Roachuse Fisharebest\Webtrees\Services\ModuleService;
31a49d0e3fSGreg Roachuse Fisharebest\Webtrees\Tree;
327c4add84SGreg Roachuse Fisharebest\Webtrees\User;
33a49d0e3fSGreg Roachuse Psr\Http\Message\ResponseInterface;
34a49d0e3fSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
35a49d0e3fSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
36a49d0e3fSGreg Roach
37a49d0e3fSGreg Roachuse function array_combine;
38a49d0e3fSGreg Roach
39a49d0e3fSGreg Roach/**
40a49d0e3fSGreg Roach * Edit user account details.
41a49d0e3fSGreg Roach */
42a49d0e3fSGreg Roachclass AccountEdit implements RequestHandlerInterface
43a49d0e3fSGreg Roach{
44a49d0e3fSGreg Roach    use ViewResponseTrait;
45a49d0e3fSGreg Roach
46f91b18ebSGreg Roach    /** @var MessageService */
47f91b18ebSGreg Roach    private $message_service;
48f91b18ebSGreg Roach
49150f35adSGreg Roach    /** @var ModuleService */
50150f35adSGreg Roach    private $module_service;
51150f35adSGreg Roach
52150f35adSGreg Roach    /**
53150f35adSGreg Roach     * AccountEdit constructor.
54150f35adSGreg Roach     *
55f91b18ebSGreg Roach     * @param MessageService $message_service
56150f35adSGreg Roach     * @param ModuleService  $module_service
57150f35adSGreg Roach     */
58f91b18ebSGreg Roach    public function __construct(MessageService $message_service, ModuleService $module_service)
59150f35adSGreg Roach    {
60f91b18ebSGreg Roach        $this->message_service = $message_service;
61150f35adSGreg Roach        $this->module_service = $module_service;
62150f35adSGreg Roach    }
63150f35adSGreg Roach
64a49d0e3fSGreg Roach    /**
65a49d0e3fSGreg Roach     * @param ServerRequestInterface $request
66a49d0e3fSGreg Roach     *
67a49d0e3fSGreg Roach     * @return ResponseInterface
68a49d0e3fSGreg Roach     */
69a49d0e3fSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
70a49d0e3fSGreg Roach    {
71a49d0e3fSGreg Roach        $tree = $request->getAttribute('tree');
727c4add84SGreg Roach
73a49d0e3fSGreg Roach        $user = $request->getAttribute('user');
747c4add84SGreg Roach        assert($user instanceof User);
75a49d0e3fSGreg Roach
76a49d0e3fSGreg Roach        if ($tree instanceof Tree) {
77*1fe542e9SGreg Roach            $my_individual_record = Registry::individualFactory()->make($tree->getUserPreference(Auth::user(), UserInterface::PREF_TREE_ACCOUNT_XREF), $tree);
78*1fe542e9SGreg Roach            $default_individual   = Registry::individualFactory()->make($tree->getUserPreference(Auth::user(), UserInterface::PREF_TREE_DEFAULT_XREF), $tree);
79a49d0e3fSGreg Roach        } else {
80a49d0e3fSGreg Roach            $my_individual_record = null;
81a49d0e3fSGreg Roach            $default_individual   = null;
82a49d0e3fSGreg Roach        }
83a49d0e3fSGreg Roach
84150f35adSGreg Roach        $languages = $this->module_service->findByInterface(ModuleLanguageInterface::class, true, true)
85150f35adSGreg Roach            ->mapWithKeys(static function (ModuleLanguageInterface $module): array {
86150f35adSGreg Roach                $locale = $module->locale();
87150f35adSGreg Roach
88150f35adSGreg Roach                return [$locale->languageTag() => $locale->endonym()];
89150f35adSGreg Roach            });
90150f35adSGreg Roach
91*1fe542e9SGreg Roach        $show_delete_option = $user->getPreference(UserInterface::PREF_IS_ADMINISTRATOR) !== '1';
92a49d0e3fSGreg Roach        $timezone_ids       = DateTimeZone::listIdentifiers();
93a49d0e3fSGreg Roach        $timezones          = array_combine($timezone_ids, $timezone_ids);
94a49d0e3fSGreg Roach        $title              = I18N::translate('My account');
95a49d0e3fSGreg Roach
96a49d0e3fSGreg Roach        return $this->viewResponse('edit-account-page', [
97f91b18ebSGreg Roach            'contact_methods'      => $this->message_service->contactMethods(),
98a49d0e3fSGreg Roach            'default_individual'   => $default_individual,
99150f35adSGreg Roach            'languages'            => $languages->all(),
100a49d0e3fSGreg Roach            'my_individual_record' => $my_individual_record,
101a49d0e3fSGreg Roach            'show_delete_option'   => $show_delete_option,
102a49d0e3fSGreg Roach            'timezones'            => $timezones,
103a49d0e3fSGreg Roach            'title'                => $title,
104a49d0e3fSGreg Roach            'tree'                 => $tree,
105a49d0e3fSGreg Roach            'user'                 => $user,
106a49d0e3fSGreg Roach        ]);
107a49d0e3fSGreg Roach    }
108a49d0e3fSGreg Roach}
109