xref: /webtrees/app/Http/RequestHandlers/TreePreferencesPage.php (revision e57829396e448d173bb4be808a8b9fd89548228b)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 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 <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22use Fisharebest\Webtrees\Auth;
23use Fisharebest\Webtrees\Contracts\ElementInterface;
24use Fisharebest\Webtrees\Contracts\UserInterface;
25use Fisharebest\Webtrees\Date;
26use Fisharebest\Webtrees\Elements\UnknownElement;
27use Fisharebest\Webtrees\Http\ViewResponseTrait;
28use Fisharebest\Webtrees\I18N;
29use Fisharebest\Webtrees\Module\ModuleThemeInterface;
30use Fisharebest\Webtrees\Registry;
31use Fisharebest\Webtrees\Services\ModuleService;
32use Fisharebest\Webtrees\Services\TreeService;
33use Fisharebest\Webtrees\Services\UserService;
34use Fisharebest\Webtrees\SurnameTradition;
35use Fisharebest\Webtrees\Tree;
36use Illuminate\Support\Collection;
37use Psr\Http\Message\ResponseInterface;
38use Psr\Http\Message\ServerRequestInterface;
39use Psr\Http\Server\RequestHandlerInterface;
40
41use function app;
42use function assert;
43use function e;
44use function explode;
45use function in_array;
46
47/**
48 * Edit the tree preferences.
49 */
50class TreePreferencesPage implements RequestHandlerInterface
51{
52    use ViewResponseTrait;
53
54    private ModuleService $module_service;
55
56    private TreeService $tree_service;
57
58    private UserService $user_service;
59
60    public function __construct(
61        ModuleService $module_service,
62        TreeService $tree_service,
63        UserService $user_service
64    ) {
65        $this->module_service = $module_service;
66        $this->tree_service   = $tree_service;
67        $this->user_service   = $user_service;
68    }
69
70    /**
71     * @param ServerRequestInterface $request
72     *
73     * @return ResponseInterface
74     */
75    public function handle(ServerRequestInterface $request): ResponseInterface
76    {
77        $this->layout = 'layouts/administration';
78
79        $tree = $request->getAttribute('tree');
80        assert($tree instanceof Tree);
81
82        $data_folder = Registry::filesystem()->dataName();
83
84        $french_calendar_start    = new Date('22 SEP 1792');
85        $french_calendar_end      = new Date('31 DEC 1805');
86        $gregorian_calendar_start = new Date('15 OCT 1582');
87
88        $surname_list_styles = [
89            /* I18N: Layout option for lists of names */
90            'style1' => I18N::translate('list'),
91            /* I18N: Layout option for lists of names */
92            'style2' => I18N::translate('table'),
93            /* I18N: Layout option for lists of names */
94            'style3' => I18N::translate('tag cloud'),
95        ];
96
97        $page_layouts = [
98            /* I18N: page orientation */
99            0 => I18N::translate('Portrait'),
100            /* I18N: page orientation */
101            1 => I18N::translate('Landscape'),
102        ];
103
104        $formats = [
105            /* I18N: None of the other options */
106            ''         => I18N::translate('none'),
107            /* I18N: https://en.wikipedia.org/wiki/Markdown */
108            'markdown' => I18N::translate('markdown'),
109        ];
110
111        $source_types = [
112            0 => I18N::translate('none'),
113            1 => I18N::translate('facts'),
114            2 => I18N::translate('records'),
115        ];
116
117        $theme_options = $this->module_service
118            ->findByInterface(ModuleThemeInterface::class)
119            ->map($this->module_service->titleMapper())
120            ->prepend(I18N::translate('<default theme>'), '');
121
122        $privacy_options = [
123            Auth::PRIV_USER => I18N::translate('Show to members'),
124            Auth::PRIV_NONE => I18N::translate('Show to managers'),
125            Auth::PRIV_HIDE => I18N::translate('Hide from everyone'),
126        ];
127
128        // For historical reasons, we have two fields in one
129        $calendar_formats = explode('_and_', $tree->getPreference('CALENDAR_FORMAT') . '_and_');
130
131        // Split into separate fields
132        $relatives_events = explode(',', $tree->getPreference('SHOW_RELATIVES_EVENTS'));
133
134        $pedigree_individual = Registry::individualFactory()->make($tree->getPreference('PEDIGREE_ROOT_ID'), $tree);
135
136        $members = $this->user_service->all()->filter(static function (UserInterface $user) use ($tree): bool {
137            return Auth::isMember($tree, $user);
138        });
139
140        $ignore_facts = ['CHAN', 'CHIL', 'FAMC', 'FAMS', 'HUSB', 'NOTE', 'OBJE', 'SOUR', 'SUBM', 'WIFE'];
141
142        $all_family_facts = Collection::make(Registry::elementFactory()->make('FAM')->subtags())
143            ->filter(static fn (string $value, string $key): bool => !in_array($key, $ignore_facts, true))
144            ->mapWithKeys(static fn (string $value, string $key): array => [$key => 'FAM:' . $key])
145            ->map(static fn (string $tag): ElementInterface => Registry::elementFactory()->make($tag))
146            ->filter(static fn (ElementInterface $element): bool => !$element instanceof UnknownElement)
147            ->map(static fn (ElementInterface $element): string => $element->label())
148            ->sort(I18N::comparator());
149
150        $all_individual_facts = Collection::make(Registry::elementFactory()->make('INDI')->subtags())
151            ->filter(static fn (string $value, string $key): bool => !in_array($key, $ignore_facts, true))
152            ->mapWithKeys(static fn (string $value, string $key): array => [$key => 'INDI:' . $key])
153            ->map(static fn (string $tag): ElementInterface => Registry::elementFactory()->make($tag))
154            ->filter(static fn (ElementInterface $element): bool => !$element instanceof UnknownElement)
155            ->map(static fn (ElementInterface $element): string => $element->label())
156            ->sort(I18N::comparator());
157
158        $all_surname_traditions = SurnameTradition::allDescriptions();
159
160        $tree_count = $this->tree_service->all()->count();
161
162        $title = I18N::translate('Preferences') . ' — ' . e($tree->title());
163
164        $base_url = app(ServerRequestInterface::class)->getAttribute('base_url');
165
166        return $this->viewResponse('admin/trees-preferences', [
167            'all_family_facts'         => $all_family_facts,
168            'all_individual_facts'     => $all_individual_facts,
169            'all_surname_traditions'   => $all_surname_traditions,
170            'base_url'                 => $base_url,
171            'calendar_formats'         => $calendar_formats,
172            'data_folder'              => $data_folder,
173            'formats'                  => $formats,
174            'french_calendar_end'      => $french_calendar_end,
175            'french_calendar_start'    => $french_calendar_start,
176            'gregorian_calendar_start' => $gregorian_calendar_start,
177            'members'                  => $members,
178            'page_layouts'             => $page_layouts,
179            'pedigree_individual'      => $pedigree_individual,
180            'privacy_options'          => $privacy_options,
181            'relatives_events'         => $relatives_events,
182            'source_types'             => $source_types,
183            'surname_list_styles'      => $surname_list_styles,
184            'theme_options'            => $theme_options,
185            'title'                    => $title,
186            'tree'                     => $tree,
187            'tree_count'               => $tree_count,
188        ]);
189    }
190}
191