xref: /webtrees/app/Http/RequestHandlers/TreePrivacyAction.php (revision 110d87e5cc3161866682d0a932d11474cfbcad7f)
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 Fisharebest\Webtrees\FlashMessages;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Validator;
25use Illuminate\Database\Capsule\Manager as DB;
26use Psr\Http\Message\ResponseInterface;
27use Psr\Http\Message\ServerRequestInterface;
28use Psr\Http\Server\RequestHandlerInterface;
29
30use function e;
31use function redirect;
32use function route;
33
34/**
35 * Edit the tree privacy.
36 */
37class TreePrivacyAction implements RequestHandlerInterface
38{
39    /**
40     * @param ServerRequestInterface $request
41     *
42     * @return ResponseInterface
43     */
44    public function handle(ServerRequestInterface $request): ResponseInterface
45    {
46        $tree   = Validator::attributes($request)->tree();
47        $params = (array) $request->getParsedBody();
48
49        $delete_default_resn_id = $params['delete'] ?? [];
50
51        DB::table('default_resn')
52            ->whereIn('default_resn_id', $delete_default_resn_id)
53            ->delete();
54
55        $xrefs     = $params['xref'] ?? [];
56        $tag_types = $params['tag_type'] ?? [];
57        $resns     = $params['resn'] ?? [];
58
59        foreach ($xrefs as $n => $xref) {
60            $tag_type = $tag_types[$n];
61            $resn     = $resns[$n];
62
63            // Delete any existing data
64            if ($tag_type !== '' && $xref !== '') {
65                DB::table('default_resn')
66                    ->where('gedcom_id', '=', $tree->id())
67                    ->where('tag_type', '=', $tag_type)
68                    ->where('xref', '=', $xref)
69                    ->delete();
70            }
71
72            if ($tag_type !== '' && $xref === '') {
73                DB::table('default_resn')
74                    ->where('gedcom_id', '=', $tree->id())
75                    ->where('tag_type', '=', $tag_type)
76                    ->whereNull('xref')
77                    ->delete();
78            }
79
80            if ($tag_type === '' && $xref !== '') {
81                DB::table('default_resn')
82                    ->where('gedcom_id', '=', $tree->id())
83                    ->whereNull('tag_type')
84                    ->where('xref', '=', $xref)
85                    ->delete();
86            }
87
88            // Add (or update) the new data
89            if ($tag_type !== '' || $xref !== '') {
90                DB::table('default_resn')->insert([
91                    'gedcom_id' => $tree->id(),
92                    'xref'      => $xref === '' ? null : $xref,
93                    'tag_type'  => $tag_type === '' ? null : $tag_type,
94                    'resn'      => $resn,
95                ]);
96            }
97        }
98
99        $tree->setPreference('HIDE_LIVE_PEOPLE', $params['HIDE_LIVE_PEOPLE']);
100        $tree->setPreference('KEEP_ALIVE_YEARS_BIRTH', $params['KEEP_ALIVE_YEARS_BIRTH']);
101        $tree->setPreference('KEEP_ALIVE_YEARS_DEATH', $params['KEEP_ALIVE_YEARS_DEATH']);
102        $tree->setPreference('MAX_ALIVE_AGE', $params['MAX_ALIVE_AGE']);
103        $tree->setPreference('REQUIRE_AUTHENTICATION', $params['REQUIRE_AUTHENTICATION']);
104        $tree->setPreference('SHOW_DEAD_PEOPLE', $params['SHOW_DEAD_PEOPLE']);
105        $tree->setPreference('SHOW_LIVING_NAMES', $params['SHOW_LIVING_NAMES']);
106        $tree->setPreference('SHOW_PRIVATE_RELATIONSHIPS', $params['SHOW_PRIVATE_RELATIONSHIPS']);
107
108        FlashMessages::addMessage(I18N::translate('The preferences for the family tree “%s” have been updated.', e($tree->title())), 'success');
109
110        // Coming soon...
111        $all_trees = $params['all_trees'] ?? '';
112        $new_trees = $params['new_trees'] ?? '';
113
114        if ($all_trees === 'on') {
115            FlashMessages::addMessage(I18N::translate('The preferences for all family trees have been updated.', e($tree->title())), 'success');
116        }
117        if ($new_trees === 'on') {
118            FlashMessages::addMessage(I18N::translate('The preferences for new family trees have been updated.', e($tree->title())), 'success');
119        }
120
121        return redirect(route(ManageTrees::class, ['tree' => $tree->name()]));
122    }
123}
124