Issues (2503)

Http/RequestHandlers/AddSpouseToIndividualPage.php (1 issue)

Labels
Severity
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2025 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
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22
use Fisharebest\Webtrees\Auth;
23
use Fisharebest\Webtrees\Http\ViewResponseTrait;
24
use Fisharebest\Webtrees\I18N;
25
use Fisharebest\Webtrees\Registry;
26
use Fisharebest\Webtrees\Services\GedcomEditService;
27
use Fisharebest\Webtrees\Validator;
28
use Psr\Http\Message\ResponseInterface;
29
use Psr\Http\Message\ServerRequestInterface;
30
use Psr\Http\Server\RequestHandlerInterface;
31
32
use function route;
33
34
final class AddSpouseToIndividualPage implements RequestHandlerInterface
35
{
36
    use ViewResponseTrait;
37
38
    // Create mixed-sex couples by default
39
    private const array OPPOSITE_SEX = [
0 ignored issues
show
A parse error occurred: Syntax error, unexpected T_STRING, expecting '=' on line 39 at column 24
Loading history...
40
        'F' => 'M',
41
        'M' => 'F',
42
        'U' => 'U',
43
        'X' => 'U',
44
    ];
45
46
    public function __construct(
47
        private readonly GedcomEditService $gedcom_edit_service,
48
    ) {
49
    }
50
51
    public function handle(ServerRequestInterface $request): ResponseInterface
52
    {
53
        $tree       = Validator::attributes($request)->tree();
54
        $xref       = Validator::attributes($request)->isXref()->string('xref');
55
        $individual = Registry::individualFactory()->make($xref, $tree);
56
        $individual = Auth::checkIndividualAccess($individual, true);
57
58
        $sex = self::OPPOSITE_SEX[$individual->sex()];
59
60
        // Name facts.
61
        $surname_tradition = Registry::surnameTraditionFactory()
62
            ->make($tree->getPreference('SURNAME_TRADITION'));
63
64
        $names = $surname_tradition->newSpouseNames($individual, $sex);
65
66
        $facts = [
67
            'i' => $this->gedcom_edit_service->newIndividualFacts($tree, $sex, $names),
68
            'f' => $this->gedcom_edit_service->newFamilyFacts($tree),
69
        ];
70
71
        $titles = [
72
            'F' => I18N::translate('Add a wife'),
73
            'M' => I18N::translate('Add a husband'),
74
            'U' => I18N::translate('Add a spouse'),
75
        ];
76
77
        $title = $titles[$sex];
78
79
        return $this->viewResponse('edit/new-individual', [
80
            'facts'               => $facts,
81
            'gedcom_edit_service' => $this->gedcom_edit_service,
82
            'post_url'            => route(AddSpouseToIndividualAction::class, ['tree' => $tree->name(), 'xref' => $xref]),
83
            'title'               => $individual->fullName() . ' - ' . $title,
84
            'tree'                => $tree,
85
            'url'                 => Validator::queryParams($request)->isLocalUrl()->string('url', $individual->url()),
86
        ]);
87
    }
88
}
89