xref: /webtrees/app/Http/RequestHandlers/TreePreferencesAction.php (revision d11be7027e34e3121be11cc025421873364403f9)
16fd01894SGreg Roach<?php
26fd01894SGreg Roach
36fd01894SGreg Roach/**
46fd01894SGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 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"
56748dbe15SGreg Roach        $calendar_format_0           = Validator::parsedBody($request)->string('CALENDAR_FORMAT0');
57748dbe15SGreg Roach        $calendar_format_1           = Validator::parsedBody($request)->string('CALENDAR_FORMAT1');
58748dbe15SGreg Roach        $calendar_format             = implode('_and_', array_unique([$calendar_format_0, $calendar_format_1]));
59748dbe15SGreg Roach        $chart_box_tags              = Validator::parsedBody($request)->array('CHART_BOX_TAGS');
600ca85dd0SGreg Roach        $contact_user_id             = Validator::parsedBody($request)->integer('CONTACT_USER_ID', 0);
61748dbe15SGreg Roach        $expand_notes                = Validator::parsedBody($request)->boolean('EXPAND_NOTES');
62748dbe15SGreg Roach        $expand_sources              = Validator::parsedBody($request)->boolean('EXPAND_SOURCES');
63748dbe15SGreg Roach        $fam_facts_quick             = Validator::parsedBody($request)->array('FAM_FACTS_QUICK');
64748dbe15SGreg Roach        $format_text                 = Validator::parsedBody($request)->string('FORMAT_TEXT');
65748dbe15SGreg Roach        $generate_uuids              = Validator::parsedBody($request)->boolean('GENERATE_UIDS');
66748dbe15SGreg Roach        $hide_gedcom_errors          = Validator::parsedBody($request)->boolean('HIDE_GEDCOM_ERRORS');
67748dbe15SGreg Roach        $indi_facts_quick            = Validator::parsedBody($request)->array('INDI_FACTS_QUICK');
68748dbe15SGreg Roach        $media_upload                = Validator::parsedBody($request)->integer('MEDIA_UPLOAD');
69748dbe15SGreg Roach        $meta_description            = Validator::parsedBody($request)->string('META_DESCRIPTION');
70748dbe15SGreg Roach        $meta_title                  = Validator::parsedBody($request)->string('META_TITLE');
71748dbe15SGreg Roach        $no_update_chan              = Validator::parsedBody($request)->boolean('NO_UPDATE_CHAN');
72748dbe15SGreg Roach        $pedigree_root_id            = Validator::parsedBody($request)->string('PEDIGREE_ROOT_ID');
73748dbe15SGreg Roach        $quick_required_facts        = Validator::parsedBody($request)->array('QUICK_REQUIRED_FACTS');
74748dbe15SGreg Roach        $quick_required_famfacts     = Validator::parsedBody($request)->array('QUICK_REQUIRED_FAMFACTS');
75748dbe15SGreg Roach        $show_counter                = Validator::parsedBody($request)->boolean('SHOW_COUNTER');
76748dbe15SGreg Roach        $show_est_list_dates         = Validator::parsedBody($request)->boolean('SHOW_EST_LIST_DATES');
77748dbe15SGreg Roach        $show_fact_icons             = Validator::parsedBody($request)->boolean('SHOW_FACT_ICONS');
78748dbe15SGreg Roach        $show_gedcom_record          = Validator::parsedBody($request)->boolean('SHOW_GEDCOM_RECORD');
79748dbe15SGreg Roach        $show_highlight_images       = Validator::parsedBody($request)->boolean('SHOW_HIGHLIGHT_IMAGES');
80748dbe15SGreg Roach        $show_last_change            = Validator::parsedBody($request)->boolean('SHOW_LAST_CHANGE');
81748dbe15SGreg Roach        $show_media_download         = Validator::parsedBody($request)->integer('SHOW_MEDIA_DOWNLOAD');
82748dbe15SGreg Roach        $show_no_watermark           = Validator::parsedBody($request)->integer('SHOW_NO_WATERMARK');
83748dbe15SGreg Roach        $show_parents_age            = Validator::parsedBody($request)->boolean('SHOW_PARENTS_AGE');
84748dbe15SGreg Roach        $show_pedigree_places        = Validator::parsedBody($request)->integer('SHOW_PEDIGREE_PLACES');
85748dbe15SGreg Roach        $show_pedigree_places_suffix = Validator::parsedBody($request)->integer('SHOW_PEDIGREE_PLACES_SUFFIX');
86748dbe15SGreg Roach        $show_relatives_events       = Validator::parsedBody($request)->array('SHOW_RELATIVES_EVENTS');
87748dbe15SGreg Roach        $sublist_trigger_i           = Validator::parsedBody($request)->integer('SUBLIST_TRIGGER_I');
88748dbe15SGreg Roach        $surname_list_style          = Validator::parsedBody($request)->string('SURNAME_LIST_STYLE');
89748dbe15SGreg Roach        $surname_tradition           = Validator::parsedBody($request)->string('SURNAME_TRADITION');
90748dbe15SGreg Roach        $use_silhouette              = Validator::parsedBody($request)->boolean('USE_SILHOUETTE');
910ca85dd0SGreg Roach        $webmaster_user_id           = Validator::parsedBody($request)->integer('WEBMASTER_USER_ID', 0);
92748dbe15SGreg Roach        $title                       = Validator::parsedBody($request)->string('title');
93748dbe15SGreg Roach
940ca85dd0SGreg Roach        $contact_user_id   = $contact_user_id === 0 ? '' : (string) $contact_user_id;
950ca85dd0SGreg Roach        $webmaster_user_id = $webmaster_user_id === 0 ? '' : (string) $webmaster_user_id;
960ca85dd0SGreg Roach
97748dbe15SGreg Roach        $tree->setPreference('CALENDAR_FORMAT', $calendar_format);
98748dbe15SGreg Roach        $tree->setPreference('CHART_BOX_TAGS', implode(',', $chart_box_tags));
99748dbe15SGreg Roach        $tree->setPreference('CONTACT_USER_ID', $contact_user_id);
100748dbe15SGreg Roach        $tree->setPreference('EXPAND_NOTES', (string) $expand_notes);
101748dbe15SGreg Roach        $tree->setPreference('EXPAND_SOURCES', (string) $expand_sources);
102748dbe15SGreg Roach        $tree->setPreference('FAM_FACTS_QUICK', implode(',', $fam_facts_quick));
103748dbe15SGreg Roach        $tree->setPreference('FORMAT_TEXT', $format_text);
104748dbe15SGreg Roach        $tree->setPreference('GENERATE_UIDS', (string) $generate_uuids);
105748dbe15SGreg Roach        $tree->setPreference('HIDE_GEDCOM_ERRORS', (string) $hide_gedcom_errors);
106748dbe15SGreg Roach        $tree->setPreference('INDI_FACTS_QUICK', implode(',', $indi_facts_quick));
107748dbe15SGreg Roach        $tree->setPreference('MEDIA_UPLOAD', (string) $media_upload);
108748dbe15SGreg Roach        $tree->setPreference('META_DESCRIPTION', $meta_description);
109748dbe15SGreg Roach        $tree->setPreference('META_TITLE', $meta_title);
110748dbe15SGreg Roach        $tree->setPreference('NO_UPDATE_CHAN', (string) $no_update_chan);
111748dbe15SGreg Roach        $tree->setPreference('PEDIGREE_ROOT_ID', $pedigree_root_id);
112748dbe15SGreg Roach        $tree->setPreference('QUICK_REQUIRED_FACTS', implode(',', $quick_required_facts));
113748dbe15SGreg Roach        $tree->setPreference('QUICK_REQUIRED_FAMFACTS', implode(',', $quick_required_famfacts));
114748dbe15SGreg Roach        $tree->setPreference('SHOW_COUNTER', (string) $show_counter);
115748dbe15SGreg Roach        $tree->setPreference('SHOW_EST_LIST_DATES', (string) $show_est_list_dates);
116748dbe15SGreg Roach        $tree->setPreference('SHOW_FACT_ICONS', (string) $show_fact_icons);
117748dbe15SGreg Roach        $tree->setPreference('SHOW_GEDCOM_RECORD', (string) $show_gedcom_record);
118748dbe15SGreg Roach        $tree->setPreference('SHOW_HIGHLIGHT_IMAGES', (string) $show_highlight_images);
119748dbe15SGreg Roach        $tree->setPreference('SHOW_LAST_CHANGE', (string) $show_last_change);
120748dbe15SGreg Roach        $tree->setPreference('SHOW_MEDIA_DOWNLOAD', (string) $show_media_download);
121748dbe15SGreg Roach        $tree->setPreference('SHOW_NO_WATERMARK', (string) $show_no_watermark);
122748dbe15SGreg Roach        $tree->setPreference('SHOW_PARENTS_AGE', (string) $show_parents_age);
123748dbe15SGreg Roach        $tree->setPreference('SHOW_PEDIGREE_PLACES', (string) $show_pedigree_places);
124748dbe15SGreg Roach        $tree->setPreference('SHOW_PEDIGREE_PLACES_SUFFIX', (string) $show_pedigree_places_suffix);
125748dbe15SGreg Roach        $tree->setPreference('SHOW_RELATIVES_EVENTS', implode(',', $show_relatives_events));
126748dbe15SGreg Roach        $tree->setPreference('SUBLIST_TRIGGER_I', (string) $sublist_trigger_i);
127748dbe15SGreg Roach        $tree->setPreference('SURNAME_LIST_STYLE', $surname_list_style);
128748dbe15SGreg Roach        $tree->setPreference('SURNAME_TRADITION', $surname_tradition);
129748dbe15SGreg Roach        $tree->setPreference('USE_SILHOUETTE', (string) $use_silhouette);
130748dbe15SGreg Roach        $tree->setPreference('WEBMASTER_USER_ID', (string) $webmaster_user_id);
131748dbe15SGreg Roach        $tree->setPreference('title', $title);
1326fd01894SGreg Roach
133590a0393SGreg Roach        if (Auth::isAdmin()) {
1346fd01894SGreg Roach            // Only accept valid folders for MEDIA_DIRECTORY
135748dbe15SGreg Roach            $MEDIA_DIRECTORY = Validator::parsedBody($request)->string('MEDIA_DIRECTORY');
1366fd01894SGreg Roach            $MEDIA_DIRECTORY = preg_replace('/[:\/\\\\]+/', '/', $MEDIA_DIRECTORY);
1376fd01894SGreg Roach            $MEDIA_DIRECTORY = trim($MEDIA_DIRECTORY, '/') . '/';
1386fd01894SGreg Roach
1396fd01894SGreg Roach            $tree->setPreference('MEDIA_DIRECTORY', $MEDIA_DIRECTORY);
140590a0393SGreg Roach        }
1416fd01894SGreg Roach
142748dbe15SGreg Roach        $gedcom = Validator::parsedBody($request)->string('gedcom');
143590a0393SGreg Roach        $url    = route(ManageTrees::class, ['tree' => $tree->name()]);
1446fd01894SGreg Roach
145590a0393SGreg Roach        if (Auth::isAdmin() && $gedcom !== '' && $gedcom !== $tree->name()) {
1466fd01894SGreg Roach            try {
1476fd01894SGreg Roach                DB::table('gedcom')
1486fd01894SGreg Roach                    ->where('gedcom_id', '=', $tree->id())
1496fd01894SGreg Roach                    ->update(['gedcom_name' => $gedcom]);
1506fd01894SGreg Roach
151590a0393SGreg Roach                // Did we rename the default tree?
1526fd01894SGreg Roach                DB::table('site_setting')
1536fd01894SGreg Roach                    ->where('setting_name', '=', 'DEFAULT_GEDCOM')
1546fd01894SGreg Roach                    ->where('setting_value', '=', $tree->name())
1556fd01894SGreg Roach                    ->update(['setting_value' => $gedcom]);
156590a0393SGreg Roach
157590a0393SGreg Roach                $url = route(ManageTrees::class, ['tree' => $gedcom]);
15828d026adSGreg Roach            } catch (PDOException) {
1596fd01894SGreg Roach                // Probably a duplicate name.
1606fd01894SGreg Roach            }
1616fd01894SGreg Roach        }
1626fd01894SGreg Roach
1636fd01894SGreg Roach        FlashMessages::addMessage(I18N::translate('The preferences for the family tree “%s” have been updated.', e($tree->title())), 'success');
1646fd01894SGreg Roach
1656fd01894SGreg Roach        // Coming soon...
166748dbe15SGreg Roach        $all_trees = Validator::parsedBody($request)->boolean('all_trees', false);
167748dbe15SGreg Roach        $new_trees = Validator::parsedBody($request)->boolean('new_trees', false);
1686fd01894SGreg Roach
169748dbe15SGreg Roach        if ($all_trees) {
1706fd01894SGreg Roach            FlashMessages::addMessage(I18N::translate('The preferences for all family trees have been updated.'), 'success');
1716fd01894SGreg Roach        }
1726fd01894SGreg Roach
173748dbe15SGreg Roach        if ($new_trees) {
1746fd01894SGreg Roach            FlashMessages::addMessage(I18N::translate('The preferences for new family trees have been updated.'), 'success');
1756fd01894SGreg Roach        }
1766fd01894SGreg Roach
1776fd01894SGreg Roach        return redirect($url);
1786fd01894SGreg Roach    }
1796fd01894SGreg Roach}
180