xref: /webtrees/app/Http/RequestHandlers/AddSpouseToFamilyAction.php (revision f1d11eefe3d88b0296ccacf33ecbbebbaf2a5143)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2020 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 <http://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\Factory;
25use Fisharebest\Webtrees\Services\GedcomEditService;
26use Fisharebest\Webtrees\Tree;
27use Psr\Http\Message\ResponseInterface;
28use Psr\Http\Message\ServerRequestInterface;
29use Psr\Http\Server\RequestHandlerInterface;
30
31use function assert;
32use function preg_match_all;
33use function redirect;
34use function trim;
35
36/**
37 * Add a new spouse to a family.
38 */
39class AddSpouseToFamilyAction implements RequestHandlerInterface
40{
41    /** @var GedcomEditService */
42    private $gedcom_edit_service;
43
44    /**
45     * AddChildToFamilyAction constructor.
46     *
47     * @param GedcomEditService $gedcom_edit_service
48     */
49    public function __construct(GedcomEditService $gedcom_edit_service)
50    {
51        $this->gedcom_edit_service = $gedcom_edit_service;
52    }
53
54    /**
55     * @param ServerRequestInterface $request
56     *
57     * @return ResponseInterface
58     */
59    public function handle(ServerRequestInterface $request): ResponseInterface
60    {
61        $tree = $request->getAttribute('tree');
62        assert($tree instanceof Tree);
63
64        $xref   = $request->getQueryParams()['xref'];
65        $family = Factory::family()->make($xref, $tree);
66        $family = Auth::checkFamilyAccess($family, true);
67
68        $params = (array) $request->getParsedBody();
69
70        $this->gedcom_edit_service->glevels = $params['glevels'] ?? [];
71        $this->gedcom_edit_service->tag     = $params['tag'] ?? [];
72        $this->gedcom_edit_service->text    = $params['text'] ?? [];
73        $this->gedcom_edit_service->islink  = $params['islink'] ?? [];
74
75        // Create the new spouse
76        $this->gedcom_edit_service->splitSource(); // separate SOUR record from the rest
77
78        $gedrec = '0 @@ INDI';
79        $gedrec .= $this->gedcom_edit_service->addNewName($request, $tree);
80        $gedrec .= $this->gedcom_edit_service->addNewSex($request);
81        if (preg_match_all('/([A-Z0-9_]+)/', $tree->getPreference('QUICK_REQUIRED_FACTS'), $matches)) {
82            foreach ($matches[1] as $match) {
83                $gedrec .= $this->gedcom_edit_service->addNewFact($request, $tree, $match);
84            }
85        }
86
87        if ($params['SOUR_INDI'] ?? false) {
88            $gedrec = $this->gedcom_edit_service->handleUpdates($gedrec);
89        } else {
90            $gedrec = $this->gedcom_edit_service->updateRest($gedrec);
91        }
92        $gedrec .= "\n1 FAMS @" . $family->xref() . '@';
93        $spouse = $tree->createIndividual($gedrec);
94
95        // Update the existing family - add marriage, etc
96        if ($family->facts(['HUSB'])->first() instanceof Fact) {
97            $family->createFact('1 WIFE @' . $spouse->xref() . '@', true);
98        } else {
99            $family->createFact('1 HUSB @' . $spouse->xref() . '@', true);
100        }
101        $famrec = '';
102        if (preg_match_all('/([A-Z0-9_]+)/', $tree->getPreference('QUICK_REQUIRED_FAMFACTS'), $matches)) {
103            foreach ($matches[1] as $match) {
104                $famrec .= $this->gedcom_edit_service->addNewFact($request, $tree, $match);
105            }
106        }
107        if ($params['SOUR_FAM'] ?? false) {
108            $famrec = $this->gedcom_edit_service->handleUpdates($famrec);
109        } else {
110            $famrec = $this->gedcom_edit_service->updateRest($famrec);
111        }
112        $family->createFact(trim($famrec), true); // trim leading \n
113
114        if (($params['goto'] ?? '') === 'new') {
115            return redirect($spouse->url());
116        }
117
118        return redirect($family->url());
119    }
120}
121