xref: /webtrees/app/Http/RequestHandlers/AddChildToIndividualPage.php (revision 9f0bdfcdb2cc634a8fc900f60eba68ec6c5d37fd)
17c7d1e03SGreg Roach<?php
27c7d1e03SGreg Roach
37c7d1e03SGreg Roach/**
47c7d1e03SGreg Roach * webtrees: online genealogy
55bfc6897SGreg Roach * Copyright (C) 2022 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;
237c7d1e03SGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
247c7d1e03SGreg Roachuse Fisharebest\Webtrees\I18N;
256b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
269ba2a180SGreg Roachuse Fisharebest\Webtrees\Services\GedcomEditService;
27cb7a42eaSGreg Roachuse Fisharebest\Webtrees\SurnameTradition;
28b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
297c7d1e03SGreg Roachuse Psr\Http\Message\ResponseInterface;
307c7d1e03SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
317c7d1e03SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
327c7d1e03SGreg Roach
33efd4768bSGreg Roachuse function route;
347c7d1e03SGreg Roach
357c7d1e03SGreg Roach/**
367c7d1e03SGreg Roach * Add a new child to an individual, creating a one-parent family.
377c7d1e03SGreg Roach */
387c7d1e03SGreg Roachclass AddChildToIndividualPage implements RequestHandlerInterface
397c7d1e03SGreg Roach{
407c7d1e03SGreg Roach    use ViewResponseTrait;
417c7d1e03SGreg Roach
429ba2a180SGreg Roach    private GedcomEditService $gedcom_edit_service;
439ba2a180SGreg Roach
449ba2a180SGreg Roach    /**
459ba2a180SGreg Roach     * LinkSpouseToIndividualPage constructor.
469ba2a180SGreg Roach     *
479ba2a180SGreg Roach     * @param GedcomEditService $gedcom_edit_service
489ba2a180SGreg Roach     */
499ba2a180SGreg Roach    public function __construct(GedcomEditService $gedcom_edit_service)
509ba2a180SGreg Roach    {
519ba2a180SGreg Roach        $this->gedcom_edit_service = $gedcom_edit_service;
529ba2a180SGreg Roach    }
539ba2a180SGreg Roach
547c7d1e03SGreg Roach    /**
557c7d1e03SGreg Roach     * @param ServerRequestInterface $request
567c7d1e03SGreg Roach     *
577c7d1e03SGreg Roach     * @return ResponseInterface
587c7d1e03SGreg Roach     */
597c7d1e03SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
607c7d1e03SGreg Roach    {
61b55cbc6bSGreg Roach        $tree       = Validator::attributes($request)->tree();
62b55cbc6bSGreg Roach        $xref       = Validator::attributes($request)->isXref()->string('xref');
636b9cb339SGreg Roach        $individual = Registry::individualFactory()->make($xref, $tree);
647c7d1e03SGreg Roach        $individual = Auth::checkIndividualAccess($individual, true);
657c7d1e03SGreg Roach
663a1e3ba4SGreg Roach        // Name facts.
67cb7a42eaSGreg Roach        $surname_tradition = SurnameTradition::create($tree->getPreference('SURNAME_TRADITION'));
68cb7a42eaSGreg Roach
69cb7a42eaSGreg Roach        switch ($individual->sex()) {
70cb7a42eaSGreg Roach            case 'M':
71cb7a42eaSGreg Roach                $names = $surname_tradition->newChildNames($individual, null, 'U');
72cb7a42eaSGreg Roach                break;
73cb7a42eaSGreg Roach
74cb7a42eaSGreg Roach            case 'F':
75cb7a42eaSGreg Roach                $names = $surname_tradition->newChildNames(null, $individual, 'U');
76cb7a42eaSGreg Roach                break;
77cb7a42eaSGreg Roach
78cb7a42eaSGreg Roach            default:
79cb7a42eaSGreg Roach                $names = $surname_tradition->newChildNames(null, null, 'U');
80cb7a42eaSGreg Roach                break;
81cb7a42eaSGreg Roach        }
82cb7a42eaSGreg Roach
83efd4768bSGreg Roach        $facts = [
846102047cSGreg Roach            'i' => $this->gedcom_edit_service->newIndividualFacts($tree, 'U', $names),
85efd4768bSGreg Roach        ];
86efd4768bSGreg Roach
87efd4768bSGreg Roach        $title = I18N::translate('Add a child to create a one-parent family');
887c7d1e03SGreg Roach
897c7d1e03SGreg Roach        return $this->viewResponse('edit/new-individual', [
90efd4768bSGreg Roach            'cancel_url'          => $individual->url(),
91efd4768bSGreg Roach            'facts'               => $facts,
929ba2a180SGreg Roach            'gedcom_edit_service' => $this->gedcom_edit_service,
93efd4768bSGreg Roach            'post_url'            => route(AddChildToIndividualAction::class, ['tree' => $tree->name(), 'xref' => $xref]),
94efd4768bSGreg Roach            'title'               => $individual->fullName() . ' - ' . $title,
957c7d1e03SGreg Roach            'tree'                => $tree,
96*9f0bdfcdSGreg Roach            'url'                 => Validator::queryParams($request)->isLocalUrl()->string('url', $individual->url()),
977c7d1e03SGreg Roach        ]);
987c7d1e03SGreg Roach    }
997c7d1e03SGreg Roach}
100