xref: /webtrees/app/Http/RequestHandlers/TreePreferencesPage.php (revision c43c166ecb395e5c9f57dc8e20d9f6f7a27bf410)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2020 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 <http://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\GedcomTag;
26use Fisharebest\Webtrees\Http\ViewResponseTrait;
27use Fisharebest\Webtrees\I18N;
28use Fisharebest\Webtrees\Module\ModuleThemeInterface;
29use Fisharebest\Webtrees\Registry;
30use Fisharebest\Webtrees\Services\ModuleService;
31use Fisharebest\Webtrees\Services\TreeService;
32use Fisharebest\Webtrees\Services\UserService;
33use Fisharebest\Webtrees\SurnameTradition;
34use Fisharebest\Webtrees\Tree;
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    /** @var ModuleService */
55    private $module_service;
56
57    /** @var TreeService */
58    private $tree_service;
59
60    /** @var UserService */
61    private $user_service;
62
63    public function __construct(
64        ModuleService $module_service,
65        TreeService $tree_service,
66        UserService $user_service
67    ) {
68        $this->module_service = $module_service;
69        $this->tree_service   = $tree_service;
70        $this->user_service   = $user_service;
71    }
72
73    /**
74     * @param ServerRequestInterface $request
75     *
76     * @return ResponseInterface
77     */
78    public function handle(ServerRequestInterface $request): ResponseInterface
79    {
80        $this->layout = 'layouts/administration';
81
82        $tree = $request->getAttribute('tree');
83        assert($tree instanceof Tree);
84
85        $data_folder = Registry::filesystem()->dataName();
86
87        $french_calendar_start    = new Date('22 SEP 1792');
88        $french_calendar_end      = new Date('31 DEC 1805');
89        $gregorian_calendar_start = new Date('15 OCT 1582');
90
91        $surname_list_styles = [
92            /* I18N: Layout option for lists of names */
93            'style1' => I18N::translate('list'),
94            /* I18N: Layout option for lists of names */
95            'style2' => I18N::translate('table'),
96            /* I18N: Layout option for lists of names */
97            'style3' => I18N::translate('tag cloud'),
98        ];
99
100        $page_layouts = [
101            /* I18N: page orientation */
102            0 => I18N::translate('Portrait'),
103            /* I18N: page orientation */
104            1 => I18N::translate('Landscape'),
105        ];
106
107        $formats = [
108            /* I18N: None of the other options */
109            ''         => I18N::translate('none'),
110            /* I18N: https://en.wikipedia.org/wiki/Markdown */
111            'markdown' => I18N::translate('markdown'),
112        ];
113
114        $source_types = [
115            0 => I18N::translate('none'),
116            1 => I18N::translate('facts'),
117            2 => I18N::translate('records'),
118        ];
119
120        $theme_options = $this->module_service
121            ->findByInterface(ModuleThemeInterface::class)
122            ->map($this->module_service->titleMapper())
123            ->prepend(I18N::translate('<default theme>'), '');
124
125        $privacy_options = [
126            Auth::PRIV_USER => I18N::translate('Show to members'),
127            Auth::PRIV_NONE => I18N::translate('Show to managers'),
128            Auth::PRIV_HIDE => I18N::translate('Hide from everyone'),
129        ];
130
131        $tags = array_unique(array_merge(
132            explode(',', $tree->getPreference('INDI_FACTS_ADD')),
133            explode(',', $tree->getPreference('INDI_FACTS_UNIQUE')),
134            explode(',', $tree->getPreference('FAM_FACTS_ADD')),
135            explode(',', $tree->getPreference('FAM_FACTS_UNIQUE')),
136            explode(',', $tree->getPreference('NOTE_FACTS_ADD')),
137            explode(',', $tree->getPreference('NOTE_FACTS_UNIQUE')),
138            explode(',', $tree->getPreference('SOUR_FACTS_ADD')),
139            explode(',', $tree->getPreference('SOUR_FACTS_UNIQUE')),
140            explode(',', $tree->getPreference('REPO_FACTS_ADD')),
141            explode(',', $tree->getPreference('REPO_FACTS_UNIQUE')),
142            ['SOUR', 'REPO', 'OBJE', '_PRIM', 'NOTE', 'SUBM', 'SUBN', '_UID', 'CHAN']
143        ));
144
145        $all_tags = [];
146        foreach ($tags as $tag) {
147            if ($tag) {
148                $all_tags[$tag] = GedcomTag::getLabel($tag);
149            }
150        }
151
152        uasort($all_tags, '\Fisharebest\Webtrees\I18N::strcasecmp');
153
154        // For historical reasons, we have two fields in one
155        $calendar_formats = explode('_and_', $tree->getPreference('CALENDAR_FORMAT') . '_and_');
156
157        // Split into separate fields
158        $relatives_events = explode(',', $tree->getPreference('SHOW_RELATIVES_EVENTS'));
159
160        $pedigree_individual = Registry::individualFactory()->make($tree->getPreference('PEDIGREE_ROOT_ID'), $tree);
161
162        $members = $this->user_service->all()->filter(static function (UserInterface $user) use ($tree): bool {
163            return Auth::isMember($tree, $user);
164        });
165
166        $all_fam_facts  = GedcomTag::getPicklistFacts('FAM');
167        $all_indi_facts = GedcomTag::getPicklistFacts('INDI');
168        $all_name_facts = GedcomTag::getPicklistFacts('NAME');
169        $all_plac_facts = GedcomTag::getPicklistFacts('PLAC');
170        $all_repo_facts = GedcomTag::getPicklistFacts('REPO');
171        $all_sour_facts = GedcomTag::getPicklistFacts('SOUR');
172
173        $all_surname_traditions = SurnameTradition::allDescriptions();
174
175        $tree_count = $this->tree_service->all()->count();
176
177        $title = I18N::translate('Preferences') . ' — ' . e($tree->title());
178
179        $base_url = app(ServerRequestInterface::class)->getAttribute('base_url');
180
181        return $this->viewResponse('admin/trees-preferences', [
182            'all_fam_facts'            => $all_fam_facts,
183            'all_indi_facts'           => $all_indi_facts,
184            'all_name_facts'           => $all_name_facts,
185            'all_plac_facts'           => $all_plac_facts,
186            'all_repo_facts'           => $all_repo_facts,
187            'all_sour_facts'           => $all_sour_facts,
188            'all_surname_traditions'   => $all_surname_traditions,
189            'base_url'                 => $base_url,
190            'calendar_formats'         => $calendar_formats,
191            'data_folder'              => $data_folder,
192            'formats'                  => $formats,
193            'french_calendar_end'      => $french_calendar_end,
194            'french_calendar_start'    => $french_calendar_start,
195            'gregorian_calendar_start' => $gregorian_calendar_start,
196            'members'                  => $members,
197            'page_layouts'             => $page_layouts,
198            'pedigree_individual'      => $pedigree_individual,
199            'privacy_options'          => $privacy_options,
200            'relatives_events'         => $relatives_events,
201            'source_types'             => $source_types,
202            'surname_list_styles'      => $surname_list_styles,
203            'theme_options'            => $theme_options,
204            'title'                    => $title,
205            'tree'                     => $tree,
206            'tree_count'               => $tree_count,
207        ]);
208    }
209}
210