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