xref: /webtrees/app/Http/RequestHandlers/TreePreferencesAction.php (revision 6fd01894a78d321fac365dd0291a2fc52129fa03)
1*6fd01894SGreg Roach<?php
2*6fd01894SGreg Roach
3*6fd01894SGreg Roach/**
4*6fd01894SGreg Roach * webtrees: online genealogy
5*6fd01894SGreg Roach * Copyright (C) 2020 webtrees development team
6*6fd01894SGreg Roach * This program is free software: you can redistribute it and/or modify
7*6fd01894SGreg Roach * it under the terms of the GNU General Public License as published by
8*6fd01894SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9*6fd01894SGreg Roach * (at your option) any later version.
10*6fd01894SGreg Roach * This program is distributed in the hope that it will be useful,
11*6fd01894SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12*6fd01894SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13*6fd01894SGreg Roach * GNU General Public License for more details.
14*6fd01894SGreg Roach * You should have received a copy of the GNU General Public License
15*6fd01894SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
16*6fd01894SGreg Roach */
17*6fd01894SGreg Roach
18*6fd01894SGreg Roachdeclare(strict_types=1);
19*6fd01894SGreg Roach
20*6fd01894SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
21*6fd01894SGreg Roach
22*6fd01894SGreg Roachuse Exception;
23*6fd01894SGreg Roachuse Fisharebest\Webtrees\FlashMessages;
24*6fd01894SGreg Roachuse Fisharebest\Webtrees\I18N;
25*6fd01894SGreg Roachuse Fisharebest\Webtrees\Tree;
26*6fd01894SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
27*6fd01894SGreg Roachuse Psr\Http\Message\ResponseInterface;
28*6fd01894SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
29*6fd01894SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
30*6fd01894SGreg Roach
31*6fd01894SGreg Roachuse function array_unique;
32*6fd01894SGreg Roachuse function assert;
33*6fd01894SGreg Roachuse function e;
34*6fd01894SGreg Roachuse function implode;
35*6fd01894SGreg Roachuse function preg_replace;
36*6fd01894SGreg Roachuse function redirect;
37*6fd01894SGreg Roachuse function route;
38*6fd01894SGreg Roachuse function trim;
39*6fd01894SGreg Roach
40*6fd01894SGreg Roach/**
41*6fd01894SGreg Roach * Edit the tree preferences.
42*6fd01894SGreg Roach */
43*6fd01894SGreg Roachclass TreePreferencesAction implements RequestHandlerInterface
44*6fd01894SGreg Roach{
45*6fd01894SGreg Roach    /**
46*6fd01894SGreg Roach     * @param ServerRequestInterface $request
47*6fd01894SGreg Roach     *
48*6fd01894SGreg Roach     * @return ResponseInterface
49*6fd01894SGreg Roach     */
50*6fd01894SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
51*6fd01894SGreg Roach    {
52*6fd01894SGreg Roach        $tree = $request->getAttribute('tree');
53*6fd01894SGreg Roach        assert($tree instanceof Tree);
54*6fd01894SGreg Roach
55*6fd01894SGreg Roach        $params = (array) $request->getParsedBody();
56*6fd01894SGreg Roach
57*6fd01894SGreg Roach        $tree->setPreference('ADVANCED_NAME_FACTS', implode(',', $params['ADVANCED_NAME_FACTS'] ?? []));
58*6fd01894SGreg Roach        $tree->setPreference('ADVANCED_PLAC_FACTS', implode(',', $params['ADVANCED_PLAC_FACTS'] ?? []));
59*6fd01894SGreg Roach        // For backwards compatibility with webtrees 1.x we store the two calendar formats in one variable
60*6fd01894SGreg Roach        // e.g. "gregorian_and_jewish"
61*6fd01894SGreg Roach        $tree->setPreference('CALENDAR_FORMAT', implode('_and_', array_unique([
62*6fd01894SGreg Roach            $params['CALENDAR_FORMAT0'] ?? 'none',
63*6fd01894SGreg Roach            $params['CALENDAR_FORMAT1'] ?? 'none',
64*6fd01894SGreg Roach        ])));
65*6fd01894SGreg Roach        $tree->setPreference('CHART_BOX_TAGS', implode(',', $params['CHART_BOX_TAGS'] ?? []));
66*6fd01894SGreg Roach        $tree->setPreference('CONTACT_USER_ID', $params['CONTACT_USER_ID'] ?? '');
67*6fd01894SGreg Roach        $tree->setPreference('EXPAND_NOTES', $params['EXPAND_NOTES'] ?? '');
68*6fd01894SGreg Roach        $tree->setPreference('EXPAND_SOURCES', $params['EXPAND_SOURCES'] ?? '');
69*6fd01894SGreg Roach        $tree->setPreference('FAM_FACTS_ADD', implode(',', $params['FAM_FACTS_ADD'] ?? []));
70*6fd01894SGreg Roach        $tree->setPreference('FAM_FACTS_QUICK', implode(',', $params['FAM_FACTS_QUICK'] ?? []));
71*6fd01894SGreg Roach        $tree->setPreference('FAM_FACTS_UNIQUE', implode(',', $params['FAM_FACTS_UNIQUE'] ?? []));
72*6fd01894SGreg Roach        $tree->setPreference('FULL_SOURCES', $params['FULL_SOURCES'] ?? '');
73*6fd01894SGreg Roach        $tree->setPreference('FORMAT_TEXT', $params['FORMAT_TEXT'] ?? '');
74*6fd01894SGreg Roach        $tree->setPreference('GENERATE_UIDS', $params['GENERATE_UIDS'] ?? '');
75*6fd01894SGreg Roach        $tree->setPreference('HIDE_GEDCOM_ERRORS', $params['HIDE_GEDCOM_ERRORS'] ?? '');
76*6fd01894SGreg Roach        $tree->setPreference('INDI_FACTS_ADD', implode(',', $params['INDI_FACTS_ADD'] ?? []));
77*6fd01894SGreg Roach        $tree->setPreference('INDI_FACTS_QUICK', implode(',', $params['INDI_FACTS_QUICK'] ?? []));
78*6fd01894SGreg Roach        $tree->setPreference('INDI_FACTS_UNIQUE', implode(',', $params['INDI_FACTS_UNIQUE'] ?? []));
79*6fd01894SGreg Roach        $tree->setPreference('MEDIA_UPLOAD', $params['MEDIA_UPLOAD'] ?? '');
80*6fd01894SGreg Roach        $tree->setPreference('META_DESCRIPTION', $params['META_DESCRIPTION'] ?? '');
81*6fd01894SGreg Roach        $tree->setPreference('META_TITLE', $params['META_TITLE'] ?? '');
82*6fd01894SGreg Roach        $tree->setPreference('NO_UPDATE_CHAN', $params['NO_UPDATE_CHAN'] ?? '');
83*6fd01894SGreg Roach        $tree->setPreference('PEDIGREE_ROOT_ID', $params['PEDIGREE_ROOT_ID'] ?? '');
84*6fd01894SGreg Roach        $tree->setPreference('PREFER_LEVEL2_SOURCES', $params['PREFER_LEVEL2_SOURCES'] ?? '');
85*6fd01894SGreg Roach        $tree->setPreference('QUICK_REQUIRED_FACTS', implode(',', $params['QUICK_REQUIRED_FACTS'] ?? []));
86*6fd01894SGreg Roach        $tree->setPreference('QUICK_REQUIRED_FAMFACTS', implode(',', $params['QUICK_REQUIRED_FAMFACTS'] ?? []));
87*6fd01894SGreg Roach        $tree->setPreference('REPO_FACTS_ADD', implode(',', $params['REPO_FACTS_ADD'] ?? []));
88*6fd01894SGreg Roach        $tree->setPreference('REPO_FACTS_QUICK', implode(',', $params['REPO_FACTS_QUICK'] ?? []));
89*6fd01894SGreg Roach        $tree->setPreference('REPO_FACTS_UNIQUE', implode(',', $params['REPO_FACTS_UNIQUE'] ?? []));
90*6fd01894SGreg Roach        $tree->setPreference('SHOW_COUNTER', $params['SHOW_COUNTER'] ?? '');
91*6fd01894SGreg Roach        $tree->setPreference('SHOW_EST_LIST_DATES', $params['SHOW_EST_LIST_DATES'] ?? '');
92*6fd01894SGreg Roach        $tree->setPreference('SHOW_FACT_ICONS', $params['SHOW_FACT_ICONS'] ?? '');
93*6fd01894SGreg Roach        $tree->setPreference('SHOW_GEDCOM_RECORD', $params['SHOW_GEDCOM_RECORD'] ?? '');
94*6fd01894SGreg Roach        $tree->setPreference('SHOW_HIGHLIGHT_IMAGES', $params['SHOW_HIGHLIGHT_IMAGES'] ?? '');
95*6fd01894SGreg Roach        $tree->setPreference('SHOW_LAST_CHANGE', $params['SHOW_LAST_CHANGE'] ?? '');
96*6fd01894SGreg Roach        $tree->setPreference('SHOW_MEDIA_DOWNLOAD', $params['SHOW_MEDIA_DOWNLOAD'] ?? '');
97*6fd01894SGreg Roach        $tree->setPreference('SHOW_NO_WATERMARK', $params['SHOW_NO_WATERMARK'] ?? '');
98*6fd01894SGreg Roach        $tree->setPreference('SHOW_PARENTS_AGE', $params['SHOW_PARENTS_AGE'] ?? '');
99*6fd01894SGreg Roach        $tree->setPreference('SHOW_PEDIGREE_PLACES', $params['SHOW_PEDIGREE_PLACES'] ?? '');
100*6fd01894SGreg Roach        $tree->setPreference('SHOW_PEDIGREE_PLACES_SUFFIX', $params['SHOW_PEDIGREE_PLACES_SUFFIX'] ?? '');
101*6fd01894SGreg Roach        $tree->setPreference('SHOW_RELATIVES_EVENTS', implode(',', $params['SHOW_RELATIVES_EVENTS'] ?? []));
102*6fd01894SGreg Roach        $tree->setPreference('SOUR_FACTS_ADD', implode(',', $params['SOUR_FACTS_ADD'] ?? []));
103*6fd01894SGreg Roach        $tree->setPreference('SOUR_FACTS_QUICK', implode(',', $params['SOUR_FACTS_QUICK'] ?? []));
104*6fd01894SGreg Roach        $tree->setPreference('SOUR_FACTS_UNIQUE', implode(',', $params['SOUR_FACTS_UNIQUE'] ?? []));
105*6fd01894SGreg Roach        $tree->setPreference('SUBLIST_TRIGGER_I', $params['SUBLIST_TRIGGER_I'] ?? '200');
106*6fd01894SGreg Roach        $tree->setPreference('SURNAME_LIST_STYLE', $params['SURNAME_LIST_STYLE'] ?? '');
107*6fd01894SGreg Roach        $tree->setPreference('SURNAME_TRADITION', $params['SURNAME_TRADITION'] ?? '');
108*6fd01894SGreg Roach        $tree->setPreference('USE_SILHOUETTE', $params['USE_SILHOUETTE'] ?? '');
109*6fd01894SGreg Roach        $tree->setPreference('WEBMASTER_USER_ID', $params['WEBMASTER_USER_ID'] ?? '');
110*6fd01894SGreg Roach        $tree->setPreference('title', $params['title'] ?? '');
111*6fd01894SGreg Roach
112*6fd01894SGreg Roach        // Only accept valid folders for MEDIA_DIRECTORY
113*6fd01894SGreg Roach        $MEDIA_DIRECTORY = $params['MEDIA_DIRECTORY'] ?? '';
114*6fd01894SGreg Roach        $MEDIA_DIRECTORY = preg_replace('/[:\/\\\\]+/', '/', $MEDIA_DIRECTORY);
115*6fd01894SGreg Roach        $MEDIA_DIRECTORY = trim($MEDIA_DIRECTORY, '/') . '/';
116*6fd01894SGreg Roach
117*6fd01894SGreg Roach        $tree->setPreference('MEDIA_DIRECTORY', $MEDIA_DIRECTORY);
118*6fd01894SGreg Roach
119*6fd01894SGreg Roach        $gedcom = $params['gedcom'] ?? '';
120*6fd01894SGreg Roach
121*6fd01894SGreg Roach        if ($gedcom !== '' && $gedcom !== $tree->name()) {
122*6fd01894SGreg Roach            try {
123*6fd01894SGreg Roach                DB::table('gedcom')
124*6fd01894SGreg Roach                    ->where('gedcom_id', '=', $tree->id())
125*6fd01894SGreg Roach                    ->update(['gedcom_name' => $gedcom]);
126*6fd01894SGreg Roach
127*6fd01894SGreg Roach                DB::table('site_setting')
128*6fd01894SGreg Roach                    ->where('setting_name', '=', 'DEFAULT_GEDCOM')
129*6fd01894SGreg Roach                    ->where('setting_value', '=', $tree->name())
130*6fd01894SGreg Roach                    ->update(['setting_value' => $gedcom]);
131*6fd01894SGreg Roach            } catch (Exception $ex) {
132*6fd01894SGreg Roach                // Probably a duplicate name.
133*6fd01894SGreg Roach            }
134*6fd01894SGreg Roach        }
135*6fd01894SGreg Roach
136*6fd01894SGreg Roach        FlashMessages::addMessage(I18N::translate('The preferences for the family tree “%s” have been updated.', e($tree->title())), 'success');
137*6fd01894SGreg Roach
138*6fd01894SGreg Roach        // Coming soon...
139*6fd01894SGreg Roach        $all_trees = $params['all_trees'] ?? '';
140*6fd01894SGreg Roach        $new_trees = $params['new_trees'] ?? '';
141*6fd01894SGreg Roach
142*6fd01894SGreg Roach        if ($all_trees === 'on') {
143*6fd01894SGreg Roach            FlashMessages::addMessage(I18N::translate('The preferences for all family trees have been updated.'), 'success');
144*6fd01894SGreg Roach        }
145*6fd01894SGreg Roach
146*6fd01894SGreg Roach        if ($new_trees === 'on') {
147*6fd01894SGreg Roach            FlashMessages::addMessage(I18N::translate('The preferences for new family trees have been updated.'), 'success');
148*6fd01894SGreg Roach        }
149*6fd01894SGreg Roach
150*6fd01894SGreg Roach        $url = route(ManageTrees::class, ['tree' => $tree->name()]);
151*6fd01894SGreg Roach
152*6fd01894SGreg Roach        return redirect($url);
153*6fd01894SGreg Roach    }
154*6fd01894SGreg Roach}
155