xref: /webtrees/app/Http/RequestHandlers/TreePrivacyAction.php (revision 6f4ec3cadc983f0a7294108c634bef48846b4311)
16fd01894SGreg Roach<?php
26fd01894SGreg Roach
36fd01894SGreg Roach/**
46fd01894SGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
66fd01894SGreg Roach * This program is free software: you can redistribute it and/or modify
76fd01894SGreg Roach * it under the terms of the GNU General Public License as published by
86fd01894SGreg Roach * the Free Software Foundation, either version 3 of the License, or
96fd01894SGreg Roach * (at your option) any later version.
106fd01894SGreg Roach * This program is distributed in the hope that it will be useful,
116fd01894SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
126fd01894SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
136fd01894SGreg Roach * GNU General Public License for more details.
146fd01894SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
166fd01894SGreg Roach */
176fd01894SGreg Roach
186fd01894SGreg Roachdeclare(strict_types=1);
196fd01894SGreg Roach
206fd01894SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
216fd01894SGreg Roach
22*6f4ec3caSGreg Roachuse Fisharebest\Webtrees\DB;
236fd01894SGreg Roachuse Fisharebest\Webtrees\FlashMessages;
24748dbe15SGreg Roachuse Fisharebest\Webtrees\Http\Exceptions\HttpBadRequestException;
256fd01894SGreg Roachuse Fisharebest\Webtrees\I18N;
26b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
276fd01894SGreg Roachuse Psr\Http\Message\ResponseInterface;
286fd01894SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
296fd01894SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
306fd01894SGreg Roach
316fd01894SGreg Roachuse function e;
326fd01894SGreg Roachuse function redirect;
336fd01894SGreg Roachuse function route;
346fd01894SGreg Roach
356fd01894SGreg Roach/**
366fd01894SGreg Roach * Edit the tree privacy.
376fd01894SGreg Roach */
386fd01894SGreg Roachclass TreePrivacyAction implements RequestHandlerInterface
396fd01894SGreg Roach{
406fd01894SGreg Roach    /**
416fd01894SGreg Roach     * @param ServerRequestInterface $request
426fd01894SGreg Roach     *
436fd01894SGreg Roach     * @return ResponseInterface
446fd01894SGreg Roach     */
456fd01894SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
466fd01894SGreg Roach    {
47b55cbc6bSGreg Roach        $tree = Validator::attributes($request)->tree();
486fd01894SGreg Roach
49748dbe15SGreg Roach        $delete_default_resn_id = Validator::parsedBody($request)->array('delete');
506fd01894SGreg Roach
516fd01894SGreg Roach        DB::table('default_resn')
526fd01894SGreg Roach            ->whereIn('default_resn_id', $delete_default_resn_id)
536fd01894SGreg Roach            ->delete();
546fd01894SGreg Roach
55748dbe15SGreg Roach        $xrefs     = Validator::parsedBody($request)->array('xref');
56748dbe15SGreg Roach        $tag_types = Validator::parsedBody($request)->array('tag_type');
57748dbe15SGreg Roach        $resns     = Validator::parsedBody($request)->array('resn');
58748dbe15SGreg Roach
59748dbe15SGreg Roach        $count_xrefs     = count($xrefs);
60748dbe15SGreg Roach        $count_tag_types = count($tag_types);
61748dbe15SGreg Roach        $count_resns     = count($resns);
62748dbe15SGreg Roach
63748dbe15SGreg Roach        if ($count_xrefs !== $count_tag_types || $count_xrefs !== $count_resns) {
64748dbe15SGreg Roach            $message = 'Bad parameter count: ' . $count_xrefs . '/' . $count_tag_types . '/' . $count_resns;
65748dbe15SGreg Roach            throw new HttpBadRequestException($message);
66748dbe15SGreg Roach        }
676fd01894SGreg Roach
686fd01894SGreg Roach        foreach ($xrefs as $n => $xref) {
696fd01894SGreg Roach            $tag_type = $tag_types[$n];
706fd01894SGreg Roach            $resn     = $resns[$n];
716fd01894SGreg Roach
726fd01894SGreg Roach            // Delete any existing data
736fd01894SGreg Roach            if ($tag_type !== '' && $xref !== '') {
746fd01894SGreg Roach                DB::table('default_resn')
756fd01894SGreg Roach                    ->where('gedcom_id', '=', $tree->id())
766fd01894SGreg Roach                    ->where('tag_type', '=', $tag_type)
776fd01894SGreg Roach                    ->where('xref', '=', $xref)
786fd01894SGreg Roach                    ->delete();
796fd01894SGreg Roach            }
806fd01894SGreg Roach
816fd01894SGreg Roach            if ($tag_type !== '' && $xref === '') {
826fd01894SGreg Roach                DB::table('default_resn')
836fd01894SGreg Roach                    ->where('gedcom_id', '=', $tree->id())
846fd01894SGreg Roach                    ->where('tag_type', '=', $tag_type)
856fd01894SGreg Roach                    ->whereNull('xref')
866fd01894SGreg Roach                    ->delete();
876fd01894SGreg Roach            }
886fd01894SGreg Roach
896fd01894SGreg Roach            if ($tag_type === '' && $xref !== '') {
906fd01894SGreg Roach                DB::table('default_resn')
916fd01894SGreg Roach                    ->where('gedcom_id', '=', $tree->id())
926fd01894SGreg Roach                    ->whereNull('tag_type')
936fd01894SGreg Roach                    ->where('xref', '=', $xref)
946fd01894SGreg Roach                    ->delete();
956fd01894SGreg Roach            }
966fd01894SGreg Roach
976fd01894SGreg Roach            // Add (or update) the new data
986fd01894SGreg Roach            if ($tag_type !== '' || $xref !== '') {
996fd01894SGreg Roach                DB::table('default_resn')->insert([
1006fd01894SGreg Roach                    'gedcom_id' => $tree->id(),
1016fd01894SGreg Roach                    'xref'      => $xref === '' ? null : $xref,
1026fd01894SGreg Roach                    'tag_type'  => $tag_type === '' ? null : $tag_type,
1036fd01894SGreg Roach                    'resn'      => $resn,
1046fd01894SGreg Roach                ]);
1056fd01894SGreg Roach            }
1066fd01894SGreg Roach        }
1076fd01894SGreg Roach
108748dbe15SGreg Roach        $hide_live_people           = Validator::parsedBody($request)->string('HIDE_LIVE_PEOPLE');
109748dbe15SGreg Roach        $keep_alive_years_birth     = Validator::parsedBody($request)->integer('KEEP_ALIVE_YEARS_BIRTH', 0);
110748dbe15SGreg Roach        $keep_alive_years_death     = Validator::parsedBody($request)->integer('KEEP_ALIVE_YEARS_DEATH', 0);
111748dbe15SGreg Roach        $max_alive_age              = Validator::parsedBody($request)->integer('MAX_ALIVE_AGE');
112748dbe15SGreg Roach        $require_authentication     = Validator::parsedBody($request)->string('REQUIRE_AUTHENTICATION');
113748dbe15SGreg Roach        $show_dead_people           = Validator::parsedBody($request)->string('SHOW_DEAD_PEOPLE');
114748dbe15SGreg Roach        $show_living_names          = Validator::parsedBody($request)->string('SHOW_LIVING_NAMES');
115748dbe15SGreg Roach        $show_private_relationships = Validator::parsedBody($request)->string('SHOW_PRIVATE_RELATIONSHIPS');
116748dbe15SGreg Roach
117748dbe15SGreg Roach        $tree->setPreference('HIDE_LIVE_PEOPLE', $hide_live_people);
118748dbe15SGreg Roach        $tree->setPreference('KEEP_ALIVE_YEARS_BIRTH', (string) $keep_alive_years_birth);
119748dbe15SGreg Roach        $tree->setPreference('KEEP_ALIVE_YEARS_DEATH', (string) $keep_alive_years_death);
120748dbe15SGreg Roach        $tree->setPreference('MAX_ALIVE_AGE', (string) $max_alive_age);
121748dbe15SGreg Roach        $tree->setPreference('REQUIRE_AUTHENTICATION', $require_authentication);
122748dbe15SGreg Roach        $tree->setPreference('SHOW_DEAD_PEOPLE', $show_dead_people);
123748dbe15SGreg Roach        $tree->setPreference('SHOW_LIVING_NAMES', $show_living_names);
124748dbe15SGreg Roach        $tree->setPreference('SHOW_PRIVATE_RELATIONSHIPS', $show_private_relationships);
1256fd01894SGreg Roach
1266fd01894SGreg Roach        FlashMessages::addMessage(I18N::translate('The preferences for the family tree “%s” have been updated.', e($tree->title())), 'success');
1276fd01894SGreg Roach
1286fd01894SGreg Roach        // Coming soon...
129748dbe15SGreg Roach        $all_trees = Validator::parsedBody($request)->boolean('all_trees', false);
130748dbe15SGreg Roach        $new_trees = Validator::parsedBody($request)->boolean('new_trees', false);
1316fd01894SGreg Roach
132748dbe15SGreg Roach        if ($all_trees) {
1336fd01894SGreg Roach            FlashMessages::addMessage(I18N::translate('The preferences for all family trees have been updated.', e($tree->title())), 'success');
1346fd01894SGreg Roach        }
135748dbe15SGreg Roach        if ($new_trees) {
1366fd01894SGreg Roach            FlashMessages::addMessage(I18N::translate('The preferences for new family trees have been updated.', e($tree->title())), 'success');
1376fd01894SGreg Roach        }
1386fd01894SGreg Roach
1396fd01894SGreg Roach        return redirect(route(ManageTrees::class, ['tree' => $tree->name()]));
1406fd01894SGreg Roach    }
1416fd01894SGreg Roach}
142