xref: /webtrees/app/Http/RequestHandlers/AddSpouseToFamilyAction.php (revision 7c7d1e03066c50e172e4a6239e322c1e0cd62040)
1*7c7d1e03SGreg Roach<?php
2*7c7d1e03SGreg Roach
3*7c7d1e03SGreg Roach/**
4*7c7d1e03SGreg Roach * webtrees: online genealogy
5*7c7d1e03SGreg Roach * Copyright (C) 2020 webtrees development team
6*7c7d1e03SGreg Roach * This program is free software: you can redistribute it and/or modify
7*7c7d1e03SGreg Roach * it under the terms of the GNU General Public License as published by
8*7c7d1e03SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9*7c7d1e03SGreg Roach * (at your option) any later version.
10*7c7d1e03SGreg Roach * This program is distributed in the hope that it will be useful,
11*7c7d1e03SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12*7c7d1e03SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13*7c7d1e03SGreg Roach * GNU General Public License for more details.
14*7c7d1e03SGreg Roach * You should have received a copy of the GNU General Public License
15*7c7d1e03SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
16*7c7d1e03SGreg Roach */
17*7c7d1e03SGreg Roach
18*7c7d1e03SGreg Roachdeclare(strict_types=1);
19*7c7d1e03SGreg Roach
20*7c7d1e03SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
21*7c7d1e03SGreg Roach
22*7c7d1e03SGreg Roachuse Fisharebest\Webtrees\Auth;
23*7c7d1e03SGreg Roachuse Fisharebest\Webtrees\Fact;
24*7c7d1e03SGreg Roachuse Fisharebest\Webtrees\Factory;
25*7c7d1e03SGreg Roachuse Fisharebest\Webtrees\Services\GedcomEditService;
26*7c7d1e03SGreg Roachuse Fisharebest\Webtrees\Tree;
27*7c7d1e03SGreg Roachuse Psr\Http\Message\ResponseInterface;
28*7c7d1e03SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
29*7c7d1e03SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
30*7c7d1e03SGreg Roach
31*7c7d1e03SGreg Roachuse function assert;
32*7c7d1e03SGreg Roachuse function preg_match_all;
33*7c7d1e03SGreg Roachuse function redirect;
34*7c7d1e03SGreg Roachuse function trim;
35*7c7d1e03SGreg Roach
36*7c7d1e03SGreg Roach/**
37*7c7d1e03SGreg Roach * Add a new spouse to a family.
38*7c7d1e03SGreg Roach */
39*7c7d1e03SGreg Roachclass AddSpouseToFamilyAction implements RequestHandlerInterface
40*7c7d1e03SGreg Roach{
41*7c7d1e03SGreg Roach    /** @var GedcomEditService */
42*7c7d1e03SGreg Roach    private $gedcom_edit_service;
43*7c7d1e03SGreg Roach
44*7c7d1e03SGreg Roach    /**
45*7c7d1e03SGreg Roach     * AddChildToFamilyAction constructor.
46*7c7d1e03SGreg Roach     *
47*7c7d1e03SGreg Roach     * @param GedcomEditService $gedcom_edit_service
48*7c7d1e03SGreg Roach     */
49*7c7d1e03SGreg Roach    public function __construct(GedcomEditService $gedcom_edit_service)
50*7c7d1e03SGreg Roach    {
51*7c7d1e03SGreg Roach        $this->gedcom_edit_service = $gedcom_edit_service;
52*7c7d1e03SGreg Roach    }
53*7c7d1e03SGreg Roach
54*7c7d1e03SGreg Roach    /**
55*7c7d1e03SGreg Roach     * @param ServerRequestInterface $request
56*7c7d1e03SGreg Roach     *
57*7c7d1e03SGreg Roach     * @return ResponseInterface
58*7c7d1e03SGreg Roach     */
59*7c7d1e03SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
60*7c7d1e03SGreg Roach    {
61*7c7d1e03SGreg Roach        $tree = $request->getAttribute('tree');
62*7c7d1e03SGreg Roach        assert($tree instanceof Tree);
63*7c7d1e03SGreg Roach
64*7c7d1e03SGreg Roach        $xref   = $request->getQueryParams()['xref'];
65*7c7d1e03SGreg Roach        $family = Factory::family()->make($xref, $tree);
66*7c7d1e03SGreg Roach        $family = Auth::checkFamilyAccess($family, true);
67*7c7d1e03SGreg Roach
68*7c7d1e03SGreg Roach        $params = (array) $request->getParsedBody();
69*7c7d1e03SGreg Roach
70*7c7d1e03SGreg Roach        $this->gedcom_edit_service->glevels = $params['glevels'] ?? [];
71*7c7d1e03SGreg Roach        $this->gedcom_edit_service->tag     = $params['tag'] ?? [];
72*7c7d1e03SGreg Roach        $this->gedcom_edit_service->text    = $params['text'] ?? [];
73*7c7d1e03SGreg Roach        $this->gedcom_edit_service->islink  = $params['islink'] ?? [];
74*7c7d1e03SGreg Roach
75*7c7d1e03SGreg Roach        // Create the new spouse
76*7c7d1e03SGreg Roach        $this->gedcom_edit_service->splitSource(); // separate SOUR record from the rest
77*7c7d1e03SGreg Roach
78*7c7d1e03SGreg Roach        $gedrec = '0 @@ INDI';
79*7c7d1e03SGreg Roach        $gedrec .= $this->gedcom_edit_service->addNewName($request, $tree);
80*7c7d1e03SGreg Roach        $gedrec .= $this->gedcom_edit_service->addNewSex($request);
81*7c7d1e03SGreg Roach        if (preg_match_all('/([A-Z0-9_]+)/', $tree->getPreference('QUICK_REQUIRED_FACTS'), $matches)) {
82*7c7d1e03SGreg Roach            foreach ($matches[1] as $match) {
83*7c7d1e03SGreg Roach                $gedrec .= $this->gedcom_edit_service->addNewFact($request, $tree, $match);
84*7c7d1e03SGreg Roach            }
85*7c7d1e03SGreg Roach        }
86*7c7d1e03SGreg Roach
87*7c7d1e03SGreg Roach        if ($params['SOUR_INDI'] ?? false) {
88*7c7d1e03SGreg Roach            $gedrec = $this->gedcom_edit_service->handleUpdates($gedrec);
89*7c7d1e03SGreg Roach        } else {
90*7c7d1e03SGreg Roach            $gedrec = $this->gedcom_edit_service->updateRest($gedrec);
91*7c7d1e03SGreg Roach        }
92*7c7d1e03SGreg Roach        $gedrec .= "\n1 FAMS @" . $family->xref() . '@';
93*7c7d1e03SGreg Roach        $spouse = $tree->createIndividual($gedrec);
94*7c7d1e03SGreg Roach
95*7c7d1e03SGreg Roach        // Update the existing family - add marriage, etc
96*7c7d1e03SGreg Roach        if ($family->facts(['HUSB'])->first() instanceof Fact) {
97*7c7d1e03SGreg Roach            $family->createFact('1 WIFE @' . $spouse->xref() . '@', true);
98*7c7d1e03SGreg Roach        } else {
99*7c7d1e03SGreg Roach            $family->createFact('1 HUSB @' . $spouse->xref() . '@', true);
100*7c7d1e03SGreg Roach        }
101*7c7d1e03SGreg Roach        $famrec = '';
102*7c7d1e03SGreg Roach        if (preg_match_all('/([A-Z0-9_]+)/', $tree->getPreference('QUICK_REQUIRED_FAMFACTS'), $matches)) {
103*7c7d1e03SGreg Roach            foreach ($matches[1] as $match) {
104*7c7d1e03SGreg Roach                $famrec .= $this->gedcom_edit_service->addNewFact($request, $tree, $match);
105*7c7d1e03SGreg Roach            }
106*7c7d1e03SGreg Roach        }
107*7c7d1e03SGreg Roach        if ($params['SOUR_FAM'] ?? false) {
108*7c7d1e03SGreg Roach            $famrec = $this->gedcom_edit_service->handleUpdates($famrec);
109*7c7d1e03SGreg Roach        } else {
110*7c7d1e03SGreg Roach            $famrec = $this->gedcom_edit_service->updateRest($famrec);
111*7c7d1e03SGreg Roach        }
112*7c7d1e03SGreg Roach        $family->createFact(trim($famrec), true); // trim leading \n
113*7c7d1e03SGreg Roach
114*7c7d1e03SGreg Roach        if (($params['goto'] ?? '') === 'new') {
115*7c7d1e03SGreg Roach            return redirect($spouse->url());
116*7c7d1e03SGreg Roach        }
117*7c7d1e03SGreg Roach
118*7c7d1e03SGreg Roach        return redirect($family->url());
119*7c7d1e03SGreg Roach    }
120*7c7d1e03SGreg Roach}
121