xref: /webtrees/app/Http/RequestHandlers/AccountEdit.php (revision e93a8df2f8d797005750082cc3766c0e80799688)
1a49d0e3fSGreg Roach<?php
2a49d0e3fSGreg Roach
3a49d0e3fSGreg Roach/**
4a49d0e3fSGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 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;
32b55cbc6bSGreg 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    /**
51f91b18ebSGreg Roach     * @param MessageService $message_service
52150f35adSGreg Roach     * @param ModuleService  $module_service
53150f35adSGreg Roach     */
54f91b18ebSGreg Roach    public function __construct(MessageService $message_service, ModuleService $module_service)
55150f35adSGreg Roach    {
56f91b18ebSGreg Roach        $this->message_service = $message_service;
57150f35adSGreg Roach        $this->module_service = $module_service;
58150f35adSGreg Roach    }
59150f35adSGreg Roach
60a49d0e3fSGreg Roach    /**
61a49d0e3fSGreg Roach     * @param ServerRequestInterface $request
62a49d0e3fSGreg Roach     *
63a49d0e3fSGreg Roach     * @return ResponseInterface
64a49d0e3fSGreg Roach     */
65a49d0e3fSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
66a49d0e3fSGreg Roach    {
67b55cbc6bSGreg Roach        $tree = Validator::attributes($request)->treeOptional();
68b55cbc6bSGreg Roach        $user = Validator::attributes($request)->user();
69a49d0e3fSGreg Roach
70a49d0e3fSGreg Roach        if ($tree instanceof Tree) {
711fe542e9SGreg Roach            $my_individual_record = Registry::individualFactory()->make($tree->getUserPreference(Auth::user(), UserInterface::PREF_TREE_ACCOUNT_XREF), $tree);
721fe542e9SGreg Roach            $default_individual   = Registry::individualFactory()->make($tree->getUserPreference(Auth::user(), UserInterface::PREF_TREE_DEFAULT_XREF), $tree);
73a49d0e3fSGreg Roach        } else {
74a49d0e3fSGreg Roach            $my_individual_record = null;
75a49d0e3fSGreg Roach            $default_individual   = null;
76a49d0e3fSGreg Roach        }
77a49d0e3fSGreg Roach
78150f35adSGreg Roach        $languages = $this->module_service->findByInterface(ModuleLanguageInterface::class, true, true)
79150f35adSGreg Roach            ->mapWithKeys(static function (ModuleLanguageInterface $module): array {
80150f35adSGreg Roach                $locale = $module->locale();
81150f35adSGreg Roach
82150f35adSGreg Roach                return [$locale->languageTag() => $locale->endonym()];
83150f35adSGreg Roach            });
84150f35adSGreg Roach
851fe542e9SGreg Roach        $show_delete_option = $user->getPreference(UserInterface::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', [
91f91b18ebSGreg Roach            'contact_methods'      => $this->message_service->contactMethods(),
92a49d0e3fSGreg Roach            'default_individual'   => $default_individual,
93150f35adSGreg 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