16fd01894SGreg Roach<?php 26fd01894SGreg Roach 36fd01894SGreg Roach/** 46fd01894SGreg Roach * webtrees: online genealogy 5*89f7189bSGreg 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 15*89f7189bSGreg 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; 236fd01894SGreg Roachuse Fisharebest\Webtrees\FlashMessages; 246fd01894SGreg Roachuse Fisharebest\Webtrees\I18N; 256fd01894SGreg Roachuse Fisharebest\Webtrees\Tree; 266fd01894SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 276fd01894SGreg Roachuse Psr\Http\Message\ResponseInterface; 286fd01894SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 296fd01894SGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 306fd01894SGreg Roach 316fd01894SGreg Roachuse function array_unique; 326fd01894SGreg Roachuse function assert; 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 { 526fd01894SGreg Roach $tree = $request->getAttribute('tree'); 536fd01894SGreg Roach assert($tree instanceof Tree); 546fd01894SGreg Roach 556fd01894SGreg Roach $params = (array) $request->getParsedBody(); 566fd01894SGreg Roach 576fd01894SGreg Roach $tree->setPreference('ADVANCED_NAME_FACTS', implode(',', $params['ADVANCED_NAME_FACTS'] ?? [])); 586fd01894SGreg Roach $tree->setPreference('ADVANCED_PLAC_FACTS', implode(',', $params['ADVANCED_PLAC_FACTS'] ?? [])); 596fd01894SGreg Roach // For backwards compatibility with webtrees 1.x we store the two calendar formats in one variable 606fd01894SGreg Roach // e.g. "gregorian_and_jewish" 616fd01894SGreg Roach $tree->setPreference('CALENDAR_FORMAT', implode('_and_', array_unique([ 626fd01894SGreg Roach $params['CALENDAR_FORMAT0'] ?? 'none', 636fd01894SGreg Roach $params['CALENDAR_FORMAT1'] ?? 'none', 646fd01894SGreg Roach ]))); 656fd01894SGreg Roach $tree->setPreference('CHART_BOX_TAGS', implode(',', $params['CHART_BOX_TAGS'] ?? [])); 666fd01894SGreg Roach $tree->setPreference('CONTACT_USER_ID', $params['CONTACT_USER_ID'] ?? ''); 676fd01894SGreg Roach $tree->setPreference('EXPAND_NOTES', $params['EXPAND_NOTES'] ?? ''); 686fd01894SGreg Roach $tree->setPreference('EXPAND_SOURCES', $params['EXPAND_SOURCES'] ?? ''); 696fd01894SGreg Roach $tree->setPreference('FAM_FACTS_ADD', implode(',', $params['FAM_FACTS_ADD'] ?? [])); 706fd01894SGreg Roach $tree->setPreference('FAM_FACTS_QUICK', implode(',', $params['FAM_FACTS_QUICK'] ?? [])); 716fd01894SGreg Roach $tree->setPreference('FAM_FACTS_UNIQUE', implode(',', $params['FAM_FACTS_UNIQUE'] ?? [])); 726fd01894SGreg Roach $tree->setPreference('FULL_SOURCES', $params['FULL_SOURCES'] ?? ''); 736fd01894SGreg Roach $tree->setPreference('FORMAT_TEXT', $params['FORMAT_TEXT'] ?? ''); 746fd01894SGreg Roach $tree->setPreference('GENERATE_UIDS', $params['GENERATE_UIDS'] ?? ''); 756fd01894SGreg Roach $tree->setPreference('HIDE_GEDCOM_ERRORS', $params['HIDE_GEDCOM_ERRORS'] ?? ''); 766fd01894SGreg Roach $tree->setPreference('INDI_FACTS_ADD', implode(',', $params['INDI_FACTS_ADD'] ?? [])); 776fd01894SGreg Roach $tree->setPreference('INDI_FACTS_QUICK', implode(',', $params['INDI_FACTS_QUICK'] ?? [])); 786fd01894SGreg Roach $tree->setPreference('INDI_FACTS_UNIQUE', implode(',', $params['INDI_FACTS_UNIQUE'] ?? [])); 796fd01894SGreg Roach $tree->setPreference('MEDIA_UPLOAD', $params['MEDIA_UPLOAD'] ?? ''); 806fd01894SGreg Roach $tree->setPreference('META_DESCRIPTION', $params['META_DESCRIPTION'] ?? ''); 816fd01894SGreg Roach $tree->setPreference('META_TITLE', $params['META_TITLE'] ?? ''); 826fd01894SGreg Roach $tree->setPreference('NO_UPDATE_CHAN', $params['NO_UPDATE_CHAN'] ?? ''); 836fd01894SGreg Roach $tree->setPreference('PEDIGREE_ROOT_ID', $params['PEDIGREE_ROOT_ID'] ?? ''); 846fd01894SGreg Roach $tree->setPreference('PREFER_LEVEL2_SOURCES', $params['PREFER_LEVEL2_SOURCES'] ?? ''); 856fd01894SGreg Roach $tree->setPreference('QUICK_REQUIRED_FACTS', implode(',', $params['QUICK_REQUIRED_FACTS'] ?? [])); 866fd01894SGreg Roach $tree->setPreference('QUICK_REQUIRED_FAMFACTS', implode(',', $params['QUICK_REQUIRED_FAMFACTS'] ?? [])); 876fd01894SGreg Roach $tree->setPreference('REPO_FACTS_ADD', implode(',', $params['REPO_FACTS_ADD'] ?? [])); 886fd01894SGreg Roach $tree->setPreference('REPO_FACTS_QUICK', implode(',', $params['REPO_FACTS_QUICK'] ?? [])); 896fd01894SGreg Roach $tree->setPreference('REPO_FACTS_UNIQUE', implode(',', $params['REPO_FACTS_UNIQUE'] ?? [])); 906fd01894SGreg Roach $tree->setPreference('SHOW_COUNTER', $params['SHOW_COUNTER'] ?? ''); 916fd01894SGreg Roach $tree->setPreference('SHOW_EST_LIST_DATES', $params['SHOW_EST_LIST_DATES'] ?? ''); 926fd01894SGreg Roach $tree->setPreference('SHOW_FACT_ICONS', $params['SHOW_FACT_ICONS'] ?? ''); 936fd01894SGreg Roach $tree->setPreference('SHOW_GEDCOM_RECORD', $params['SHOW_GEDCOM_RECORD'] ?? ''); 946fd01894SGreg Roach $tree->setPreference('SHOW_HIGHLIGHT_IMAGES', $params['SHOW_HIGHLIGHT_IMAGES'] ?? ''); 956fd01894SGreg Roach $tree->setPreference('SHOW_LAST_CHANGE', $params['SHOW_LAST_CHANGE'] ?? ''); 966fd01894SGreg Roach $tree->setPreference('SHOW_MEDIA_DOWNLOAD', $params['SHOW_MEDIA_DOWNLOAD'] ?? ''); 976fd01894SGreg Roach $tree->setPreference('SHOW_NO_WATERMARK', $params['SHOW_NO_WATERMARK'] ?? ''); 986fd01894SGreg Roach $tree->setPreference('SHOW_PARENTS_AGE', $params['SHOW_PARENTS_AGE'] ?? ''); 996fd01894SGreg Roach $tree->setPreference('SHOW_PEDIGREE_PLACES', $params['SHOW_PEDIGREE_PLACES'] ?? ''); 1006fd01894SGreg Roach $tree->setPreference('SHOW_PEDIGREE_PLACES_SUFFIX', $params['SHOW_PEDIGREE_PLACES_SUFFIX'] ?? ''); 1016fd01894SGreg Roach $tree->setPreference('SHOW_RELATIVES_EVENTS', implode(',', $params['SHOW_RELATIVES_EVENTS'] ?? [])); 1026fd01894SGreg Roach $tree->setPreference('SOUR_FACTS_ADD', implode(',', $params['SOUR_FACTS_ADD'] ?? [])); 1036fd01894SGreg Roach $tree->setPreference('SOUR_FACTS_QUICK', implode(',', $params['SOUR_FACTS_QUICK'] ?? [])); 1046fd01894SGreg Roach $tree->setPreference('SOUR_FACTS_UNIQUE', implode(',', $params['SOUR_FACTS_UNIQUE'] ?? [])); 1056fd01894SGreg Roach $tree->setPreference('SUBLIST_TRIGGER_I', $params['SUBLIST_TRIGGER_I'] ?? '200'); 1066fd01894SGreg Roach $tree->setPreference('SURNAME_LIST_STYLE', $params['SURNAME_LIST_STYLE'] ?? ''); 1076fd01894SGreg Roach $tree->setPreference('SURNAME_TRADITION', $params['SURNAME_TRADITION'] ?? ''); 1086fd01894SGreg Roach $tree->setPreference('USE_SILHOUETTE', $params['USE_SILHOUETTE'] ?? ''); 1096fd01894SGreg Roach $tree->setPreference('WEBMASTER_USER_ID', $params['WEBMASTER_USER_ID'] ?? ''); 1106fd01894SGreg Roach $tree->setPreference('title', $params['title'] ?? ''); 1116fd01894SGreg Roach 1126fd01894SGreg Roach // Only accept valid folders for MEDIA_DIRECTORY 1136fd01894SGreg Roach $MEDIA_DIRECTORY = $params['MEDIA_DIRECTORY'] ?? ''; 1146fd01894SGreg Roach $MEDIA_DIRECTORY = preg_replace('/[:\/\\\\]+/', '/', $MEDIA_DIRECTORY); 1156fd01894SGreg Roach $MEDIA_DIRECTORY = trim($MEDIA_DIRECTORY, '/') . '/'; 1166fd01894SGreg Roach 1176fd01894SGreg Roach $tree->setPreference('MEDIA_DIRECTORY', $MEDIA_DIRECTORY); 1186fd01894SGreg Roach 1196fd01894SGreg Roach $gedcom = $params['gedcom'] ?? ''; 1206fd01894SGreg Roach 1216fd01894SGreg Roach if ($gedcom !== '' && $gedcom !== $tree->name()) { 1226fd01894SGreg Roach try { 1236fd01894SGreg Roach DB::table('gedcom') 1246fd01894SGreg Roach ->where('gedcom_id', '=', $tree->id()) 1256fd01894SGreg Roach ->update(['gedcom_name' => $gedcom]); 1266fd01894SGreg Roach 1276fd01894SGreg Roach DB::table('site_setting') 1286fd01894SGreg Roach ->where('setting_name', '=', 'DEFAULT_GEDCOM') 1296fd01894SGreg Roach ->where('setting_value', '=', $tree->name()) 1306fd01894SGreg Roach ->update(['setting_value' => $gedcom]); 1316fd01894SGreg Roach } catch (Exception $ex) { 1326fd01894SGreg Roach // Probably a duplicate name. 1336fd01894SGreg Roach } 1346fd01894SGreg Roach } 1356fd01894SGreg Roach 1366fd01894SGreg Roach FlashMessages::addMessage(I18N::translate('The preferences for the family tree “%s” have been updated.', e($tree->title())), 'success'); 1376fd01894SGreg Roach 1386fd01894SGreg Roach // Coming soon... 1396fd01894SGreg Roach $all_trees = $params['all_trees'] ?? ''; 1406fd01894SGreg Roach $new_trees = $params['new_trees'] ?? ''; 1416fd01894SGreg Roach 1426fd01894SGreg Roach if ($all_trees === 'on') { 1436fd01894SGreg Roach FlashMessages::addMessage(I18N::translate('The preferences for all family trees have been updated.'), 'success'); 1446fd01894SGreg Roach } 1456fd01894SGreg Roach 1466fd01894SGreg Roach if ($new_trees === 'on') { 1476fd01894SGreg Roach FlashMessages::addMessage(I18N::translate('The preferences for new family trees have been updated.'), 'success'); 1486fd01894SGreg Roach } 1496fd01894SGreg Roach 150a8169445SGreg Roach $url = route(ManageTrees::class, ['tree' => $gedcom]); 1516fd01894SGreg Roach 1526fd01894SGreg Roach return redirect($url); 1536fd01894SGreg Roach } 1546fd01894SGreg Roach} 155