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; 23efd4768bSGreg Roachuse Fisharebest\Webtrees\Fact; 247c7d1e03SGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait; 257c7d1e03SGreg Roachuse Fisharebest\Webtrees\I18N; 266b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry; 279ba2a180SGreg Roachuse Fisharebest\Webtrees\Services\GedcomEditService; 28cb7a42eaSGreg Roachuse Fisharebest\Webtrees\SurnameTradition; 297c7d1e03SGreg Roachuse Fisharebest\Webtrees\Tree; 307c7d1e03SGreg Roachuse Psr\Http\Message\ResponseInterface; 317c7d1e03SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 327c7d1e03SGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 337c7d1e03SGreg Roach 34cb7a42eaSGreg Roachuse function array_map; 357c7d1e03SGreg Roachuse function assert; 36efd4768bSGreg Roachuse function is_string; 37efd4768bSGreg Roachuse function route; 387c7d1e03SGreg Roach 397c7d1e03SGreg Roach/** 407c7d1e03SGreg Roach * Add a new parent to an individual, creating a one-parent family. 417c7d1e03SGreg Roach */ 427c7d1e03SGreg Roachclass AddParentToIndividualPage implements RequestHandlerInterface 437c7d1e03SGreg Roach{ 447c7d1e03SGreg Roach use ViewResponseTrait; 457c7d1e03SGreg Roach 469ba2a180SGreg Roach private GedcomEditService $gedcom_edit_service; 479ba2a180SGreg Roach 489ba2a180SGreg Roach /** 499ba2a180SGreg Roach * LinkSpouseToIndividualPage constructor. 509ba2a180SGreg Roach * 519ba2a180SGreg Roach * @param GedcomEditService $gedcom_edit_service 529ba2a180SGreg Roach */ 539ba2a180SGreg Roach public function __construct(GedcomEditService $gedcom_edit_service) 549ba2a180SGreg Roach { 559ba2a180SGreg Roach $this->gedcom_edit_service = $gedcom_edit_service; 569ba2a180SGreg Roach } 579ba2a180SGreg Roach 587c7d1e03SGreg Roach /** 597c7d1e03SGreg Roach * @param ServerRequestInterface $request 607c7d1e03SGreg Roach * 617c7d1e03SGreg Roach * @return ResponseInterface 627c7d1e03SGreg Roach */ 637c7d1e03SGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 647c7d1e03SGreg Roach { 657c7d1e03SGreg Roach $tree = $request->getAttribute('tree'); 667c7d1e03SGreg Roach assert($tree instanceof Tree); 677c7d1e03SGreg Roach 68efd4768bSGreg Roach $xref = $request->getAttribute('xref'); 69efd4768bSGreg Roach assert(is_string($xref)); 70efd4768bSGreg Roach 71efd4768bSGreg Roach $sex = $request->getAttribute('sex'); 7241acc92bSGreg Roach assert(is_string($sex)); 737c7d1e03SGreg Roach 746b9cb339SGreg Roach $individual = Registry::individualFactory()->make($xref, $tree); 757c7d1e03SGreg Roach $individual = Auth::checkIndividualAccess($individual, true); 767c7d1e03SGreg Roach 77efd4768bSGreg Roach // Create a dummy individual, so that we can create new/empty facts. 78efd4768bSGreg Roach $dummy = Registry::individualFactory()->new('', '0 @@ INDI', null, $tree); 79cb7a42eaSGreg Roach 80*3a1e3ba4SGreg Roach // Name facts. 81cb7a42eaSGreg Roach $surname_tradition = SurnameTradition::create($tree->getPreference('SURNAME_TRADITION')); 82cb7a42eaSGreg Roach $names = $surname_tradition->newParentNames($individual, $sex); 8305babb96SGreg Roach $name_facts = array_map(static fn (string $gedcom): Fact => new Fact($gedcom, $dummy, ''), $names); 84cb7a42eaSGreg Roach 85*3a1e3ba4SGreg Roach // Individual facts and events. 86*3a1e3ba4SGreg Roach $quick_facts = explode(',', $tree->getPreference('QUICK_REQUIRED_FACTS')); 87*3a1e3ba4SGreg Roach $indi_facts = array_map(static fn(string $fact): Fact => new Fact('1 ' . $fact, $dummy, ''), $quick_facts); 88*3a1e3ba4SGreg Roach 89efd4768bSGreg Roach $facts = [ 90efd4768bSGreg Roach 'i' => [ 91efd4768bSGreg Roach new Fact('1 SEX ' . $sex, $dummy, ''), 92cb7a42eaSGreg Roach ...$name_facts, 93*3a1e3ba4SGreg Roach ...$indi_facts, 94efd4768bSGreg Roach ], 95efd4768bSGreg Roach ]; 967c7d1e03SGreg Roach 97efd4768bSGreg Roach if ($sex === 'F') { 98efd4768bSGreg Roach $title = I18N::translate('Add a mother'); 997c7d1e03SGreg Roach } else { 100efd4768bSGreg Roach $title = I18N::translate('Add a father'); 1017c7d1e03SGreg Roach } 1027c7d1e03SGreg Roach 1037c7d1e03SGreg Roach return $this->viewResponse('edit/new-individual', [ 104efd4768bSGreg Roach 'cancel_url' => $individual->url(), 105efd4768bSGreg Roach 'facts' => $facts, 1069ba2a180SGreg Roach 'gedcom_edit_service' => $this->gedcom_edit_service, 107efd4768bSGreg Roach 'post_url' => route(AddParentToIndividualAction::class, ['tree' => $tree->name(), 'xref' => $xref]), 108efd4768bSGreg Roach 'title' => $individual->fullName() . ' - ' . $title, 1097c7d1e03SGreg Roach 'tree' => $tree, 110efd4768bSGreg Roach 'url' => $request->getQueryParams()['url'] ?? $individual->url(), 1117c7d1e03SGreg Roach ]); 1127c7d1e03SGreg Roach } 1137c7d1e03SGreg Roach} 114