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