xref: /webtrees/app/Http/RequestHandlers/TreePreferencesPage.php (revision 37646143628d6d9ab8f62ea7e5fc7d45a028daa9)
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 array_merge;
41use function array_unique;
42use function assert;
43use function e;
44use function explode;
45use function uasort;
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', 'SHARED_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', 'SHARED_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', 'SHARED_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', 'SHARED_NOTE', 'OBJE', 'RESN',
85    ];
86
87    /** @var ModuleService */
88    private $module_service;
89
90    /** @var TreeService */
91    private $tree_service;
92
93    /** @var UserService */
94    private $user_service;
95
96    public function __construct(
97        ModuleService $module_service,
98        TreeService $tree_service,
99        UserService $user_service
100    ) {
101        $this->module_service = $module_service;
102        $this->tree_service   = $tree_service;
103        $this->user_service   = $user_service;
104    }
105
106    /**
107     * @param ServerRequestInterface $request
108     *
109     * @return ResponseInterface
110     */
111    public function handle(ServerRequestInterface $request): ResponseInterface
112    {
113        $this->layout = 'layouts/administration';
114
115        $tree = $request->getAttribute('tree');
116        assert($tree instanceof Tree);
117
118        $data_folder = Registry::filesystem()->dataName();
119
120        $french_calendar_start    = new Date('22 SEP 1792');
121        $french_calendar_end      = new Date('31 DEC 1805');
122        $gregorian_calendar_start = new Date('15 OCT 1582');
123
124        $surname_list_styles = [
125            /* I18N: Layout option for lists of names */
126            'style1' => I18N::translate('list'),
127            /* I18N: Layout option for lists of names */
128            'style2' => I18N::translate('table'),
129            /* I18N: Layout option for lists of names */
130            'style3' => I18N::translate('tag cloud'),
131        ];
132
133        $page_layouts = [
134            /* I18N: page orientation */
135            0 => I18N::translate('Portrait'),
136            /* I18N: page orientation */
137            1 => I18N::translate('Landscape'),
138        ];
139
140        $formats = [
141            /* I18N: None of the other options */
142            ''         => I18N::translate('none'),
143            /* I18N: https://en.wikipedia.org/wiki/Markdown */
144            'markdown' => I18N::translate('markdown'),
145        ];
146
147        $source_types = [
148            0 => I18N::translate('none'),
149            1 => I18N::translate('facts'),
150            2 => I18N::translate('records'),
151        ];
152
153        $theme_options = $this->module_service
154            ->findByInterface(ModuleThemeInterface::class)
155            ->map($this->module_service->titleMapper())
156            ->prepend(I18N::translate('<default theme>'), '');
157
158        $privacy_options = [
159            Auth::PRIV_USER => I18N::translate('Show to members'),
160            Auth::PRIV_NONE => I18N::translate('Show to managers'),
161            Auth::PRIV_HIDE => I18N::translate('Hide from everyone'),
162        ];
163
164        $tags = array_unique(array_merge(
165            explode(',', $tree->getPreference('INDI_FACTS_ADD')),
166            explode(',', $tree->getPreference('INDI_FACTS_UNIQUE')),
167            explode(',', $tree->getPreference('FAM_FACTS_ADD')),
168            explode(',', $tree->getPreference('FAM_FACTS_UNIQUE')),
169            explode(',', $tree->getPreference('NOTE_FACTS_ADD')),
170            explode(',', $tree->getPreference('NOTE_FACTS_UNIQUE')),
171            explode(',', $tree->getPreference('SOUR_FACTS_ADD')),
172            explode(',', $tree->getPreference('SOUR_FACTS_UNIQUE')),
173            explode(',', $tree->getPreference('REPO_FACTS_ADD')),
174            explode(',', $tree->getPreference('REPO_FACTS_UNIQUE')),
175            ['SOUR', 'REPO', 'OBJE', '_PRIM', 'NOTE', 'SUBM', 'SUBN', '_UID', 'CHAN']
176        ));
177
178        $all_tags = [];
179        foreach ($tags as $tag) {
180            if ($tag) {
181                $all_tags[$tag] = GedcomTag::getLabel($tag);
182            }
183        }
184
185        uasort($all_tags, '\Fisharebest\Webtrees\I18N::strcasecmp');
186
187        // For historical reasons, we have two fields in one
188        $calendar_formats = explode('_and_', $tree->getPreference('CALENDAR_FORMAT') . '_and_');
189
190        // Split into separate fields
191        $relatives_events = explode(',', $tree->getPreference('SHOW_RELATIVES_EVENTS'));
192
193        $pedigree_individual = Registry::individualFactory()->make($tree->getPreference('PEDIGREE_ROOT_ID'), $tree);
194
195        $members = $this->user_service->all()->filter(static function (UserInterface $user) use ($tree): bool {
196            return Auth::isMember($tree, $user);
197        });
198
199        $all_fam_facts = Collection::make(self::ALL_FAM_FACTS)
200            ->mapWithKeys(static function (string $tag): array {
201                return [$tag => Registry::elementFactory()->make('FAM:' . $tag)->label()];
202            })
203            ->sort(I18N::comparator());
204
205        $all_indi_facts = Collection::make(self::ALL_INDI_FACTS)
206            ->mapWithKeys(static function (string $tag): array {
207                return [$tag => Registry::elementFactory()->make('INDI:' . $tag)->label()];
208            })
209            ->sort(I18N::comparator());
210
211        $all_name_facts = Collection::make(self::ALL_NAME_FACTS)
212            ->mapWithKeys(static function (string $tag): array {
213                return [$tag => Registry::elementFactory()->make('INDI:NAME:' . $tag)->label()];
214            })
215            ->sort(I18N::comparator());
216
217        $all_plac_facts = Collection::make(self::ALL_PLAC_FACTS)
218            ->mapWithKeys(static function (string $tag): array {
219                return [$tag => Registry::elementFactory()->make('INDI:FACT:PLAC:' . $tag)->label()];
220            })
221            ->sort(I18N::comparator());
222
223        $all_repo_facts = Collection::make(self::ALL_REPO_FACTS)
224            ->mapWithKeys(static function (string $tag): array {
225                return [$tag => Registry::elementFactory()->make('SOUR:' . $tag)->label()];
226            })
227            ->sort(I18N::comparator());
228
229        $all_sour_facts = Collection::make(self::ALL_SOUR_FACTS)
230            ->mapWithKeys(static function (string $tag): array {
231                return [$tag => Registry::elementFactory()->make('SOUR:' . $tag)->label()];
232            })
233            ->sort(I18N::comparator());
234
235        $all_surname_traditions = SurnameTradition::allDescriptions();
236
237        $tree_count = $this->tree_service->all()->count();
238
239        $title = I18N::translate('Preferences') . ' — ' . e($tree->title());
240
241        $base_url = app(ServerRequestInterface::class)->getAttribute('base_url');
242
243        return $this->viewResponse('admin/trees-preferences', [
244            'all_fam_facts'            => $all_fam_facts,
245            'all_indi_facts'           => $all_indi_facts,
246            'all_name_facts'           => $all_name_facts,
247            'all_plac_facts'           => $all_plac_facts,
248            'all_repo_facts'           => $all_repo_facts,
249            'all_sour_facts'           => $all_sour_facts,
250            'all_surname_traditions'   => $all_surname_traditions,
251            'base_url'                 => $base_url,
252            'calendar_formats'         => $calendar_formats,
253            'data_folder'              => $data_folder,
254            'formats'                  => $formats,
255            'french_calendar_end'      => $french_calendar_end,
256            'french_calendar_start'    => $french_calendar_start,
257            'gregorian_calendar_start' => $gregorian_calendar_start,
258            'members'                  => $members,
259            'page_layouts'             => $page_layouts,
260            'pedigree_individual'      => $pedigree_individual,
261            'privacy_options'          => $privacy_options,
262            'relatives_events'         => $relatives_events,
263            'source_types'             => $source_types,
264            'surname_list_styles'      => $surname_list_styles,
265            'theme_options'            => $theme_options,
266            'title'                    => $title,
267            'tree'                     => $tree,
268            'tree_count'               => $tree_count,
269        ]);
270    }
271}
272