xref: /webtrees/app/Http/RequestHandlers/AddSpouseToFamilyPage.php (revision e93a8df2f8d797005750082cc3766c0e80799688)
17c7d1e03SGreg Roach<?php
27c7d1e03SGreg Roach
37c7d1e03SGreg Roach/**
47c7d1e03SGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 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;
25cb7a42eaSGreg Roachuse Fisharebest\Webtrees\Individual;
266b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
279ba2a180SGreg Roachuse Fisharebest\Webtrees\Services\GedcomEditService;
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 spouse to a family.
377c7d1e03SGreg Roach */
387c7d1e03SGreg Roachclass AddSpouseToFamilyPage implements RequestHandlerInterface
397c7d1e03SGreg Roach{
407c7d1e03SGreg Roach    use ViewResponseTrait;
417c7d1e03SGreg Roach
429ba2a180SGreg Roach    private GedcomEditService $gedcom_edit_service;
439ba2a180SGreg Roach
449ba2a180SGreg Roach    /**
459ba2a180SGreg Roach     * @param GedcomEditService $gedcom_edit_service
469ba2a180SGreg Roach     */
479ba2a180SGreg Roach    public function __construct(GedcomEditService $gedcom_edit_service)
489ba2a180SGreg Roach    {
499ba2a180SGreg Roach        $this->gedcom_edit_service = $gedcom_edit_service;
509ba2a180SGreg Roach    }
519ba2a180SGreg 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    {
59b55cbc6bSGreg Roach        $tree   = Validator::attributes($request)->tree();
60b55cbc6bSGreg Roach        $xref   = Validator::attributes($request)->isXref()->string('xref');
61b55cbc6bSGreg Roach        $sex    = Validator::attributes($request)->string('sex');
626b9cb339SGreg Roach        $family = Registry::familyFactory()->make($xref, $tree);
637c7d1e03SGreg Roach        $family = Auth::checkFamilyAccess($family, true);
647c7d1e03SGreg Roach
653a1e3ba4SGreg Roach        // Name facts.
667e128bbfSGreg Roach        $surname_tradition = Registry::surnameTraditionFactory()
677e128bbfSGreg Roach            ->make($tree->getPreference('SURNAME_TRADITION'));
687e128bbfSGreg Roach
69cb7a42eaSGreg Roach        $spouse = $family->spouses()->first();
707e128bbfSGreg Roach
71e5ff1819SGreg Roach        if ($spouse instanceof Individual) {
72cb7a42eaSGreg Roach            $names = $surname_tradition->newSpouseNames($spouse, $sex);
73e5ff1819SGreg Roach        } else {
74e5ff1819SGreg Roach            $names = ['1 NAME ' . $surname_tradition->defaultName()];
75e5ff1819SGreg Roach        }
763a1e3ba4SGreg Roach
77efd4768bSGreg Roach        $facts = [
786102047cSGreg Roach            'i' => $this->gedcom_edit_service->newIndividualFacts($tree, $sex, $names),
796102047cSGreg Roach            'f' => $this->gedcom_edit_service->newFamilyFacts($tree),
80efd4768bSGreg Roach        ];
81efd4768bSGreg Roach
82efd4768bSGreg Roach        if ($sex === 'F') {
837c7d1e03SGreg Roach            $title = I18N::translate('Add a wife');
847c7d1e03SGreg Roach        } else {
857c7d1e03SGreg Roach            $title = I18N::translate('Add a husband');
867c7d1e03SGreg Roach        }
877c7d1e03SGreg Roach
887c7d1e03SGreg Roach        return $this->viewResponse('edit/new-individual', [
89efd4768bSGreg Roach            'facts'               => $facts,
909ba2a180SGreg Roach            'gedcom_edit_service' => $this->gedcom_edit_service,
91efd4768bSGreg Roach            'post_url'            => route(AddSpouseToFamilyAction::class, ['tree' => $tree->name(), 'xref' => $xref]),
927c7d1e03SGreg Roach            'title'               => $title,
93efd4768bSGreg Roach            'tree'                => $tree,
949f0bdfcdSGreg Roach            'url'                 => Validator::queryParams($request)->isLocalUrl()->string('url', $family->url()),
957c7d1e03SGreg Roach        ]);
967c7d1e03SGreg Roach    }
977c7d1e03SGreg Roach}
98