xref: /webtrees/app/Http/RequestHandlers/AddSpouseToIndividualPage.php (revision 99125a335d0be11b975e213c69fc4ede0c5303f1)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 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\Fact;
24use Fisharebest\Webtrees\Http\ViewResponseTrait;
25use Fisharebest\Webtrees\I18N;
26use Fisharebest\Webtrees\Registry;
27use Fisharebest\Webtrees\Tree;
28use Psr\Http\Message\ResponseInterface;
29use Psr\Http\Message\ServerRequestInterface;
30use Psr\Http\Server\RequestHandlerInterface;
31
32use function assert;
33use function is_string;
34use function route;
35
36/**
37 * Add a new spouse to an individual, creating a new family.
38 */
39class AddSpouseToIndividualPage implements RequestHandlerInterface
40{
41    use ViewResponseTrait;
42
43    // Create mixed-sex couples by default
44    private const OPPOSITE_SEX = [
45        'F' => 'M',
46        'M' => 'F',
47    ];
48
49    /**
50     * @param ServerRequestInterface $request
51     *
52     * @return ResponseInterface
53     */
54    public function handle(ServerRequestInterface $request): ResponseInterface
55    {
56        $tree = $request->getAttribute('tree');
57        assert($tree instanceof Tree);
58
59        $xref = $request->getAttribute('xref');
60        assert(is_string($xref));
61
62        $individual = Registry::individualFactory()->make($xref, $tree);
63        $individual = Auth::checkIndividualAccess($individual, true);
64
65        // Create a dummy individual, so that we can create new/empty facts.
66        $sex     = self::OPPOSITE_SEX[$individual->sex()] ?? 'U';
67        $element = Registry::elementFactory()->make('INDI:NAME');
68        $dummyi  = Registry::individualFactory()->new('', '0 @@ INDI', null, $tree);
69        $dummyf  = Registry::familyFactory()->new('', '0 @@ FAM', null, $tree);
70        $facts   = [
71            'i' => [
72                new Fact('1 SEX ' . $sex, $dummyi, ''),
73                new Fact('1 NAME ' . $element->default($tree), $dummyi, ''),
74                new Fact('1 BIRT', $dummyi, ''),
75                new Fact('1 DEAT', $dummyi, ''),
76            ],
77            'f' => [
78                new Fact('1 MARR', $dummyf, ''),
79            ],
80        ];
81
82        $titles = [
83            'F' => I18N::translate('Add a wife'),
84            'H' => I18N::translate('Add a husband'),
85            'U' => I18N::translate('Add a spouse'),
86        ];
87
88        $title = $titles[$sex] ?? $titles['U'];
89
90        return $this->viewResponse('edit/new-individual', [
91            'cancel_url' => $individual->url(),
92            'facts'      => $facts,
93            'post_url'   => route(AddSpouseToIndividualAction::class, ['tree' => $tree->name(), 'xref' => $xref]),
94            'title'      => $individual->fullName() . ' - ' . $title,
95            'tree'       => $tree,
96            'url'        => $request->getQueryParams()['url'] ?? $individual->url(),
97        ]);
98    }
99}
100