xref: /webtrees/app/Http/RequestHandlers/TreePreferencesAction.php (revision 504055284fcef1c6d50f172e3cb3fa077cc9c7a8)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2022 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 Fisharebest\Webtrees\Auth;
23use Fisharebest\Webtrees\FlashMessages;
24use Fisharebest\Webtrees\I18N;
25use Fisharebest\Webtrees\Validator;
26use Illuminate\Database\Capsule\Manager as DB;
27use PDOException;
28use Psr\Http\Message\ResponseInterface;
29use Psr\Http\Message\ServerRequestInterface;
30use Psr\Http\Server\RequestHandlerInterface;
31
32use function array_unique;
33use function e;
34use function implode;
35use function preg_replace;
36use function redirect;
37use function route;
38use function trim;
39
40/**
41 * Edit the tree preferences.
42 */
43class TreePreferencesAction implements RequestHandlerInterface
44{
45    /**
46     * @param ServerRequestInterface $request
47     *
48     * @return ResponseInterface
49     */
50    public function handle(ServerRequestInterface $request): ResponseInterface
51    {
52        $tree   = Validator::attributes($request)->tree();
53        $params = (array) $request->getParsedBody();
54
55        // For backwards compatibility with webtrees 1.x we store the two calendar formats in one variable
56        // e.g. "gregorian_and_jewish"
57        $tree->setPreference('CALENDAR_FORMAT', implode('_and_', array_unique([
58            $params['CALENDAR_FORMAT0'] ?? 'none',
59            $params['CALENDAR_FORMAT1'] ?? 'none',
60        ])));
61        $tree->setPreference('CHART_BOX_TAGS', implode(',', $params['CHART_BOX_TAGS'] ?? []));
62        $tree->setPreference('CONTACT_USER_ID', $params['CONTACT_USER_ID'] ?? '');
63        $tree->setPreference('EXPAND_NOTES', $params['EXPAND_NOTES'] ?? '');
64        $tree->setPreference('EXPAND_SOURCES', $params['EXPAND_SOURCES'] ?? '');
65        $tree->setPreference('FAM_FACTS_QUICK', implode(',', $params['FAM_FACTS_QUICK'] ?? []));
66        $tree->setPreference('FORMAT_TEXT', $params['FORMAT_TEXT'] ?? '');
67        $tree->setPreference('GENERATE_UIDS', $params['GENERATE_UIDS'] ?? '');
68        $tree->setPreference('HIDE_GEDCOM_ERRORS', $params['HIDE_GEDCOM_ERRORS'] ?? '');
69        $tree->setPreference('INDI_FACTS_QUICK', implode(',', $params['INDI_FACTS_QUICK'] ?? []));
70        $tree->setPreference('MEDIA_UPLOAD', $params['MEDIA_UPLOAD'] ?? '');
71        $tree->setPreference('META_DESCRIPTION', $params['META_DESCRIPTION'] ?? '');
72        $tree->setPreference('META_TITLE', $params['META_TITLE'] ?? '');
73        $tree->setPreference('NO_UPDATE_CHAN', $params['NO_UPDATE_CHAN'] ?? '');
74        $tree->setPreference('PEDIGREE_ROOT_ID', $params['PEDIGREE_ROOT_ID'] ?? '');
75        $tree->setPreference('QUICK_REQUIRED_FACTS', implode(',', $params['QUICK_REQUIRED_FACTS'] ?? []));
76        $tree->setPreference('QUICK_REQUIRED_FAMFACTS', implode(',', $params['QUICK_REQUIRED_FAMFACTS'] ?? []));
77        $tree->setPreference('SHOW_COUNTER', $params['SHOW_COUNTER'] ?? '');
78        $tree->setPreference('SHOW_EST_LIST_DATES', $params['SHOW_EST_LIST_DATES'] ?? '');
79        $tree->setPreference('SHOW_FACT_ICONS', $params['SHOW_FACT_ICONS'] ?? '');
80        $tree->setPreference('SHOW_GEDCOM_RECORD', $params['SHOW_GEDCOM_RECORD'] ?? '');
81        $tree->setPreference('SHOW_HIGHLIGHT_IMAGES', $params['SHOW_HIGHLIGHT_IMAGES'] ?? '');
82        $tree->setPreference('SHOW_LAST_CHANGE', $params['SHOW_LAST_CHANGE'] ?? '');
83        $tree->setPreference('SHOW_MEDIA_DOWNLOAD', $params['SHOW_MEDIA_DOWNLOAD'] ?? '');
84        $tree->setPreference('SHOW_NO_WATERMARK', $params['SHOW_NO_WATERMARK'] ?? '');
85        $tree->setPreference('SHOW_PARENTS_AGE', $params['SHOW_PARENTS_AGE'] ?? '');
86        $tree->setPreference('SHOW_PEDIGREE_PLACES', $params['SHOW_PEDIGREE_PLACES'] ?? '');
87        $tree->setPreference('SHOW_PEDIGREE_PLACES_SUFFIX', $params['SHOW_PEDIGREE_PLACES_SUFFIX'] ?? '');
88        $tree->setPreference('SHOW_RELATIVES_EVENTS', implode(',', $params['SHOW_RELATIVES_EVENTS'] ?? []));
89        $tree->setPreference('SUBLIST_TRIGGER_I', $params['SUBLIST_TRIGGER_I'] ?? '200');
90        $tree->setPreference('SURNAME_LIST_STYLE', $params['SURNAME_LIST_STYLE'] ?? '');
91        $tree->setPreference('SURNAME_TRADITION', $params['SURNAME_TRADITION'] ?? '');
92        $tree->setPreference('USE_SILHOUETTE', $params['USE_SILHOUETTE'] ?? '');
93        $tree->setPreference('WEBMASTER_USER_ID', $params['WEBMASTER_USER_ID'] ?? '');
94        $tree->setPreference('title', $params['title'] ?? '');
95
96        if (Auth::isAdmin()) {
97            // Only accept valid folders for MEDIA_DIRECTORY
98            $MEDIA_DIRECTORY = $params['MEDIA_DIRECTORY'] ?? '';
99            $MEDIA_DIRECTORY = preg_replace('/[:\/\\\\]+/', '/', $MEDIA_DIRECTORY);
100            $MEDIA_DIRECTORY = trim($MEDIA_DIRECTORY, '/') . '/';
101
102            $tree->setPreference('MEDIA_DIRECTORY', $MEDIA_DIRECTORY);
103        }
104
105        $gedcom = $params['gedcom'] ?? '';
106        $url    = route(ManageTrees::class, ['tree' => $tree->name()]);
107
108        if (Auth::isAdmin() && $gedcom !== '' && $gedcom !== $tree->name()) {
109            try {
110                DB::table('gedcom')
111                    ->where('gedcom_id', '=', $tree->id())
112                    ->update(['gedcom_name' => $gedcom]);
113
114                // Did we rename the default tree?
115                DB::table('site_setting')
116                    ->where('setting_name', '=', 'DEFAULT_GEDCOM')
117                    ->where('setting_value', '=', $tree->name())
118                    ->update(['setting_value' => $gedcom]);
119
120                $url = route(ManageTrees::class, ['tree' => $gedcom]);
121            } catch (PDOException $ex) {
122                // Probably a duplicate name.
123            }
124        }
125
126        FlashMessages::addMessage(I18N::translate('The preferences for the family tree “%s” have been updated.', e($tree->title())), 'success');
127
128        // Coming soon...
129        $all_trees = $params['all_trees'] ?? '';
130        $new_trees = $params['new_trees'] ?? '';
131
132        if ($all_trees === 'on') {
133            FlashMessages::addMessage(I18N::translate('The preferences for all family trees have been updated.'), 'success');
134        }
135
136        if ($new_trees === 'on') {
137            FlashMessages::addMessage(I18N::translate('The preferences for new family trees have been updated.'), 'success');
138        }
139
140        return redirect($url);
141    }
142}
143