xref: /webtrees/app/Http/RequestHandlers/AddChildToFamilyPage.php (revision b55cbc6b43247e8b2ad14af6f6d24dc6747195ff)
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;
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;
28*b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
297c7d1e03SGreg Roachuse Psr\Http\Message\ResponseInterface;
307c7d1e03SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
317c7d1e03SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
327c7d1e03SGreg Roach
337c7d1e03SGreg Roachuse function assert;
34efd4768bSGreg Roachuse function is_string;
35efd4768bSGreg Roachuse function route;
367c7d1e03SGreg Roach
377c7d1e03SGreg Roach/**
387c7d1e03SGreg Roach * Add a new child to a family.
397c7d1e03SGreg Roach */
407c7d1e03SGreg Roachclass AddChildToFamilyPage implements RequestHandlerInterface
417c7d1e03SGreg Roach{
427c7d1e03SGreg Roach    use ViewResponseTrait;
437c7d1e03SGreg Roach
449ba2a180SGreg Roach    private GedcomEditService $gedcom_edit_service;
459ba2a180SGreg Roach
469ba2a180SGreg Roach    /**
479ba2a180SGreg Roach     * LinkSpouseToIndividualPage constructor.
489ba2a180SGreg Roach     *
499ba2a180SGreg Roach     * @param GedcomEditService $gedcom_edit_service
509ba2a180SGreg Roach     */
519ba2a180SGreg Roach    public function __construct(GedcomEditService $gedcom_edit_service)
529ba2a180SGreg Roach    {
539ba2a180SGreg Roach        $this->gedcom_edit_service = $gedcom_edit_service;
549ba2a180SGreg Roach    }
559ba2a180SGreg Roach
567c7d1e03SGreg Roach    /**
577c7d1e03SGreg Roach     * @param ServerRequestInterface $request
587c7d1e03SGreg Roach     *
597c7d1e03SGreg Roach     * @return ResponseInterface
607c7d1e03SGreg Roach     */
617c7d1e03SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
627c7d1e03SGreg Roach    {
63*b55cbc6bSGreg Roach        $tree   = Validator::attributes($request)->tree();
64*b55cbc6bSGreg Roach        $xref   = Validator::attributes($request)->isXref()->string('xref');
65*b55cbc6bSGreg Roach        $sex    = Validator::attributes($request)->string('sex');
666b9cb339SGreg Roach        $family = Registry::familyFactory()->make($xref, $tree);
677c7d1e03SGreg Roach        $family = Auth::checkFamilyAccess($family, true);
687c7d1e03SGreg Roach
693a1e3ba4SGreg Roach        // Name facts.
70cb7a42eaSGreg Roach        $surname_tradition = SurnameTradition::create($tree->getPreference('SURNAME_TRADITION'));
71cb7a42eaSGreg Roach        $names             = $surname_tradition->newChildNames($family->husband(), $family->wife(), $sex);
723a1e3ba4SGreg Roach
73efd4768bSGreg Roach        $facts = [
746102047cSGreg Roach            'i' => $this->gedcom_edit_service->newIndividualFacts($tree, $sex, $names),
75efd4768bSGreg Roach        ];
767c7d1e03SGreg Roach
77efd4768bSGreg Roach        $titles = [
787c7d1e03SGreg Roach            'M' => I18N::translate('Add a son'),
797c7d1e03SGreg Roach            'F' => I18N::translate('Add a daughter'),
807c7d1e03SGreg Roach            'U' => I18N::translate('Add a child'),
817c7d1e03SGreg Roach        ];
827c7d1e03SGreg Roach
83efd4768bSGreg Roach        $title = $titles[$sex] ?? $titles['U'];
847c7d1e03SGreg Roach
857c7d1e03SGreg Roach        return $this->viewResponse('edit/new-individual', [
86efd4768bSGreg Roach            'cancel_url'          => $family->url(),
87efd4768bSGreg Roach            'facts'               => $facts,
889ba2a180SGreg Roach            'gedcom_edit_service' => $this->gedcom_edit_service,
89efd4768bSGreg Roach            'post_url'            => route(AddChildToFamilyAction::class, ['tree' => $tree->name(), 'xref' => $xref]),
90efd4768bSGreg Roach            'title'               => $family->fullName() . ' - ' . $title,
917c7d1e03SGreg Roach            'tree'                => $tree,
92efd4768bSGreg Roach            'url'                 => $request->getQueryParams()['url'] ?? $family->url(),
937c7d1e03SGreg Roach        ]);
947c7d1e03SGreg Roach    }
957c7d1e03SGreg Roach}
96