xref: /webtrees/app/Http/RequestHandlers/AddSpouseToIndividualPage.php (revision 5bfc689774bb9a6401271c4ed15a6d50652c991b)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2022 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22use Fisharebest\Webtrees\Auth;
23use Fisharebest\Webtrees\Http\ViewResponseTrait;
24use Fisharebest\Webtrees\I18N;
25use Fisharebest\Webtrees\Registry;
26use Fisharebest\Webtrees\Services\GedcomEditService;
27use Fisharebest\Webtrees\SurnameTradition;
28use Fisharebest\Webtrees\Validator;
29use Psr\Http\Message\ResponseInterface;
30use Psr\Http\Message\ServerRequestInterface;
31use Psr\Http\Server\RequestHandlerInterface;
32
33use function route;
34
35/**
36 * Add a new spouse to an individual, creating a new family.
37 */
38class AddSpouseToIndividualPage implements RequestHandlerInterface
39{
40    use ViewResponseTrait;
41
42    // Create mixed-sex couples by default
43    private const OPPOSITE_SEX = [
44        'F' => 'M',
45        'M' => 'F',
46        'U' => 'U',
47        'X' => 'U',
48    ];
49
50    private GedcomEditService $gedcom_edit_service;
51
52    /**
53     * LinkSpouseToIndividualPage constructor.
54     *
55     * @param GedcomEditService $gedcom_edit_service
56     */
57    public function __construct(GedcomEditService $gedcom_edit_service)
58    {
59        $this->gedcom_edit_service = $gedcom_edit_service;
60    }
61
62    /**
63     * @param ServerRequestInterface $request
64     *
65     * @return ResponseInterface
66     */
67    public function handle(ServerRequestInterface $request): ResponseInterface
68    {
69        $tree       = Validator::attributes($request)->tree();
70        $xref       = Validator::attributes($request)->isXref()->string('xref');
71        $individual = Registry::individualFactory()->make($xref, $tree);
72        $individual = Auth::checkIndividualAccess($individual, true);
73
74        $sex = self::OPPOSITE_SEX[$individual->sex()];
75
76        // Name facts.
77        $surname_tradition = SurnameTradition::create($tree->getPreference('SURNAME_TRADITION'));
78        $names             = $surname_tradition->newSpouseNames($individual, $sex);
79
80        $facts = [
81            'i' => $this->gedcom_edit_service->newIndividualFacts($tree, $sex, $names),
82            'f' => $this->gedcom_edit_service->newFamilyFacts($tree),
83        ];
84
85        $titles = [
86            'F' => I18N::translate('Add a wife'),
87            'M' => I18N::translate('Add a husband'),
88            'U' => I18N::translate('Add a spouse'),
89        ];
90
91        $title = $titles[$sex];
92
93        return $this->viewResponse('edit/new-individual', [
94            'cancel_url'          => $individual->url(),
95            'facts'               => $facts,
96            'gedcom_edit_service' => $this->gedcom_edit_service,
97            'post_url'            => route(AddSpouseToIndividualAction::class, ['tree' => $tree->name(), 'xref' => $xref]),
98            'title'               => $individual->fullName() . ' - ' . $title,
99            'tree'                => $tree,
100            'url'                 => $request->getQueryParams()['url'] ?? $individual->url(),
101        ]);
102    }
103}
104