xref: /webtrees/app/Http/RequestHandlers/TreePreferencesAction.php (revision 590a039372e5ccf5df82fd60d84123d78913d833)
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
226fd01894SGreg Roachuse Exception;
23*590a0393SGreg Roachuse Fisharebest\Webtrees\Auth;
246fd01894SGreg Roachuse Fisharebest\Webtrees\FlashMessages;
256fd01894SGreg Roachuse Fisharebest\Webtrees\I18N;
266fd01894SGreg Roachuse Fisharebest\Webtrees\Tree;
276fd01894SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
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        $tree->setPreference('ADVANCED_NAME_FACTS', implode(',', $params['ADVANCED_NAME_FACTS'] ?? []));
596fd01894SGreg Roach        $tree->setPreference('ADVANCED_PLAC_FACTS', implode(',', $params['ADVANCED_PLAC_FACTS'] ?? []));
606fd01894SGreg Roach        // For backwards compatibility with webtrees 1.x we store the two calendar formats in one variable
616fd01894SGreg Roach        // e.g. "gregorian_and_jewish"
626fd01894SGreg Roach        $tree->setPreference('CALENDAR_FORMAT', implode('_and_', array_unique([
636fd01894SGreg Roach            $params['CALENDAR_FORMAT0'] ?? 'none',
646fd01894SGreg Roach            $params['CALENDAR_FORMAT1'] ?? 'none',
656fd01894SGreg Roach        ])));
666fd01894SGreg Roach        $tree->setPreference('CHART_BOX_TAGS', implode(',', $params['CHART_BOX_TAGS'] ?? []));
676fd01894SGreg Roach        $tree->setPreference('CONTACT_USER_ID', $params['CONTACT_USER_ID'] ?? '');
686fd01894SGreg Roach        $tree->setPreference('EXPAND_NOTES', $params['EXPAND_NOTES'] ?? '');
696fd01894SGreg Roach        $tree->setPreference('EXPAND_SOURCES', $params['EXPAND_SOURCES'] ?? '');
706fd01894SGreg Roach        $tree->setPreference('FAM_FACTS_ADD', implode(',', $params['FAM_FACTS_ADD'] ?? []));
716fd01894SGreg Roach        $tree->setPreference('FAM_FACTS_QUICK', implode(',', $params['FAM_FACTS_QUICK'] ?? []));
726fd01894SGreg Roach        $tree->setPreference('FAM_FACTS_UNIQUE', implode(',', $params['FAM_FACTS_UNIQUE'] ?? []));
736fd01894SGreg Roach        $tree->setPreference('FULL_SOURCES', $params['FULL_SOURCES'] ?? '');
746fd01894SGreg Roach        $tree->setPreference('FORMAT_TEXT', $params['FORMAT_TEXT'] ?? '');
756fd01894SGreg Roach        $tree->setPreference('GENERATE_UIDS', $params['GENERATE_UIDS'] ?? '');
766fd01894SGreg Roach        $tree->setPreference('HIDE_GEDCOM_ERRORS', $params['HIDE_GEDCOM_ERRORS'] ?? '');
776fd01894SGreg Roach        $tree->setPreference('INDI_FACTS_ADD', implode(',', $params['INDI_FACTS_ADD'] ?? []));
786fd01894SGreg Roach        $tree->setPreference('INDI_FACTS_QUICK', implode(',', $params['INDI_FACTS_QUICK'] ?? []));
796fd01894SGreg Roach        $tree->setPreference('INDI_FACTS_UNIQUE', implode(',', $params['INDI_FACTS_UNIQUE'] ?? []));
806fd01894SGreg Roach        $tree->setPreference('MEDIA_UPLOAD', $params['MEDIA_UPLOAD'] ?? '');
816fd01894SGreg Roach        $tree->setPreference('META_DESCRIPTION', $params['META_DESCRIPTION'] ?? '');
826fd01894SGreg Roach        $tree->setPreference('META_TITLE', $params['META_TITLE'] ?? '');
836fd01894SGreg Roach        $tree->setPreference('NO_UPDATE_CHAN', $params['NO_UPDATE_CHAN'] ?? '');
846fd01894SGreg Roach        $tree->setPreference('PEDIGREE_ROOT_ID', $params['PEDIGREE_ROOT_ID'] ?? '');
856fd01894SGreg Roach        $tree->setPreference('PREFER_LEVEL2_SOURCES', $params['PREFER_LEVEL2_SOURCES'] ?? '');
866fd01894SGreg Roach        $tree->setPreference('QUICK_REQUIRED_FACTS', implode(',', $params['QUICK_REQUIRED_FACTS'] ?? []));
876fd01894SGreg Roach        $tree->setPreference('QUICK_REQUIRED_FAMFACTS', implode(',', $params['QUICK_REQUIRED_FAMFACTS'] ?? []));
886fd01894SGreg Roach        $tree->setPreference('REPO_FACTS_ADD', implode(',', $params['REPO_FACTS_ADD'] ?? []));
896fd01894SGreg Roach        $tree->setPreference('REPO_FACTS_QUICK', implode(',', $params['REPO_FACTS_QUICK'] ?? []));
906fd01894SGreg Roach        $tree->setPreference('REPO_FACTS_UNIQUE', implode(',', $params['REPO_FACTS_UNIQUE'] ?? []));
916fd01894SGreg Roach        $tree->setPreference('SHOW_COUNTER', $params['SHOW_COUNTER'] ?? '');
926fd01894SGreg Roach        $tree->setPreference('SHOW_EST_LIST_DATES', $params['SHOW_EST_LIST_DATES'] ?? '');
936fd01894SGreg Roach        $tree->setPreference('SHOW_FACT_ICONS', $params['SHOW_FACT_ICONS'] ?? '');
946fd01894SGreg Roach        $tree->setPreference('SHOW_GEDCOM_RECORD', $params['SHOW_GEDCOM_RECORD'] ?? '');
956fd01894SGreg Roach        $tree->setPreference('SHOW_HIGHLIGHT_IMAGES', $params['SHOW_HIGHLIGHT_IMAGES'] ?? '');
966fd01894SGreg Roach        $tree->setPreference('SHOW_LAST_CHANGE', $params['SHOW_LAST_CHANGE'] ?? '');
976fd01894SGreg Roach        $tree->setPreference('SHOW_MEDIA_DOWNLOAD', $params['SHOW_MEDIA_DOWNLOAD'] ?? '');
986fd01894SGreg Roach        $tree->setPreference('SHOW_NO_WATERMARK', $params['SHOW_NO_WATERMARK'] ?? '');
996fd01894SGreg Roach        $tree->setPreference('SHOW_PARENTS_AGE', $params['SHOW_PARENTS_AGE'] ?? '');
1006fd01894SGreg Roach        $tree->setPreference('SHOW_PEDIGREE_PLACES', $params['SHOW_PEDIGREE_PLACES'] ?? '');
1016fd01894SGreg Roach        $tree->setPreference('SHOW_PEDIGREE_PLACES_SUFFIX', $params['SHOW_PEDIGREE_PLACES_SUFFIX'] ?? '');
1026fd01894SGreg Roach        $tree->setPreference('SHOW_RELATIVES_EVENTS', implode(',', $params['SHOW_RELATIVES_EVENTS'] ?? []));
1036fd01894SGreg Roach        $tree->setPreference('SOUR_FACTS_ADD', implode(',', $params['SOUR_FACTS_ADD'] ?? []));
1046fd01894SGreg Roach        $tree->setPreference('SOUR_FACTS_QUICK', implode(',', $params['SOUR_FACTS_QUICK'] ?? []));
1056fd01894SGreg Roach        $tree->setPreference('SOUR_FACTS_UNIQUE', implode(',', $params['SOUR_FACTS_UNIQUE'] ?? []));
1066fd01894SGreg Roach        $tree->setPreference('SUBLIST_TRIGGER_I', $params['SUBLIST_TRIGGER_I'] ?? '200');
1076fd01894SGreg Roach        $tree->setPreference('SURNAME_LIST_STYLE', $params['SURNAME_LIST_STYLE'] ?? '');
1086fd01894SGreg Roach        $tree->setPreference('SURNAME_TRADITION', $params['SURNAME_TRADITION'] ?? '');
1096fd01894SGreg Roach        $tree->setPreference('USE_SILHOUETTE', $params['USE_SILHOUETTE'] ?? '');
1106fd01894SGreg Roach        $tree->setPreference('WEBMASTER_USER_ID', $params['WEBMASTER_USER_ID'] ?? '');
1116fd01894SGreg Roach        $tree->setPreference('title', $params['title'] ?? '');
1126fd01894SGreg Roach
113*590a0393SGreg Roach        if (Auth::isAdmin()) {
1146fd01894SGreg Roach            // Only accept valid folders for MEDIA_DIRECTORY
1156fd01894SGreg Roach            $MEDIA_DIRECTORY = $params['MEDIA_DIRECTORY'] ?? '';
1166fd01894SGreg Roach            $MEDIA_DIRECTORY = preg_replace('/[:\/\\\\]+/', '/', $MEDIA_DIRECTORY);
1176fd01894SGreg Roach            $MEDIA_DIRECTORY = trim($MEDIA_DIRECTORY, '/') . '/';
1186fd01894SGreg Roach
1196fd01894SGreg Roach            $tree->setPreference('MEDIA_DIRECTORY', $MEDIA_DIRECTORY);
120*590a0393SGreg Roach        }
1216fd01894SGreg Roach
1226fd01894SGreg Roach        $gedcom = $params['gedcom'] ?? '';
123*590a0393SGreg Roach        $url    = route(ManageTrees::class, ['tree' => $tree->name()]);
1246fd01894SGreg Roach
125*590a0393SGreg Roach        if (Auth::isAdmin() && $gedcom !== '' && $gedcom !== $tree->name()) {
1266fd01894SGreg Roach            try {
1276fd01894SGreg Roach                DB::table('gedcom')
1286fd01894SGreg Roach                    ->where('gedcom_id', '=', $tree->id())
1296fd01894SGreg Roach                    ->update(['gedcom_name' => $gedcom]);
1306fd01894SGreg Roach
131*590a0393SGreg Roach                // Did we rename the default tree?
1326fd01894SGreg Roach                DB::table('site_setting')
1336fd01894SGreg Roach                    ->where('setting_name', '=', 'DEFAULT_GEDCOM')
1346fd01894SGreg Roach                    ->where('setting_value', '=', $tree->name())
1356fd01894SGreg Roach                    ->update(['setting_value' => $gedcom]);
136*590a0393SGreg Roach
137*590a0393SGreg Roach                $url = route(ManageTrees::class, ['tree' => $gedcom]);
1386fd01894SGreg Roach            } catch (Exception $ex) {
1396fd01894SGreg Roach                // Probably a duplicate name.
1406fd01894SGreg Roach            }
1416fd01894SGreg Roach        }
1426fd01894SGreg Roach
1436fd01894SGreg Roach        FlashMessages::addMessage(I18N::translate('The preferences for the family tree “%s” have been updated.', e($tree->title())), 'success');
1446fd01894SGreg Roach
1456fd01894SGreg Roach        // Coming soon...
1466fd01894SGreg Roach        $all_trees = $params['all_trees'] ?? '';
1476fd01894SGreg Roach        $new_trees = $params['new_trees'] ?? '';
1486fd01894SGreg Roach
1496fd01894SGreg Roach        if ($all_trees === 'on') {
1506fd01894SGreg Roach            FlashMessages::addMessage(I18N::translate('The preferences for all family trees have been updated.'), 'success');
1516fd01894SGreg Roach        }
1526fd01894SGreg Roach
1536fd01894SGreg Roach        if ($new_trees === 'on') {
1546fd01894SGreg Roach            FlashMessages::addMessage(I18N::translate('The preferences for new family trees have been updated.'), 'success');
1556fd01894SGreg Roach        }
1566fd01894SGreg Roach
1576fd01894SGreg Roach        return redirect($url);
1586fd01894SGreg Roach    }
1596fd01894SGreg Roach}
160