xref: /webtrees/app/Http/RequestHandlers/AccountEdit.php (revision b55cbc6b43247e8b2ad14af6f6d24dc6747195ff)
1a49d0e3fSGreg Roach<?php
2a49d0e3fSGreg Roach
3a49d0e3fSGreg Roach/**
4a49d0e3fSGreg Roach * webtrees: online genealogy
51fe542e9SGreg 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
1589f7189bSGreg Roach * along with this program. If not, see <https://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;
241fe542e9SGreg 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;
32*b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
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
46c4943cffSGreg Roach    private MessageService $message_service;
47f91b18ebSGreg Roach
48c4943cffSGreg Roach    private ModuleService $module_service;
49150f35adSGreg Roach
50150f35adSGreg Roach    /**
51150f35adSGreg Roach     * AccountEdit constructor.
52150f35adSGreg Roach     *
53f91b18ebSGreg Roach     * @param MessageService $message_service
54150f35adSGreg Roach     * @param ModuleService  $module_service
55150f35adSGreg Roach     */
56f91b18ebSGreg Roach    public function __construct(MessageService $message_service, ModuleService $module_service)
57150f35adSGreg Roach    {
58f91b18ebSGreg Roach        $this->message_service = $message_service;
59150f35adSGreg Roach        $this->module_service = $module_service;
60150f35adSGreg Roach    }
61150f35adSGreg Roach
62a49d0e3fSGreg Roach    /**
63a49d0e3fSGreg Roach     * @param ServerRequestInterface $request
64a49d0e3fSGreg Roach     *
65a49d0e3fSGreg Roach     * @return ResponseInterface
66a49d0e3fSGreg Roach     */
67a49d0e3fSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
68a49d0e3fSGreg Roach    {
69*b55cbc6bSGreg Roach        $tree = Validator::attributes($request)->treeOptional();
70*b55cbc6bSGreg Roach        $user = Validator::attributes($request)->user();
71a49d0e3fSGreg Roach
72a49d0e3fSGreg Roach        if ($tree instanceof Tree) {
731fe542e9SGreg Roach            $my_individual_record = Registry::individualFactory()->make($tree->getUserPreference(Auth::user(), UserInterface::PREF_TREE_ACCOUNT_XREF), $tree);
741fe542e9SGreg Roach            $default_individual   = Registry::individualFactory()->make($tree->getUserPreference(Auth::user(), UserInterface::PREF_TREE_DEFAULT_XREF), $tree);
75a49d0e3fSGreg Roach        } else {
76a49d0e3fSGreg Roach            $my_individual_record = null;
77a49d0e3fSGreg Roach            $default_individual   = null;
78a49d0e3fSGreg Roach        }
79a49d0e3fSGreg Roach
80150f35adSGreg Roach        $languages = $this->module_service->findByInterface(ModuleLanguageInterface::class, true, true)
81150f35adSGreg Roach            ->mapWithKeys(static function (ModuleLanguageInterface $module): array {
82150f35adSGreg Roach                $locale = $module->locale();
83150f35adSGreg Roach
84150f35adSGreg Roach                return [$locale->languageTag() => $locale->endonym()];
85150f35adSGreg Roach            });
86150f35adSGreg Roach
871fe542e9SGreg Roach        $show_delete_option = $user->getPreference(UserInterface::PREF_IS_ADMINISTRATOR) !== '1';
88a49d0e3fSGreg Roach        $timezone_ids       = DateTimeZone::listIdentifiers();
89a49d0e3fSGreg Roach        $timezones          = array_combine($timezone_ids, $timezone_ids);
90a49d0e3fSGreg Roach        $title              = I18N::translate('My account');
91a49d0e3fSGreg Roach
92a49d0e3fSGreg Roach        return $this->viewResponse('edit-account-page', [
93f91b18ebSGreg Roach            'contact_methods'      => $this->message_service->contactMethods(),
94a49d0e3fSGreg Roach            'default_individual'   => $default_individual,
95150f35adSGreg Roach            'languages'            => $languages->all(),
96a49d0e3fSGreg Roach            'my_individual_record' => $my_individual_record,
97a49d0e3fSGreg Roach            'show_delete_option'   => $show_delete_option,
98a49d0e3fSGreg Roach            'timezones'            => $timezones,
99a49d0e3fSGreg Roach            'title'                => $title,
100a49d0e3fSGreg Roach            'tree'                 => $tree,
101a49d0e3fSGreg Roach            'user'                 => $user,
102a49d0e3fSGreg Roach        ]);
103a49d0e3fSGreg Roach    }
104a49d0e3fSGreg Roach}
105