xref: /webtrees/app/Http/RequestHandlers/TreePreferencesPage.php (revision 69cdf01494376a49252eb6ad994b0a685494b87e)
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    /** @var ModuleService */
85    private $module_service;
86
87    /** @var TreeService */
88    private $tree_service;
89
90    /** @var UserService */
91    private $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        $all_fam_facts = Collection::make(self::ALL_FAM_FACTS)
174            ->mapWithKeys(static function (string $tag): array {
175                return [$tag => Registry::elementFactory()->make('FAM:' . $tag)->label()];
176            })
177            ->sort(I18N::comparator());
178
179        $all_indi_facts = Collection::make(self::ALL_INDI_FACTS)
180            ->mapWithKeys(static function (string $tag): array {
181                return [$tag => Registry::elementFactory()->make('INDI:' . $tag)->label()];
182            })
183            ->sort(I18N::comparator());
184
185        $all_name_facts = Collection::make(self::ALL_NAME_FACTS)
186            ->mapWithKeys(static function (string $tag): array {
187                return [$tag => Registry::elementFactory()->make('INDI:NAME:' . $tag)->label()];
188            })
189            ->sort(I18N::comparator());
190
191        $all_plac_facts = Collection::make(self::ALL_PLAC_FACTS)
192            ->mapWithKeys(static function (string $tag): array {
193                return [$tag => Registry::elementFactory()->make('INDI:FACT:PLAC:' . $tag)->label()];
194            })
195            ->sort(I18N::comparator());
196
197        $all_repo_facts = Collection::make(self::ALL_REPO_FACTS)
198            ->mapWithKeys(static function (string $tag): array {
199                return [$tag => Registry::elementFactory()->make('REPO:' . $tag)->label()];
200            })
201            ->sort(I18N::comparator());
202
203        $all_sour_facts = Collection::make(self::ALL_SOUR_FACTS)
204            ->mapWithKeys(static function (string $tag): array {
205                return [$tag => Registry::elementFactory()->make('SOUR:' . $tag)->label()];
206            })
207            ->sort(I18N::comparator());
208
209        $all_surname_traditions = SurnameTradition::allDescriptions();
210
211        $tree_count = $this->tree_service->all()->count();
212
213        $title = I18N::translate('Preferences') . ' — ' . e($tree->title());
214
215        $base_url = app(ServerRequestInterface::class)->getAttribute('base_url');
216
217        return $this->viewResponse('admin/trees-preferences', [
218            'all_fam_facts'            => $all_fam_facts,
219            'all_indi_facts'           => $all_indi_facts,
220            'all_name_facts'           => $all_name_facts,
221            'all_plac_facts'           => $all_plac_facts,
222            'all_repo_facts'           => $all_repo_facts,
223            'all_sour_facts'           => $all_sour_facts,
224            'all_surname_traditions'   => $all_surname_traditions,
225            'base_url'                 => $base_url,
226            'calendar_formats'         => $calendar_formats,
227            'data_folder'              => $data_folder,
228            'formats'                  => $formats,
229            'french_calendar_end'      => $french_calendar_end,
230            'french_calendar_start'    => $french_calendar_start,
231            'gregorian_calendar_start' => $gregorian_calendar_start,
232            'members'                  => $members,
233            'page_layouts'             => $page_layouts,
234            'pedigree_individual'      => $pedigree_individual,
235            'privacy_options'          => $privacy_options,
236            'relatives_events'         => $relatives_events,
237            'source_types'             => $source_types,
238            'surname_list_styles'      => $surname_list_styles,
239            'theme_options'            => $theme_options,
240            'title'                    => $title,
241            'tree'                     => $tree,
242            'tree_count'               => $tree_count,
243        ]);
244    }
245}
246