Passed
Push — master ( 57bfa9...a49d0e )
by Greg
05:39
created

AccountEdit::installedLanguages()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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