xref: /webtrees/app/Http/RequestHandlers/TreePreferencesAction.php (revision 45ebc6c6221f90468111ed2015b8449dca331d93)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2022 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\FlashMessages;
24use Fisharebest\Webtrees\I18N;
25use Fisharebest\Webtrees\Validator;
26use Illuminate\Database\Capsule\Manager as DB;
27use PDOException;
28use Psr\Http\Message\ResponseInterface;
29use Psr\Http\Message\ServerRequestInterface;
30use Psr\Http\Server\RequestHandlerInterface;
31
32use function array_unique;
33use function e;
34use function implode;
35use function preg_replace;
36use function redirect;
37use function route;
38use function trim;
39
40/**
41 * Edit the tree preferences.
42 */
43class TreePreferencesAction implements RequestHandlerInterface
44{
45    /**
46     * @param ServerRequestInterface $request
47     *
48     * @return ResponseInterface
49     */
50    public function handle(ServerRequestInterface $request): ResponseInterface
51    {
52        $tree = Validator::attributes($request)->tree();
53
54        // For backwards compatibility with webtrees 1.x we store the two calendar formats in one variable
55        // e.g. "gregorian_and_jewish"
56        $calendar_format_0           = Validator::parsedBody($request)->string('CALENDAR_FORMAT0');
57        $calendar_format_1           = Validator::parsedBody($request)->string('CALENDAR_FORMAT1');
58        $calendar_format             = implode('_and_', array_unique([$calendar_format_0, $calendar_format_1]));
59        $chart_box_tags              = Validator::parsedBody($request)->array('CHART_BOX_TAGS');
60        $contact_user_id             = Validator::parsedBody($request)->string('CONTACT_USER_ID');
61        $expand_notes                = Validator::parsedBody($request)->boolean('EXPAND_NOTES');
62        $expand_sources              = Validator::parsedBody($request)->boolean('EXPAND_SOURCES');
63        $fam_facts_quick             = Validator::parsedBody($request)->array('FAM_FACTS_QUICK');
64        $format_text                 = Validator::parsedBody($request)->string('FORMAT_TEXT');
65        $generate_uuids              = Validator::parsedBody($request)->boolean('GENERATE_UIDS');
66        $hide_gedcom_errors          = Validator::parsedBody($request)->boolean('HIDE_GEDCOM_ERRORS');
67        $indi_facts_quick            = Validator::parsedBody($request)->array('INDI_FACTS_QUICK');
68        $media_upload                = Validator::parsedBody($request)->integer('MEDIA_UPLOAD');
69        $meta_description            = Validator::parsedBody($request)->string('META_DESCRIPTION');
70        $meta_title                  = Validator::parsedBody($request)->string('META_TITLE');
71        $no_update_chan              = Validator::parsedBody($request)->boolean('NO_UPDATE_CHAN');
72        $pedigree_root_id            = Validator::parsedBody($request)->string('PEDIGREE_ROOT_ID');
73        $quick_required_facts        = Validator::parsedBody($request)->array('QUICK_REQUIRED_FACTS');
74        $quick_required_famfacts     = Validator::parsedBody($request)->array('QUICK_REQUIRED_FAMFACTS');
75        $show_counter                = Validator::parsedBody($request)->boolean('SHOW_COUNTER');
76        $show_est_list_dates         = Validator::parsedBody($request)->boolean('SHOW_EST_LIST_DATES');
77        $show_fact_icons             = Validator::parsedBody($request)->boolean('SHOW_FACT_ICONS');
78        $show_gedcom_record          = Validator::parsedBody($request)->boolean('SHOW_GEDCOM_RECORD');
79        $show_highlight_images       = Validator::parsedBody($request)->boolean('SHOW_HIGHLIGHT_IMAGES');
80        $show_last_change            = Validator::parsedBody($request)->boolean('SHOW_LAST_CHANGE');
81        $show_media_download         = Validator::parsedBody($request)->integer('SHOW_MEDIA_DOWNLOAD');
82        $show_no_watermark           = Validator::parsedBody($request)->integer('SHOW_NO_WATERMARK');
83        $show_parents_age            = Validator::parsedBody($request)->boolean('SHOW_PARENTS_AGE');
84        $show_pedigree_places        = Validator::parsedBody($request)->integer('SHOW_PEDIGREE_PLACES');
85        $show_pedigree_places_suffix = Validator::parsedBody($request)->integer('SHOW_PEDIGREE_PLACES_SUFFIX');
86        $show_relatives_events       = Validator::parsedBody($request)->array('SHOW_RELATIVES_EVENTS');
87        $sublist_trigger_i           = Validator::parsedBody($request)->integer('SUBLIST_TRIGGER_I');
88        $surname_list_style          = Validator::parsedBody($request)->string('SURNAME_LIST_STYLE');
89        $surname_tradition           = Validator::parsedBody($request)->string('SURNAME_TRADITION');
90        $use_silhouette              = Validator::parsedBody($request)->boolean('USE_SILHOUETTE');
91        $webmaster_user_id           = Validator::parsedBody($request)->integer('WEBMASTER_USER_ID');
92        $title                       = Validator::parsedBody($request)->string('title');
93
94        $tree->setPreference('CALENDAR_FORMAT', $calendar_format);
95        $tree->setPreference('CHART_BOX_TAGS', implode(',', $chart_box_tags));
96        $tree->setPreference('CONTACT_USER_ID', $contact_user_id);
97        $tree->setPreference('EXPAND_NOTES', (string) $expand_notes);
98        $tree->setPreference('EXPAND_SOURCES', (string) $expand_sources);
99        $tree->setPreference('FAM_FACTS_QUICK', implode(',', $fam_facts_quick));
100        $tree->setPreference('FORMAT_TEXT', $format_text);
101        $tree->setPreference('GENERATE_UIDS', (string) $generate_uuids);
102        $tree->setPreference('HIDE_GEDCOM_ERRORS', (string) $hide_gedcom_errors);
103        $tree->setPreference('INDI_FACTS_QUICK', implode(',', $indi_facts_quick));
104        $tree->setPreference('MEDIA_UPLOAD', (string) $media_upload);
105        $tree->setPreference('META_DESCRIPTION', $meta_description);
106        $tree->setPreference('META_TITLE', $meta_title);
107        $tree->setPreference('NO_UPDATE_CHAN', (string) $no_update_chan);
108        $tree->setPreference('PEDIGREE_ROOT_ID', $pedigree_root_id);
109        $tree->setPreference('QUICK_REQUIRED_FACTS', implode(',', $quick_required_facts));
110        $tree->setPreference('QUICK_REQUIRED_FAMFACTS', implode(',', $quick_required_famfacts));
111        $tree->setPreference('SHOW_COUNTER', (string) $show_counter);
112        $tree->setPreference('SHOW_EST_LIST_DATES', (string) $show_est_list_dates);
113        $tree->setPreference('SHOW_FACT_ICONS', (string) $show_fact_icons);
114        $tree->setPreference('SHOW_GEDCOM_RECORD', (string) $show_gedcom_record);
115        $tree->setPreference('SHOW_HIGHLIGHT_IMAGES', (string) $show_highlight_images);
116        $tree->setPreference('SHOW_LAST_CHANGE', (string) $show_last_change);
117        $tree->setPreference('SHOW_MEDIA_DOWNLOAD', (string) $show_media_download);
118        $tree->setPreference('SHOW_NO_WATERMARK', (string) $show_no_watermark);
119        $tree->setPreference('SHOW_PARENTS_AGE', (string) $show_parents_age);
120        $tree->setPreference('SHOW_PEDIGREE_PLACES', (string) $show_pedigree_places);
121        $tree->setPreference('SHOW_PEDIGREE_PLACES_SUFFIX', (string) $show_pedigree_places_suffix);
122        $tree->setPreference('SHOW_RELATIVES_EVENTS', implode(',', $show_relatives_events));
123        $tree->setPreference('SUBLIST_TRIGGER_I', (string) $sublist_trigger_i);
124        $tree->setPreference('SURNAME_LIST_STYLE', $surname_list_style);
125        $tree->setPreference('SURNAME_TRADITION', $surname_tradition);
126        $tree->setPreference('USE_SILHOUETTE', (string) $use_silhouette);
127        $tree->setPreference('WEBMASTER_USER_ID', (string) $webmaster_user_id);
128        $tree->setPreference('title', $title);
129
130        if (Auth::isAdmin()) {
131            // Only accept valid folders for MEDIA_DIRECTORY
132            $MEDIA_DIRECTORY = Validator::parsedBody($request)->string('MEDIA_DIRECTORY');
133            $MEDIA_DIRECTORY = preg_replace('/[:\/\\\\]+/', '/', $MEDIA_DIRECTORY);
134            $MEDIA_DIRECTORY = trim($MEDIA_DIRECTORY, '/') . '/';
135
136            $tree->setPreference('MEDIA_DIRECTORY', $MEDIA_DIRECTORY);
137        }
138
139        $gedcom = Validator::parsedBody($request)->string('gedcom');
140        $url    = route(ManageTrees::class, ['tree' => $tree->name()]);
141
142        if (Auth::isAdmin() && $gedcom !== '' && $gedcom !== $tree->name()) {
143            try {
144                DB::table('gedcom')
145                    ->where('gedcom_id', '=', $tree->id())
146                    ->update(['gedcom_name' => $gedcom]);
147
148                // Did we rename the default tree?
149                DB::table('site_setting')
150                    ->where('setting_name', '=', 'DEFAULT_GEDCOM')
151                    ->where('setting_value', '=', $tree->name())
152                    ->update(['setting_value' => $gedcom]);
153
154                $url = route(ManageTrees::class, ['tree' => $gedcom]);
155            } catch (PDOException) {
156                // Probably a duplicate name.
157            }
158        }
159
160        FlashMessages::addMessage(I18N::translate('The preferences for the family tree “%s” have been updated.', e($tree->title())), 'success');
161
162        // Coming soon...
163        $all_trees = Validator::parsedBody($request)->boolean('all_trees', false);
164        $new_trees = Validator::parsedBody($request)->boolean('new_trees', false);
165
166        if ($all_trees) {
167            FlashMessages::addMessage(I18N::translate('The preferences for all family trees have been updated.'), 'success');
168        }
169
170        if ($new_trees) {
171            FlashMessages::addMessage(I18N::translate('The preferences for new family trees have been updated.'), 'success');
172        }
173
174        return redirect($url);
175    }
176}
177