xref: /webtrees/app/Http/RequestHandlers/TreePreferencesAction.php (revision 748dbe155a6d19d66918ad136947fa23ee8f8469)
16fd01894SGreg Roach<?php
26fd01894SGreg Roach
36fd01894SGreg Roach/**
46fd01894SGreg Roach * webtrees: online genealogy
55bfc6897SGreg Roach * Copyright (C) 2022 webtrees development team
66fd01894SGreg Roach * This program is free software: you can redistribute it and/or modify
76fd01894SGreg Roach * it under the terms of the GNU General Public License as published by
86fd01894SGreg Roach * the Free Software Foundation, either version 3 of the License, or
96fd01894SGreg Roach * (at your option) any later version.
106fd01894SGreg Roach * This program is distributed in the hope that it will be useful,
116fd01894SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
126fd01894SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
136fd01894SGreg Roach * GNU General Public License for more details.
146fd01894SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
166fd01894SGreg Roach */
176fd01894SGreg Roach
186fd01894SGreg Roachdeclare(strict_types=1);
196fd01894SGreg Roach
206fd01894SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
216fd01894SGreg Roach
22590a0393SGreg Roachuse Fisharebest\Webtrees\Auth;
236fd01894SGreg Roachuse Fisharebest\Webtrees\FlashMessages;
246fd01894SGreg Roachuse Fisharebest\Webtrees\I18N;
25b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
266fd01894SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
27fa5cbab5SGreg Roachuse PDOException;
286fd01894SGreg Roachuse Psr\Http\Message\ResponseInterface;
296fd01894SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
306fd01894SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
316fd01894SGreg Roach
326fd01894SGreg Roachuse function array_unique;
336fd01894SGreg Roachuse function e;
346fd01894SGreg Roachuse function implode;
356fd01894SGreg Roachuse function preg_replace;
366fd01894SGreg Roachuse function redirect;
376fd01894SGreg Roachuse function route;
386fd01894SGreg Roachuse function trim;
396fd01894SGreg Roach
406fd01894SGreg Roach/**
416fd01894SGreg Roach * Edit the tree preferences.
426fd01894SGreg Roach */
436fd01894SGreg Roachclass TreePreferencesAction implements RequestHandlerInterface
446fd01894SGreg Roach{
456fd01894SGreg Roach    /**
466fd01894SGreg Roach     * @param ServerRequestInterface $request
476fd01894SGreg Roach     *
486fd01894SGreg Roach     * @return ResponseInterface
496fd01894SGreg Roach     */
506fd01894SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
516fd01894SGreg Roach    {
52b55cbc6bSGreg Roach        $tree = Validator::attributes($request)->tree();
536fd01894SGreg Roach
546fd01894SGreg Roach        // For backwards compatibility with webtrees 1.x we store the two calendar formats in one variable
556fd01894SGreg Roach        // e.g. "gregorian_and_jewish"
56*748dbe15SGreg Roach        $calendar_format_0           = Validator::parsedBody($request)->string('CALENDAR_FORMAT0');
57*748dbe15SGreg Roach        $calendar_format_1           = Validator::parsedBody($request)->string('CALENDAR_FORMAT1');
58*748dbe15SGreg Roach        $calendar_format             = implode('_and_', array_unique([$calendar_format_0, $calendar_format_1]));
59*748dbe15SGreg Roach        $chart_box_tags              = Validator::parsedBody($request)->array('CHART_BOX_TAGS');
60*748dbe15SGreg Roach        $contact_user_id             = Validator::parsedBody($request)->string('CONTACT_USER_ID');
61*748dbe15SGreg Roach        $expand_notes                = Validator::parsedBody($request)->boolean('EXPAND_NOTES');
62*748dbe15SGreg Roach        $expand_sources              = Validator::parsedBody($request)->boolean('EXPAND_SOURCES');
63*748dbe15SGreg Roach        $fam_facts_quick             = Validator::parsedBody($request)->array('FAM_FACTS_QUICK');
64*748dbe15SGreg Roach        $format_text                 = Validator::parsedBody($request)->string('FORMAT_TEXT');
65*748dbe15SGreg Roach        $generate_uuids              = Validator::parsedBody($request)->boolean('GENERATE_UIDS');
66*748dbe15SGreg Roach        $hide_gedcom_errors          = Validator::parsedBody($request)->boolean('HIDE_GEDCOM_ERRORS');
67*748dbe15SGreg Roach        $indi_facts_quick            = Validator::parsedBody($request)->array('INDI_FACTS_QUICK');
68*748dbe15SGreg Roach        $media_upload                = Validator::parsedBody($request)->integer('MEDIA_UPLOAD');
69*748dbe15SGreg Roach        $meta_description            = Validator::parsedBody($request)->string('META_DESCRIPTION');
70*748dbe15SGreg Roach        $meta_title                  = Validator::parsedBody($request)->string('META_TITLE');
71*748dbe15SGreg Roach        $no_update_chan              = Validator::parsedBody($request)->boolean('NO_UPDATE_CHAN');
72*748dbe15SGreg Roach        $pedigree_root_id            = Validator::parsedBody($request)->string('PEDIGREE_ROOT_ID');
73*748dbe15SGreg Roach        $quick_required_facts        = Validator::parsedBody($request)->array('QUICK_REQUIRED_FACTS');
74*748dbe15SGreg Roach        $quick_required_famfacts     = Validator::parsedBody($request)->array('QUICK_REQUIRED_FAMFACTS');
75*748dbe15SGreg Roach        $show_counter                = Validator::parsedBody($request)->boolean('SHOW_COUNTER');
76*748dbe15SGreg Roach        $show_est_list_dates         = Validator::parsedBody($request)->boolean('SHOW_EST_LIST_DATES');
77*748dbe15SGreg Roach        $show_fact_icons             = Validator::parsedBody($request)->boolean('SHOW_FACT_ICONS');
78*748dbe15SGreg Roach        $show_gedcom_record          = Validator::parsedBody($request)->boolean('SHOW_GEDCOM_RECORD');
79*748dbe15SGreg Roach        $show_highlight_images       = Validator::parsedBody($request)->boolean('SHOW_HIGHLIGHT_IMAGES');
80*748dbe15SGreg Roach        $show_last_change            = Validator::parsedBody($request)->boolean('SHOW_LAST_CHANGE');
81*748dbe15SGreg Roach        $show_media_download         = Validator::parsedBody($request)->integer('SHOW_MEDIA_DOWNLOAD');
82*748dbe15SGreg Roach        $show_no_watermark           = Validator::parsedBody($request)->integer('SHOW_NO_WATERMARK');
83*748dbe15SGreg Roach        $show_parents_age            = Validator::parsedBody($request)->boolean('SHOW_PARENTS_AGE');
84*748dbe15SGreg Roach        $show_pedigree_places        = Validator::parsedBody($request)->integer('SHOW_PEDIGREE_PLACES');
85*748dbe15SGreg Roach        $show_pedigree_places_suffix = Validator::parsedBody($request)->integer('SHOW_PEDIGREE_PLACES_SUFFIX');
86*748dbe15SGreg Roach        $show_relatives_events       = Validator::parsedBody($request)->array('SHOW_RELATIVES_EVENTS');
87*748dbe15SGreg Roach        $sublist_trigger_i           = Validator::parsedBody($request)->integer('SUBLIST_TRIGGER_I');
88*748dbe15SGreg Roach        $surname_list_style          = Validator::parsedBody($request)->string('SURNAME_LIST_STYLE');
89*748dbe15SGreg Roach        $surname_tradition           = Validator::parsedBody($request)->string('SURNAME_TRADITION');
90*748dbe15SGreg Roach        $use_silhouette              = Validator::parsedBody($request)->boolean('USE_SILHOUETTE');
91*748dbe15SGreg Roach        $webmaster_user_id           = Validator::parsedBody($request)->integer('WEBMASTER_USER_ID');
92*748dbe15SGreg Roach        $title                       = Validator::parsedBody($request)->string('title');
93*748dbe15SGreg Roach
94*748dbe15SGreg Roach        $tree->setPreference('CALENDAR_FORMAT', $calendar_format);
95*748dbe15SGreg Roach        $tree->setPreference('CHART_BOX_TAGS', implode(',', $chart_box_tags));
96*748dbe15SGreg Roach        $tree->setPreference('CONTACT_USER_ID', $contact_user_id);
97*748dbe15SGreg Roach        $tree->setPreference('EXPAND_NOTES', (string) $expand_notes);
98*748dbe15SGreg Roach        $tree->setPreference('EXPAND_SOURCES', (string) $expand_sources);
99*748dbe15SGreg Roach        $tree->setPreference('FAM_FACTS_QUICK', implode(',', $fam_facts_quick));
100*748dbe15SGreg Roach        $tree->setPreference('FORMAT_TEXT', $format_text);
101*748dbe15SGreg Roach        $tree->setPreference('GENERATE_UIDS', (string) $generate_uuids);
102*748dbe15SGreg Roach        $tree->setPreference('HIDE_GEDCOM_ERRORS', (string) $hide_gedcom_errors);
103*748dbe15SGreg Roach        $tree->setPreference('INDI_FACTS_QUICK', implode(',', $indi_facts_quick));
104*748dbe15SGreg Roach        $tree->setPreference('MEDIA_UPLOAD', (string) $media_upload);
105*748dbe15SGreg Roach        $tree->setPreference('META_DESCRIPTION', $meta_description);
106*748dbe15SGreg Roach        $tree->setPreference('META_TITLE', $meta_title);
107*748dbe15SGreg Roach        $tree->setPreference('NO_UPDATE_CHAN', (string) $no_update_chan);
108*748dbe15SGreg Roach        $tree->setPreference('PEDIGREE_ROOT_ID', $pedigree_root_id);
109*748dbe15SGreg Roach        $tree->setPreference('QUICK_REQUIRED_FACTS', implode(',', $quick_required_facts));
110*748dbe15SGreg Roach        $tree->setPreference('QUICK_REQUIRED_FAMFACTS', implode(',', $quick_required_famfacts));
111*748dbe15SGreg Roach        $tree->setPreference('SHOW_COUNTER', (string) $show_counter);
112*748dbe15SGreg Roach        $tree->setPreference('SHOW_EST_LIST_DATES', (string) $show_est_list_dates);
113*748dbe15SGreg Roach        $tree->setPreference('SHOW_FACT_ICONS', (string) $show_fact_icons);
114*748dbe15SGreg Roach        $tree->setPreference('SHOW_GEDCOM_RECORD', (string) $show_gedcom_record);
115*748dbe15SGreg Roach        $tree->setPreference('SHOW_HIGHLIGHT_IMAGES', (string) $show_highlight_images);
116*748dbe15SGreg Roach        $tree->setPreference('SHOW_LAST_CHANGE', (string) $show_last_change);
117*748dbe15SGreg Roach        $tree->setPreference('SHOW_MEDIA_DOWNLOAD', (string) $show_media_download);
118*748dbe15SGreg Roach        $tree->setPreference('SHOW_NO_WATERMARK', (string) $show_no_watermark);
119*748dbe15SGreg Roach        $tree->setPreference('SHOW_PARENTS_AGE', (string) $show_parents_age);
120*748dbe15SGreg Roach        $tree->setPreference('SHOW_PEDIGREE_PLACES', (string) $show_pedigree_places);
121*748dbe15SGreg Roach        $tree->setPreference('SHOW_PEDIGREE_PLACES_SUFFIX', (string) $show_pedigree_places_suffix);
122*748dbe15SGreg Roach        $tree->setPreference('SHOW_RELATIVES_EVENTS', implode(',', $show_relatives_events));
123*748dbe15SGreg Roach        $tree->setPreference('SUBLIST_TRIGGER_I', (string) $sublist_trigger_i);
124*748dbe15SGreg Roach        $tree->setPreference('SURNAME_LIST_STYLE', $surname_list_style);
125*748dbe15SGreg Roach        $tree->setPreference('SURNAME_TRADITION', $surname_tradition);
126*748dbe15SGreg Roach        $tree->setPreference('USE_SILHOUETTE', (string) $use_silhouette);
127*748dbe15SGreg Roach        $tree->setPreference('WEBMASTER_USER_ID', (string) $webmaster_user_id);
128*748dbe15SGreg Roach        $tree->setPreference('title', $title);
1296fd01894SGreg Roach
130590a0393SGreg Roach        if (Auth::isAdmin()) {
1316fd01894SGreg Roach            // Only accept valid folders for MEDIA_DIRECTORY
132*748dbe15SGreg Roach            $MEDIA_DIRECTORY = Validator::parsedBody($request)->string('MEDIA_DIRECTORY');
1336fd01894SGreg Roach            $MEDIA_DIRECTORY = preg_replace('/[:\/\\\\]+/', '/', $MEDIA_DIRECTORY);
1346fd01894SGreg Roach            $MEDIA_DIRECTORY = trim($MEDIA_DIRECTORY, '/') . '/';
1356fd01894SGreg Roach
1366fd01894SGreg Roach            $tree->setPreference('MEDIA_DIRECTORY', $MEDIA_DIRECTORY);
137590a0393SGreg Roach        }
1386fd01894SGreg Roach
139*748dbe15SGreg Roach        $gedcom = Validator::parsedBody($request)->string('gedcom');
140590a0393SGreg Roach        $url    = route(ManageTrees::class, ['tree' => $tree->name()]);
1416fd01894SGreg Roach
142590a0393SGreg Roach        if (Auth::isAdmin() && $gedcom !== '' && $gedcom !== $tree->name()) {
1436fd01894SGreg Roach            try {
1446fd01894SGreg Roach                DB::table('gedcom')
1456fd01894SGreg Roach                    ->where('gedcom_id', '=', $tree->id())
1466fd01894SGreg Roach                    ->update(['gedcom_name' => $gedcom]);
1476fd01894SGreg Roach
148590a0393SGreg Roach                // Did we rename the default tree?
1496fd01894SGreg Roach                DB::table('site_setting')
1506fd01894SGreg Roach                    ->where('setting_name', '=', 'DEFAULT_GEDCOM')
1516fd01894SGreg Roach                    ->where('setting_value', '=', $tree->name())
1526fd01894SGreg Roach                    ->update(['setting_value' => $gedcom]);
153590a0393SGreg Roach
154590a0393SGreg Roach                $url = route(ManageTrees::class, ['tree' => $gedcom]);
155fa5cbab5SGreg Roach            } catch (PDOException $ex) {
1566fd01894SGreg Roach                // Probably a duplicate name.
1576fd01894SGreg Roach            }
1586fd01894SGreg Roach        }
1596fd01894SGreg Roach
1606fd01894SGreg Roach        FlashMessages::addMessage(I18N::translate('The preferences for the family tree “%s” have been updated.', e($tree->title())), 'success');
1616fd01894SGreg Roach
1626fd01894SGreg Roach        // Coming soon...
163*748dbe15SGreg Roach        $all_trees = Validator::parsedBody($request)->boolean('all_trees', false);
164*748dbe15SGreg Roach        $new_trees = Validator::parsedBody($request)->boolean('new_trees', false);
1656fd01894SGreg Roach
166*748dbe15SGreg Roach        if ($all_trees) {
1676fd01894SGreg Roach            FlashMessages::addMessage(I18N::translate('The preferences for all family trees have been updated.'), 'success');
1686fd01894SGreg Roach        }
1696fd01894SGreg Roach
170*748dbe15SGreg Roach        if ($new_trees) {
1716fd01894SGreg Roach            FlashMessages::addMessage(I18N::translate('The preferences for new family trees have been updated.'), 'success');
1726fd01894SGreg Roach        }
1736fd01894SGreg Roach
1746fd01894SGreg Roach        return redirect($url);
1756fd01894SGreg Roach    }
1766fd01894SGreg Roach}
177