xref: /webtrees/app/Http/RequestHandlers/AddSpouseToIndividualPage.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;
256b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
269ba2a180SGreg Roachuse Fisharebest\Webtrees\Services\GedcomEditService;
27b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
287c7d1e03SGreg Roachuse Psr\Http\Message\ResponseInterface;
297c7d1e03SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
307c7d1e03SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
317c7d1e03SGreg Roach
32efd4768bSGreg Roachuse function route;
337c7d1e03SGreg Roach
347c7d1e03SGreg Roach/**
357c7d1e03SGreg Roach * Add a new spouse to an individual, creating a new family.
367c7d1e03SGreg Roach */
377c7d1e03SGreg Roachclass AddSpouseToIndividualPage implements RequestHandlerInterface
387c7d1e03SGreg Roach{
397c7d1e03SGreg Roach    use ViewResponseTrait;
407c7d1e03SGreg Roach
41efd4768bSGreg Roach    // Create mixed-sex couples by default
42efd4768bSGreg Roach    private const OPPOSITE_SEX = [
43efd4768bSGreg Roach        'F' => 'M',
44efd4768bSGreg Roach        'M' => 'F',
4580b86810SGreg Roach        'U' => 'U',
46e73150afSGreg Roach        'X' => 'U',
47efd4768bSGreg Roach    ];
48efd4768bSGreg Roach
499ba2a180SGreg Roach    private GedcomEditService $gedcom_edit_service;
509ba2a180SGreg Roach
519ba2a180SGreg Roach    /**
529ba2a180SGreg Roach     * @param GedcomEditService $gedcom_edit_service
539ba2a180SGreg Roach     */
549ba2a180SGreg Roach    public function __construct(GedcomEditService $gedcom_edit_service)
559ba2a180SGreg Roach    {
569ba2a180SGreg Roach        $this->gedcom_edit_service = $gedcom_edit_service;
579ba2a180SGreg Roach    }
589ba2a180SGreg Roach
597c7d1e03SGreg Roach    /**
607c7d1e03SGreg Roach     * @param ServerRequestInterface $request
617c7d1e03SGreg Roach     *
627c7d1e03SGreg Roach     * @return ResponseInterface
637c7d1e03SGreg Roach     */
647c7d1e03SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
657c7d1e03SGreg Roach    {
66b55cbc6bSGreg Roach        $tree       = Validator::attributes($request)->tree();
67b55cbc6bSGreg Roach        $xref       = Validator::attributes($request)->isXref()->string('xref');
686b9cb339SGreg Roach        $individual = Registry::individualFactory()->make($xref, $tree);
697c7d1e03SGreg Roach        $individual = Auth::checkIndividualAccess($individual, true);
707c7d1e03SGreg Roach
7180b86810SGreg Roach        $sex = self::OPPOSITE_SEX[$individual->sex()];
72cb7a42eaSGreg Roach
733a1e3ba4SGreg Roach        // Name facts.
747e128bbfSGreg Roach        $surname_tradition = Registry::surnameTraditionFactory()
757e128bbfSGreg Roach            ->make($tree->getPreference('SURNAME_TRADITION'));
767e128bbfSGreg Roach
77cb7a42eaSGreg Roach        $names = $surname_tradition->newSpouseNames($individual, $sex);
78cb7a42eaSGreg Roach
79efd4768bSGreg Roach        $facts = [
80e73150afSGreg Roach            'i' => $this->gedcom_edit_service->newIndividualFacts($tree, $sex, $names),
816102047cSGreg Roach            'f' => $this->gedcom_edit_service->newFamilyFacts($tree),
82efd4768bSGreg Roach        ];
83efd4768bSGreg Roach
84efd4768bSGreg Roach        $titles = [
85efd4768bSGreg Roach            'F' => I18N::translate('Add a wife'),
8680b86810SGreg Roach            'M' => I18N::translate('Add a husband'),
87efd4768bSGreg Roach            'U' => I18N::translate('Add a spouse'),
88efd4768bSGreg Roach        ];
89efd4768bSGreg Roach
90d8809d62SGreg Roach        $title = $titles[$sex];
917c7d1e03SGreg Roach
927c7d1e03SGreg Roach        return $this->viewResponse('edit/new-individual', [
93efd4768bSGreg Roach            'facts'               => $facts,
949ba2a180SGreg Roach            'gedcom_edit_service' => $this->gedcom_edit_service,
95efd4768bSGreg Roach            'post_url'            => route(AddSpouseToIndividualAction::class, ['tree' => $tree->name(), 'xref' => $xref]),
96efd4768bSGreg Roach            'title'               => $individual->fullName() . ' - ' . $title,
977c7d1e03SGreg Roach            'tree'                => $tree,
989f0bdfcdSGreg Roach            'url'                 => Validator::queryParams($request)->isLocalUrl()->string('url', $individual->url()),
997c7d1e03SGreg Roach        ]);
1007c7d1e03SGreg Roach    }
1017c7d1e03SGreg Roach}
102