xref: /webtrees/app/Http/RequestHandlers/AddParentToIndividualPage.php (revision cb7a42eae1efabac96d9d7693151fe0421b6717b)
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;
27*cb7a42eaSGreg Roachuse Fisharebest\Webtrees\SurnameTradition;
287c7d1e03SGreg Roachuse Fisharebest\Webtrees\Tree;
297c7d1e03SGreg Roachuse Psr\Http\Message\ResponseInterface;
307c7d1e03SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
317c7d1e03SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
327c7d1e03SGreg Roach
33*cb7a42eaSGreg Roachuse function array_map;
347c7d1e03SGreg Roachuse function assert;
35efd4768bSGreg Roachuse function is_string;
36efd4768bSGreg Roachuse function route;
377c7d1e03SGreg Roach
387c7d1e03SGreg Roach/**
397c7d1e03SGreg Roach * Add a new parent to an individual, creating a one-parent family.
407c7d1e03SGreg Roach */
417c7d1e03SGreg Roachclass AddParentToIndividualPage implements RequestHandlerInterface
427c7d1e03SGreg Roach{
437c7d1e03SGreg Roach    use ViewResponseTrait;
447c7d1e03SGreg Roach
457c7d1e03SGreg Roach    /**
467c7d1e03SGreg Roach     * @param ServerRequestInterface $request
477c7d1e03SGreg Roach     *
487c7d1e03SGreg Roach     * @return ResponseInterface
497c7d1e03SGreg Roach     */
507c7d1e03SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
517c7d1e03SGreg Roach    {
527c7d1e03SGreg Roach        $tree = $request->getAttribute('tree');
537c7d1e03SGreg Roach        assert($tree instanceof Tree);
547c7d1e03SGreg Roach
55efd4768bSGreg Roach        $xref = $request->getAttribute('xref');
56efd4768bSGreg Roach        assert(is_string($xref));
57efd4768bSGreg Roach
58efd4768bSGreg Roach        $sex = $request->getAttribute('sex');
59efd4768bSGreg Roach        assert(is_string($xref));
607c7d1e03SGreg Roach
616b9cb339SGreg Roach        $individual = Registry::individualFactory()->make($xref, $tree);
627c7d1e03SGreg Roach        $individual = Auth::checkIndividualAccess($individual, true);
637c7d1e03SGreg Roach
64efd4768bSGreg Roach        // Create a dummy individual, so that we can create new/empty facts.
65efd4768bSGreg Roach        $dummy   = Registry::individualFactory()->new('', '0 @@ INDI', null, $tree);
66*cb7a42eaSGreg Roach
67*cb7a42eaSGreg Roach        // Default names facts.
68*cb7a42eaSGreg Roach        $surname_tradition = SurnameTradition::create($tree->getPreference('SURNAME_TRADITION'));
69*cb7a42eaSGreg Roach        $names             = $surname_tradition->newParentNames($individual, $sex);
70*cb7a42eaSGreg Roach        $name_facts        = array_map(fn (string $gedcom): Fact => new Fact($gedcom, $dummy, ''), $names);
71*cb7a42eaSGreg Roach
72efd4768bSGreg Roach        $facts   = [
73efd4768bSGreg Roach            'i' => [
74efd4768bSGreg Roach                new Fact('1 SEX ' . $sex, $dummy, ''),
75*cb7a42eaSGreg Roach                ...$name_facts,
76efd4768bSGreg Roach                new Fact('1 BIRT', $dummy, ''),
77efd4768bSGreg Roach                new Fact('1 DEAT', $dummy, ''),
78efd4768bSGreg Roach            ],
79efd4768bSGreg Roach        ];
807c7d1e03SGreg Roach
81efd4768bSGreg Roach        if ($sex === 'F') {
82efd4768bSGreg Roach            $title = I18N::translate('Add a mother');
837c7d1e03SGreg Roach        } else {
84efd4768bSGreg Roach            $title = I18N::translate('Add a father');
857c7d1e03SGreg Roach        }
867c7d1e03SGreg Roach
877c7d1e03SGreg Roach        return $this->viewResponse('edit/new-individual', [
88efd4768bSGreg Roach            'cancel_url' => $individual->url(),
89efd4768bSGreg Roach            'facts'      => $facts,
90efd4768bSGreg Roach            'post_url'   => route(AddParentToIndividualAction::class, ['tree' => $tree->name(), 'xref' => $xref]),
91efd4768bSGreg Roach            'title'      => $individual->fullName() . ' - ' . $title,
927c7d1e03SGreg Roach            'tree'       => $tree,
93efd4768bSGreg Roach            'url'        => $request->getQueryParams()['url'] ?? $individual->url(),
947c7d1e03SGreg Roach        ]);
957c7d1e03SGreg Roach    }
967c7d1e03SGreg Roach}
97