17c7d1e03SGreg Roach<?php 27c7d1e03SGreg Roach 37c7d1e03SGreg Roach/** 47c7d1e03SGreg Roach * webtrees: online genealogy 5*d11be702SGreg Roach * Copyright (C) 2023 webtrees development team 67c7d1e03SGreg Roach * This program is free software: you can redistribute it and/or modify 77c7d1e03SGreg Roach * it under the terms of the GNU General Public License as published by 87c7d1e03SGreg Roach * the Free Software Foundation, either version 3 of the License, or 97c7d1e03SGreg Roach * (at your option) any later version. 107c7d1e03SGreg Roach * This program is distributed in the hope that it will be useful, 117c7d1e03SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 127c7d1e03SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 137c7d1e03SGreg Roach * GNU General Public License for more details. 147c7d1e03SGreg 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/>. 167c7d1e03SGreg Roach */ 177c7d1e03SGreg Roach 187c7d1e03SGreg Roachdeclare(strict_types=1); 197c7d1e03SGreg Roach 207c7d1e03SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers; 217c7d1e03SGreg Roach 227c7d1e03SGreg Roachuse Fisharebest\Webtrees\Auth; 23665e281aSGreg Roachuse Fisharebest\Webtrees\Elements\PedigreeLinkageType; 246b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry; 25b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator; 267c7d1e03SGreg Roachuse Psr\Http\Message\ResponseInterface; 277c7d1e03SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 287c7d1e03SGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 297c7d1e03SGreg Roach 307c7d1e03SGreg Roachuse function redirect; 317c7d1e03SGreg Roach 327c7d1e03SGreg Roach/** 337c7d1e03SGreg Roach * Link an existing individual as child in an existing family. 347c7d1e03SGreg Roach */ 357c7d1e03SGreg Roachclass LinkChildToFamilyAction implements RequestHandlerInterface 367c7d1e03SGreg Roach{ 377c7d1e03SGreg Roach /** 387c7d1e03SGreg Roach * @param ServerRequestInterface $request 397c7d1e03SGreg Roach * 407c7d1e03SGreg Roach * @return ResponseInterface 417c7d1e03SGreg Roach */ 427c7d1e03SGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 437c7d1e03SGreg Roach { 44b55cbc6bSGreg Roach $tree = Validator::attributes($request)->tree(); 45b55cbc6bSGreg Roach $xref = Validator::attributes($request)->isXref()->string('xref'); 466b9cb339SGreg Roach $individual = Registry::individualFactory()->make($xref, $tree); 477c7d1e03SGreg Roach $individual = Auth::checkIndividualAccess($individual, true); 48748dbe15SGreg Roach $famid = Validator::parsedBody($request)->isXref()->string('famid'); 49748dbe15SGreg Roach $PEDI = Validator::parsedBody($request)->string('PEDI'); 507c7d1e03SGreg Roach 516b9cb339SGreg Roach $family = Registry::familyFactory()->make($famid, $tree); 527c7d1e03SGreg Roach $family = Auth::checkFamilyAccess($family, true); 537c7d1e03SGreg Roach 547c7d1e03SGreg Roach // Replace any existing child->family link (we may be changing the PEDI); 557c7d1e03SGreg Roach $fact_id = ''; 567c7d1e03SGreg Roach foreach ($individual->facts(['FAMC']) as $fact) { 577c7d1e03SGreg Roach if ($family === $fact->target()) { 587c7d1e03SGreg Roach $fact_id = $fact->id(); 597c7d1e03SGreg Roach break; 607c7d1e03SGreg Roach } 617c7d1e03SGreg Roach } 627c7d1e03SGreg Roach 63fb0b33a5SGreg Roach switch ($PEDI) { 64fb0b33a5SGreg Roach case '': 65fb0b33a5SGreg Roach $gedcom = "1 FAMC @$famid@"; 66fb0b33a5SGreg Roach break; 6788a03560SGreg Roach case PedigreeLinkageType::VALUE_ADOPTED: 68fb0b33a5SGreg Roach $gedcom = "1 FAMC @$famid@\n2 PEDI $PEDI\n1 ADOP\n2 FAMC @$famid@\n3 ADOP BOTH"; 69fb0b33a5SGreg Roach break; 7088a03560SGreg Roach case PedigreeLinkageType::VALUE_SEALING: 71fb0b33a5SGreg Roach $gedcom = "1 FAMC @$famid@\n2 PEDI $PEDI\n1 SLGC\n2 FAMC @$famid@"; 72fb0b33a5SGreg Roach break; 7388a03560SGreg Roach case PedigreeLinkageType::VALUE_FOSTER: 74fb0b33a5SGreg Roach $gedcom = "1 FAMC @$famid@\n2 PEDI $PEDI\n1 EVEN\n2 TYPE $PEDI"; 75fb0b33a5SGreg Roach break; 76fb0b33a5SGreg Roach default: 77fb0b33a5SGreg Roach $gedcom = "1 FAMC @$famid@\n2 PEDI $PEDI"; 78fb0b33a5SGreg Roach break; 79fb0b33a5SGreg Roach } 80fb0b33a5SGreg Roach 817c7d1e03SGreg Roach $individual->updateFact($fact_id, $gedcom, true); 827c7d1e03SGreg Roach 837c7d1e03SGreg Roach // Only set the family->child link if it does not already exist 847c7d1e03SGreg Roach $chil_link_exists = false; 857c7d1e03SGreg Roach foreach ($family->facts(['CHIL']) as $fact) { 867c7d1e03SGreg Roach if ($individual === $fact->target()) { 877c7d1e03SGreg Roach $chil_link_exists = true; 887c7d1e03SGreg Roach break; 897c7d1e03SGreg Roach } 907c7d1e03SGreg Roach } 917c7d1e03SGreg Roach 927c7d1e03SGreg Roach if (!$chil_link_exists) { 937c7d1e03SGreg Roach $family->createFact('1 CHIL @' . $individual->xref() . '@', true); 947c7d1e03SGreg Roach } 957c7d1e03SGreg Roach 967c7d1e03SGreg Roach return redirect($individual->url()); 977c7d1e03SGreg Roach } 987c7d1e03SGreg Roach} 99