17c7d1e03SGreg Roach<?php 27c7d1e03SGreg Roach 37c7d1e03SGreg Roach/** 47c7d1e03SGreg Roach * webtrees: online genealogy 589f7189bSGreg Roach * Copyright (C) 2021 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; 236b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry; 247c7d1e03SGreg Roachuse Fisharebest\Webtrees\Services\GedcomEditService; 257c7d1e03SGreg Roachuse Fisharebest\Webtrees\Tree; 267c7d1e03SGreg Roachuse Psr\Http\Message\ResponseInterface; 277c7d1e03SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 287c7d1e03SGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 297c7d1e03SGreg Roach 307c7d1e03SGreg Roachuse function assert; 31*efd4768bSGreg Roachuse function is_string; 327c7d1e03SGreg Roachuse function redirect; 337c7d1e03SGreg Roach 347c7d1e03SGreg Roach/** 357c7d1e03SGreg Roach * Add a new child to an individual, creating a one-parent family. 367c7d1e03SGreg Roach */ 377c7d1e03SGreg Roachclass AddChildToIndividualAction implements RequestHandlerInterface 387c7d1e03SGreg Roach{ 397c7d1e03SGreg Roach /** @var GedcomEditService */ 407c7d1e03SGreg Roach private $gedcom_edit_service; 417c7d1e03SGreg Roach 427c7d1e03SGreg Roach /** 437c7d1e03SGreg Roach * AddChildToFamilyAction constructor. 447c7d1e03SGreg Roach * 457c7d1e03SGreg Roach * @param GedcomEditService $gedcom_edit_service 467c7d1e03SGreg Roach */ 477c7d1e03SGreg Roach public function __construct(GedcomEditService $gedcom_edit_service) 487c7d1e03SGreg Roach { 497c7d1e03SGreg Roach $this->gedcom_edit_service = $gedcom_edit_service; 507c7d1e03SGreg Roach } 517c7d1e03SGreg Roach 527c7d1e03SGreg Roach /** 537c7d1e03SGreg Roach * @param ServerRequestInterface $request 547c7d1e03SGreg Roach * 557c7d1e03SGreg Roach * @return ResponseInterface 567c7d1e03SGreg Roach */ 577c7d1e03SGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 587c7d1e03SGreg Roach { 597c7d1e03SGreg Roach $tree = $request->getAttribute('tree'); 607c7d1e03SGreg Roach assert($tree instanceof Tree); 617c7d1e03SGreg Roach 62*efd4768bSGreg Roach $xref = $request->getAttribute('xref'); 63*efd4768bSGreg Roach assert(is_string($xref)); 64*efd4768bSGreg Roach 65*efd4768bSGreg Roach $params = (array) $request->getParsedBody(); 667c7d1e03SGreg Roach 676b9cb339SGreg Roach $individual = Registry::individualFactory()->make($xref, $tree); 687c7d1e03SGreg Roach $individual = Auth::checkIndividualAccess($individual, true); 697c7d1e03SGreg Roach 70*efd4768bSGreg Roach $levels = $params['ilevels'] ?? []; 71*efd4768bSGreg Roach $tags = $params['itags'] ?? []; 72*efd4768bSGreg Roach $values = $params['ivalues'] ?? []; 737c7d1e03SGreg Roach 74*efd4768bSGreg Roach // Create the new child 75*efd4768bSGreg Roach $gedcom = "0 @@ INDI\n" . $this->gedcom_edit_service->editLinesToGedcom('INDI', $levels, $tags, $values); 767c7d1e03SGreg Roach $child = $tree->createIndividual($gedcom); 777c7d1e03SGreg Roach 78*efd4768bSGreg Roach // Create a new family 79*efd4768bSGreg Roach $link = $child->sex() === 'F' ? 'WIFE' : 'HUSB'; 80*efd4768bSGreg Roach $gedcom = "0 @@ FAM\n1 " . $link . " @" . $individual->xref() . "@\n1 CHIL @" . $child->xref() . '@'; 81*efd4768bSGreg Roach $family = $tree->createFamily($gedcom); 827c7d1e03SGreg Roach 83*efd4768bSGreg Roach // Link the individual to the family 84*efd4768bSGreg Roach $individual->createFact('1 FAMS @' . $family->xref() . '@', false); 857c7d1e03SGreg Roach 86*efd4768bSGreg Roach // Link the child to the family 87*efd4768bSGreg Roach $child->createFact('1 FAMC @' . $family->xref() . '@', false); 88*efd4768bSGreg Roach 89*efd4768bSGreg Roach return redirect($params['url'] ?? $child->url()); 907c7d1e03SGreg Roach } 917c7d1e03SGreg Roach} 92