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