xref: /webtrees/app/Http/RequestHandlers/TreePreferencesPage.php (revision 3faaf002227d16afd77a906db4683ac5c8df89ae)
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 const ALL_FAM_FACTS = [
55        'RESN', 'ANUL', 'CENS', 'DIV', 'DIVF', 'ENGA', 'MARB', 'MARC', 'MARR', 'MARL', 'MARS', 'RESI', 'EVEN',
56        'NCHI', 'SUBM', 'SLGS', 'REFN', 'RIN', 'CHAN', 'NOTE', 'SOUR', 'OBJE',
57        '_NMR', '_COML', '_MBON', '_MARI', '_SEPR', '_TODO',
58    ];
59
60    private const ALL_INDI_FACTS = [
61        'RESN', 'NAME', 'SEX', 'BIRT', 'CHR', 'DEAT', 'BURI', 'CREM', 'ADOP', 'BAPM', 'BARM', 'BASM',
62        'BLES', 'CHRA', 'CONF', 'FCOM', 'ORDN', 'NATU', 'EMIG', 'IMMI', 'CENS', 'PROB', 'WILL',
63        'GRAD', 'RETI', 'EVEN', 'CAST', 'DSCR', 'EDUC', 'IDNO', 'NATI', 'NCHI', 'NMR', 'OCCU', 'PROP',
64        'RELI', 'RESI', 'SSN', 'TITL', 'FACT', 'BAPL', 'CONL', 'ENDL', 'SLGC', 'SUBM', 'ASSO',
65        'ALIA', 'ANCI', 'DESI', 'RFN', 'AFN', 'REFN', 'RIN', 'CHAN', 'NOTE', 'SOUR', 'OBJE',
66        '_BRTM', '_DEG', '_DNA', '_EYEC', '_FNRL', '_HAIR', '_HEIG', '_HNM', '_HOL', '_INTE', '_MDCL',
67        '_MEDC', '_MILI', '_MILT', '_NAME', '_NAMS', '_NLIV', '_NMAR', '_PRMN', '_TODO', '_UID', '_WEIG', '_YART',
68    ];
69
70    private const ALL_NAME_FACTS = [
71        'FONE', 'ROMN', '_HEB', '_AKA', '_MARNM',
72    ];
73
74    private const ALL_PLAC_FACTS = [
75        'FONE', 'ROMN', '_GOV', '_HEB',
76    ];
77
78    private const ALL_REPO_FACTS = [
79        'NAME', 'ADDR', 'PHON', 'EMAIL', 'FAX', 'WWW', 'NOTE', 'REFN', 'RIN', 'CHAN', 'RESN',
80    ];
81
82    private const ALL_SOUR_FACTS = [
83        'DATA', 'AUTH', 'TITL', 'ABBR', 'PUBL', 'TEXT', 'REPO', 'REFN', 'RIN',
84        'CHAN', 'NOTE', 'OBJE', 'RESN',
85    ];
86
87    private ModuleService $module_service;
88
89    private TreeService $tree_service;
90
91    private UserService $user_service;
92
93    public function __construct(
94        ModuleService $module_service,
95        TreeService $tree_service,
96        UserService $user_service
97    ) {
98        $this->module_service = $module_service;
99        $this->tree_service   = $tree_service;
100        $this->user_service   = $user_service;
101    }
102
103    /**
104     * @param ServerRequestInterface $request
105     *
106     * @return ResponseInterface
107     */
108    public function handle(ServerRequestInterface $request): ResponseInterface
109    {
110        $this->layout = 'layouts/administration';
111
112        $tree = $request->getAttribute('tree');
113        assert($tree instanceof Tree);
114
115        $data_folder = Registry::filesystem()->dataName();
116
117        $french_calendar_start    = new Date('22 SEP 1792');
118        $french_calendar_end      = new Date('31 DEC 1805');
119        $gregorian_calendar_start = new Date('15 OCT 1582');
120
121        $surname_list_styles = [
122            /* I18N: Layout option for lists of names */
123            'style1' => I18N::translate('list'),
124            /* I18N: Layout option for lists of names */
125            'style2' => I18N::translate('table'),
126            /* I18N: Layout option for lists of names */
127            'style3' => I18N::translate('tag cloud'),
128        ];
129
130        $page_layouts = [
131            /* I18N: page orientation */
132            0 => I18N::translate('Portrait'),
133            /* I18N: page orientation */
134            1 => I18N::translate('Landscape'),
135        ];
136
137        $formats = [
138            /* I18N: None of the other options */
139            ''         => I18N::translate('none'),
140            /* I18N: https://en.wikipedia.org/wiki/Markdown */
141            'markdown' => I18N::translate('markdown'),
142        ];
143
144        $source_types = [
145            0 => I18N::translate('none'),
146            1 => I18N::translate('facts'),
147            2 => I18N::translate('records'),
148        ];
149
150        $theme_options = $this->module_service
151            ->findByInterface(ModuleThemeInterface::class)
152            ->map($this->module_service->titleMapper())
153            ->prepend(I18N::translate('<default theme>'), '');
154
155        $privacy_options = [
156            Auth::PRIV_USER => I18N::translate('Show to members'),
157            Auth::PRIV_NONE => I18N::translate('Show to managers'),
158            Auth::PRIV_HIDE => I18N::translate('Hide from everyone'),
159        ];
160
161        // For historical reasons, we have two fields in one
162        $calendar_formats = explode('_and_', $tree->getPreference('CALENDAR_FORMAT') . '_and_');
163
164        // Split into separate fields
165        $relatives_events = explode(',', $tree->getPreference('SHOW_RELATIVES_EVENTS'));
166
167        $pedigree_individual = Registry::individualFactory()->make($tree->getPreference('PEDIGREE_ROOT_ID'), $tree);
168
169        $members = $this->user_service->all()->filter(static function (UserInterface $user) use ($tree): bool {
170            return Auth::isMember($tree, $user);
171        });
172
173        $ignore_facts = ['CHAN', 'CHIL', 'FAMC', 'FAMS', 'HUSB', 'NOTE', 'OBJE', 'SOUR', 'SUBM', 'WIFE'];
174
175        $all_family_facts = Collection::make(Registry::elementFactory()->make('FAM')->subtags())
176            ->filter(static fn (string $value, string $key): bool => !in_array($key, $ignore_facts, true))
177            ->mapWithKeys(static fn (string $value, string $key): array => [$key => 'FAM:' . $key])
178            ->map(static fn (string $tag): ElementInterface => Registry::elementFactory()->make($tag))
179            ->filter(static fn (ElementInterface $element): bool => !$element instanceof UnknownElement)
180            ->map(static fn (ElementInterface $element): string => $element->label())
181            ->sort(I18N::comparator());
182
183        $all_individual_facts = Collection::make(Registry::elementFactory()->make('INDI')->subtags())
184            ->filter(static fn (string $value, string $key): bool => !in_array($key, $ignore_facts, true))
185            ->mapWithKeys(static fn (string $value, string $key): array => [$key => 'INDI:' . $key])
186            ->map(static fn (string $tag): ElementInterface => Registry::elementFactory()->make($tag))
187            ->filter(static fn (ElementInterface $element): bool => !$element instanceof UnknownElement)
188            ->map(static fn (ElementInterface $element): string => $element->label())
189            ->sort(I18N::comparator());
190
191        $all_surname_traditions = SurnameTradition::allDescriptions();
192
193        $tree_count = $this->tree_service->all()->count();
194
195        $title = I18N::translate('Preferences') . ' — ' . e($tree->title());
196
197        $base_url = app(ServerRequestInterface::class)->getAttribute('base_url');
198
199        return $this->viewResponse('admin/trees-preferences', [
200            'all_family_facts'         => $all_family_facts,
201            'all_individual_facts'     => $all_individual_facts,
202            'all_surname_traditions'   => $all_surname_traditions,
203            'base_url'                 => $base_url,
204            'calendar_formats'         => $calendar_formats,
205            'data_folder'              => $data_folder,
206            'formats'                  => $formats,
207            'french_calendar_end'      => $french_calendar_end,
208            'french_calendar_start'    => $french_calendar_start,
209            'gregorian_calendar_start' => $gregorian_calendar_start,
210            'members'                  => $members,
211            'page_layouts'             => $page_layouts,
212            'pedigree_individual'      => $pedigree_individual,
213            'privacy_options'          => $privacy_options,
214            'relatives_events'         => $relatives_events,
215            'source_types'             => $source_types,
216            'surname_list_styles'      => $surname_list_styles,
217            'theme_options'            => $theme_options,
218            'title'                    => $title,
219            'tree'                     => $tree,
220            'tree_count'               => $tree_count,
221        ]);
222    }
223}
224