xref: /webtrees/app/Http/RequestHandlers/TreePreferencesAction.php (revision fa5cbab582a76bfbb910725460daa14f29daa561)
16fd01894SGreg Roach<?php
26fd01894SGreg Roach
36fd01894SGreg Roach/**
46fd01894SGreg Roach * webtrees: online genealogy
589f7189bSGreg Roach * Copyright (C) 2021 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;
256fd01894SGreg Roachuse Fisharebest\Webtrees\Tree;
266fd01894SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
27*fa5cbab5SGreg 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 assert;
346fd01894SGreg Roachuse function e;
356fd01894SGreg Roachuse function implode;
366fd01894SGreg Roachuse function preg_replace;
376fd01894SGreg Roachuse function redirect;
386fd01894SGreg Roachuse function route;
396fd01894SGreg Roachuse function trim;
406fd01894SGreg Roach
416fd01894SGreg Roach/**
426fd01894SGreg Roach * Edit the tree preferences.
436fd01894SGreg Roach */
446fd01894SGreg Roachclass TreePreferencesAction implements RequestHandlerInterface
456fd01894SGreg Roach{
466fd01894SGreg Roach    /**
476fd01894SGreg Roach     * @param ServerRequestInterface $request
486fd01894SGreg Roach     *
496fd01894SGreg Roach     * @return ResponseInterface
506fd01894SGreg Roach     */
516fd01894SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
526fd01894SGreg Roach    {
536fd01894SGreg Roach        $tree = $request->getAttribute('tree');
546fd01894SGreg Roach        assert($tree instanceof Tree);
556fd01894SGreg Roach
566fd01894SGreg Roach        $params = (array) $request->getParsedBody();
576fd01894SGreg Roach
586fd01894SGreg Roach        // For backwards compatibility with webtrees 1.x we store the two calendar formats in one variable
596fd01894SGreg Roach        // e.g. "gregorian_and_jewish"
606fd01894SGreg Roach        $tree->setPreference('CALENDAR_FORMAT', implode('_and_', array_unique([
616fd01894SGreg Roach            $params['CALENDAR_FORMAT0'] ?? 'none',
626fd01894SGreg Roach            $params['CALENDAR_FORMAT1'] ?? 'none',
636fd01894SGreg Roach        ])));
646fd01894SGreg Roach        $tree->setPreference('CHART_BOX_TAGS', implode(',', $params['CHART_BOX_TAGS'] ?? []));
656fd01894SGreg Roach        $tree->setPreference('CONTACT_USER_ID', $params['CONTACT_USER_ID'] ?? '');
666fd01894SGreg Roach        $tree->setPreference('EXPAND_NOTES', $params['EXPAND_NOTES'] ?? '');
676fd01894SGreg Roach        $tree->setPreference('EXPAND_SOURCES', $params['EXPAND_SOURCES'] ?? '');
686fd01894SGreg Roach        $tree->setPreference('FAM_FACTS_QUICK', implode(',', $params['FAM_FACTS_QUICK'] ?? []));
696fd01894SGreg Roach        $tree->setPreference('FULL_SOURCES', $params['FULL_SOURCES'] ?? '');
706fd01894SGreg Roach        $tree->setPreference('FORMAT_TEXT', $params['FORMAT_TEXT'] ?? '');
716fd01894SGreg Roach        $tree->setPreference('GENERATE_UIDS', $params['GENERATE_UIDS'] ?? '');
726fd01894SGreg Roach        $tree->setPreference('HIDE_GEDCOM_ERRORS', $params['HIDE_GEDCOM_ERRORS'] ?? '');
736fd01894SGreg Roach        $tree->setPreference('INDI_FACTS_QUICK', implode(',', $params['INDI_FACTS_QUICK'] ?? []));
746fd01894SGreg Roach        $tree->setPreference('MEDIA_UPLOAD', $params['MEDIA_UPLOAD'] ?? '');
756fd01894SGreg Roach        $tree->setPreference('META_DESCRIPTION', $params['META_DESCRIPTION'] ?? '');
766fd01894SGreg Roach        $tree->setPreference('META_TITLE', $params['META_TITLE'] ?? '');
776fd01894SGreg Roach        $tree->setPreference('NO_UPDATE_CHAN', $params['NO_UPDATE_CHAN'] ?? '');
786fd01894SGreg Roach        $tree->setPreference('PEDIGREE_ROOT_ID', $params['PEDIGREE_ROOT_ID'] ?? '');
796fd01894SGreg Roach        $tree->setPreference('PREFER_LEVEL2_SOURCES', $params['PREFER_LEVEL2_SOURCES'] ?? '');
806fd01894SGreg Roach        $tree->setPreference('QUICK_REQUIRED_FACTS', implode(',', $params['QUICK_REQUIRED_FACTS'] ?? []));
816fd01894SGreg Roach        $tree->setPreference('QUICK_REQUIRED_FAMFACTS', implode(',', $params['QUICK_REQUIRED_FAMFACTS'] ?? []));
826fd01894SGreg Roach        $tree->setPreference('SHOW_COUNTER', $params['SHOW_COUNTER'] ?? '');
836fd01894SGreg Roach        $tree->setPreference('SHOW_EST_LIST_DATES', $params['SHOW_EST_LIST_DATES'] ?? '');
846fd01894SGreg Roach        $tree->setPreference('SHOW_FACT_ICONS', $params['SHOW_FACT_ICONS'] ?? '');
856fd01894SGreg Roach        $tree->setPreference('SHOW_GEDCOM_RECORD', $params['SHOW_GEDCOM_RECORD'] ?? '');
866fd01894SGreg Roach        $tree->setPreference('SHOW_HIGHLIGHT_IMAGES', $params['SHOW_HIGHLIGHT_IMAGES'] ?? '');
876fd01894SGreg Roach        $tree->setPreference('SHOW_LAST_CHANGE', $params['SHOW_LAST_CHANGE'] ?? '');
886fd01894SGreg Roach        $tree->setPreference('SHOW_MEDIA_DOWNLOAD', $params['SHOW_MEDIA_DOWNLOAD'] ?? '');
896fd01894SGreg Roach        $tree->setPreference('SHOW_NO_WATERMARK', $params['SHOW_NO_WATERMARK'] ?? '');
906fd01894SGreg Roach        $tree->setPreference('SHOW_PARENTS_AGE', $params['SHOW_PARENTS_AGE'] ?? '');
916fd01894SGreg Roach        $tree->setPreference('SHOW_PEDIGREE_PLACES', $params['SHOW_PEDIGREE_PLACES'] ?? '');
926fd01894SGreg Roach        $tree->setPreference('SHOW_PEDIGREE_PLACES_SUFFIX', $params['SHOW_PEDIGREE_PLACES_SUFFIX'] ?? '');
936fd01894SGreg Roach        $tree->setPreference('SHOW_RELATIVES_EVENTS', implode(',', $params['SHOW_RELATIVES_EVENTS'] ?? []));
946fd01894SGreg Roach        $tree->setPreference('SUBLIST_TRIGGER_I', $params['SUBLIST_TRIGGER_I'] ?? '200');
956fd01894SGreg Roach        $tree->setPreference('SURNAME_LIST_STYLE', $params['SURNAME_LIST_STYLE'] ?? '');
966fd01894SGreg Roach        $tree->setPreference('SURNAME_TRADITION', $params['SURNAME_TRADITION'] ?? '');
976fd01894SGreg Roach        $tree->setPreference('USE_SILHOUETTE', $params['USE_SILHOUETTE'] ?? '');
986fd01894SGreg Roach        $tree->setPreference('WEBMASTER_USER_ID', $params['WEBMASTER_USER_ID'] ?? '');
996fd01894SGreg Roach        $tree->setPreference('title', $params['title'] ?? '');
1006fd01894SGreg Roach
101590a0393SGreg Roach        if (Auth::isAdmin()) {
1026fd01894SGreg Roach            // Only accept valid folders for MEDIA_DIRECTORY
1036fd01894SGreg Roach            $MEDIA_DIRECTORY = $params['MEDIA_DIRECTORY'] ?? '';
1046fd01894SGreg Roach            $MEDIA_DIRECTORY = preg_replace('/[:\/\\\\]+/', '/', $MEDIA_DIRECTORY);
1056fd01894SGreg Roach            $MEDIA_DIRECTORY = trim($MEDIA_DIRECTORY, '/') . '/';
1066fd01894SGreg Roach
1076fd01894SGreg Roach            $tree->setPreference('MEDIA_DIRECTORY', $MEDIA_DIRECTORY);
108590a0393SGreg Roach        }
1096fd01894SGreg Roach
1106fd01894SGreg Roach        $gedcom = $params['gedcom'] ?? '';
111590a0393SGreg Roach        $url    = route(ManageTrees::class, ['tree' => $tree->name()]);
1126fd01894SGreg Roach
113590a0393SGreg Roach        if (Auth::isAdmin() && $gedcom !== '' && $gedcom !== $tree->name()) {
1146fd01894SGreg Roach            try {
1156fd01894SGreg Roach                DB::table('gedcom')
1166fd01894SGreg Roach                    ->where('gedcom_id', '=', $tree->id())
1176fd01894SGreg Roach                    ->update(['gedcom_name' => $gedcom]);
1186fd01894SGreg Roach
119590a0393SGreg Roach                // Did we rename the default tree?
1206fd01894SGreg Roach                DB::table('site_setting')
1216fd01894SGreg Roach                    ->where('setting_name', '=', 'DEFAULT_GEDCOM')
1226fd01894SGreg Roach                    ->where('setting_value', '=', $tree->name())
1236fd01894SGreg Roach                    ->update(['setting_value' => $gedcom]);
124590a0393SGreg Roach
125590a0393SGreg Roach                $url = route(ManageTrees::class, ['tree' => $gedcom]);
126*fa5cbab5SGreg Roach            } catch (PDOException $ex) {
1276fd01894SGreg Roach                // Probably a duplicate name.
1286fd01894SGreg Roach            }
1296fd01894SGreg Roach        }
1306fd01894SGreg Roach
1316fd01894SGreg Roach        FlashMessages::addMessage(I18N::translate('The preferences for the family tree “%s” have been updated.', e($tree->title())), 'success');
1326fd01894SGreg Roach
1336fd01894SGreg Roach        // Coming soon...
1346fd01894SGreg Roach        $all_trees = $params['all_trees'] ?? '';
1356fd01894SGreg Roach        $new_trees = $params['new_trees'] ?? '';
1366fd01894SGreg Roach
1376fd01894SGreg Roach        if ($all_trees === 'on') {
1386fd01894SGreg Roach            FlashMessages::addMessage(I18N::translate('The preferences for all family trees have been updated.'), 'success');
1396fd01894SGreg Roach        }
1406fd01894SGreg Roach
1416fd01894SGreg Roach        if ($new_trees === 'on') {
1426fd01894SGreg Roach            FlashMessages::addMessage(I18N::translate('The preferences for new family trees have been updated.'), 'success');
1436fd01894SGreg Roach        }
1446fd01894SGreg Roach
1456fd01894SGreg Roach        return redirect($url);
1466fd01894SGreg Roach    }
1476fd01894SGreg Roach}
148