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\FlashMessages; 23use Fisharebest\Webtrees\Http\Exceptions\HttpBadRequestException; 24use Fisharebest\Webtrees\I18N; 25use Fisharebest\Webtrees\Validator; 26use Illuminate\Database\Capsule\Manager as DB; 27use Psr\Http\Message\ResponseInterface; 28use Psr\Http\Message\ServerRequestInterface; 29use Psr\Http\Server\RequestHandlerInterface; 30 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 = Validator::attributes($request)->tree(); 48 49 $delete_default_resn_id = Validator::parsedBody($request)->array('delete'); 50 51 DB::table('default_resn') 52 ->whereIn('default_resn_id', $delete_default_resn_id) 53 ->delete(); 54 55 $xrefs = Validator::parsedBody($request)->array('xref'); 56 $tag_types = Validator::parsedBody($request)->array('tag_type'); 57 $resns = Validator::parsedBody($request)->array('resn'); 58 59 $count_xrefs = count($xrefs); 60 $count_tag_types = count($tag_types); 61 $count_resns = count($resns); 62 63 if ($count_xrefs !== $count_tag_types || $count_xrefs !== $count_resns) { 64 $message = 'Bad parameter count: ' . $count_xrefs . '/' . $count_tag_types . '/' . $count_resns; 65 throw new HttpBadRequestException($message); 66 } 67 68 foreach ($xrefs as $n => $xref) { 69 $tag_type = $tag_types[$n]; 70 $resn = $resns[$n]; 71 72 // Delete any existing data 73 if ($tag_type !== '' && $xref !== '') { 74 DB::table('default_resn') 75 ->where('gedcom_id', '=', $tree->id()) 76 ->where('tag_type', '=', $tag_type) 77 ->where('xref', '=', $xref) 78 ->delete(); 79 } 80 81 if ($tag_type !== '' && $xref === '') { 82 DB::table('default_resn') 83 ->where('gedcom_id', '=', $tree->id()) 84 ->where('tag_type', '=', $tag_type) 85 ->whereNull('xref') 86 ->delete(); 87 } 88 89 if ($tag_type === '' && $xref !== '') { 90 DB::table('default_resn') 91 ->where('gedcom_id', '=', $tree->id()) 92 ->whereNull('tag_type') 93 ->where('xref', '=', $xref) 94 ->delete(); 95 } 96 97 // Add (or update) the new data 98 if ($tag_type !== '' || $xref !== '') { 99 DB::table('default_resn')->insert([ 100 'gedcom_id' => $tree->id(), 101 'xref' => $xref === '' ? null : $xref, 102 'tag_type' => $tag_type === '' ? null : $tag_type, 103 'resn' => $resn, 104 ]); 105 } 106 } 107 108 $hide_live_people = Validator::parsedBody($request)->string('HIDE_LIVE_PEOPLE'); 109 $keep_alive_years_birth = Validator::parsedBody($request)->integer('KEEP_ALIVE_YEARS_BIRTH', 0); 110 $keep_alive_years_death = Validator::parsedBody($request)->integer('KEEP_ALIVE_YEARS_DEATH', 0); 111 $max_alive_age = Validator::parsedBody($request)->integer('MAX_ALIVE_AGE'); 112 $require_authentication = Validator::parsedBody($request)->string('REQUIRE_AUTHENTICATION'); 113 $show_dead_people = Validator::parsedBody($request)->string('SHOW_DEAD_PEOPLE'); 114 $show_living_names = Validator::parsedBody($request)->string('SHOW_LIVING_NAMES'); 115 $show_private_relationships = Validator::parsedBody($request)->string('SHOW_PRIVATE_RELATIONSHIPS'); 116 117 $tree->setPreference('HIDE_LIVE_PEOPLE', $hide_live_people); 118 $tree->setPreference('KEEP_ALIVE_YEARS_BIRTH', (string) $keep_alive_years_birth); 119 $tree->setPreference('KEEP_ALIVE_YEARS_DEATH', (string) $keep_alive_years_death); 120 $tree->setPreference('MAX_ALIVE_AGE', (string) $max_alive_age); 121 $tree->setPreference('REQUIRE_AUTHENTICATION', $require_authentication); 122 $tree->setPreference('SHOW_DEAD_PEOPLE', $show_dead_people); 123 $tree->setPreference('SHOW_LIVING_NAMES', $show_living_names); 124 $tree->setPreference('SHOW_PRIVATE_RELATIONSHIPS', $show_private_relationships); 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 = Validator::parsedBody($request)->boolean('all_trees', false); 130 $new_trees = Validator::parsedBody($request)->boolean('new_trees', false); 131 132 if ($all_trees) { 133 FlashMessages::addMessage(I18N::translate('The preferences for all family trees have been updated.', e($tree->title())), 'success'); 134 } 135 if ($new_trees) { 136 FlashMessages::addMessage(I18N::translate('The preferences for new family trees have been updated.', e($tree->title())), 'success'); 137 } 138 139 return redirect(route(ManageTrees::class, ['tree' => $tree->name()])); 140 } 141} 142