xref: /webtrees/app/Http/RequestHandlers/AddSpouseToIndividualAction.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\Factory;
24*7c7d1e03SGreg Roachuse Fisharebest\Webtrees\Services\GedcomEditService;
25*7c7d1e03SGreg Roachuse Fisharebest\Webtrees\Tree;
26*7c7d1e03SGreg Roachuse Psr\Http\Message\ResponseInterface;
27*7c7d1e03SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
28*7c7d1e03SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
29*7c7d1e03SGreg Roach
30*7c7d1e03SGreg Roachuse function assert;
31*7c7d1e03SGreg Roachuse function preg_match_all;
32*7c7d1e03SGreg Roachuse function redirect;
33*7c7d1e03SGreg Roach
34*7c7d1e03SGreg Roach/**
35*7c7d1e03SGreg Roach * Add a new spouse to an individual, creating a new family.
36*7c7d1e03SGreg Roach */
37*7c7d1e03SGreg Roachclass AddSpouseToIndividualAction implements RequestHandlerInterface
38*7c7d1e03SGreg Roach{
39*7c7d1e03SGreg Roach    /** @var GedcomEditService */
40*7c7d1e03SGreg Roach    private $gedcom_edit_service;
41*7c7d1e03SGreg Roach
42*7c7d1e03SGreg Roach    /**
43*7c7d1e03SGreg Roach     * AddChildToFamilyAction constructor.
44*7c7d1e03SGreg Roach     *
45*7c7d1e03SGreg Roach     * @param GedcomEditService $gedcom_edit_service
46*7c7d1e03SGreg Roach     */
47*7c7d1e03SGreg Roach    public function __construct(GedcomEditService $gedcom_edit_service)
48*7c7d1e03SGreg Roach    {
49*7c7d1e03SGreg Roach        $this->gedcom_edit_service = $gedcom_edit_service;
50*7c7d1e03SGreg Roach    }
51*7c7d1e03SGreg Roach
52*7c7d1e03SGreg Roach    /**
53*7c7d1e03SGreg Roach     * @param ServerRequestInterface $request
54*7c7d1e03SGreg Roach     *
55*7c7d1e03SGreg Roach     * @return ResponseInterface
56*7c7d1e03SGreg Roach     */
57*7c7d1e03SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
58*7c7d1e03SGreg Roach    {
59*7c7d1e03SGreg Roach        $tree = $request->getAttribute('tree');
60*7c7d1e03SGreg Roach        assert($tree instanceof Tree);
61*7c7d1e03SGreg Roach
62*7c7d1e03SGreg Roach        $xref = $request->getQueryParams()['xref'];
63*7c7d1e03SGreg Roach
64*7c7d1e03SGreg Roach        $individual = Factory::individual()->make($xref, $tree);
65*7c7d1e03SGreg Roach        $individual = Auth::checkIndividualAccess($individual, true);
66*7c7d1e03SGreg Roach
67*7c7d1e03SGreg Roach        $params = (array) $request->getParsedBody();
68*7c7d1e03SGreg Roach
69*7c7d1e03SGreg Roach        $sex = $params['SEX'];
70*7c7d1e03SGreg Roach
71*7c7d1e03SGreg Roach        $this->gedcom_edit_service->glevels = $params['glevels'] ?? [];
72*7c7d1e03SGreg Roach        $this->gedcom_edit_service->tag     = $params['tag'] ?? [];
73*7c7d1e03SGreg Roach        $this->gedcom_edit_service->text    = $params['text'] ?? [];
74*7c7d1e03SGreg Roach        $this->gedcom_edit_service->islink  = $params['islink'] ?? [];
75*7c7d1e03SGreg Roach
76*7c7d1e03SGreg Roach        $this->gedcom_edit_service->splitSource();
77*7c7d1e03SGreg Roach        $indi_gedcom = '0 @@ INDI';
78*7c7d1e03SGreg Roach        $indi_gedcom .= $this->gedcom_edit_service->addNewName($request, $tree);
79*7c7d1e03SGreg Roach        $indi_gedcom .= $this->gedcom_edit_service->addNewSex($request);
80*7c7d1e03SGreg Roach        if (preg_match_all('/([A-Z0-9_]+)/', $tree->getPreference('QUICK_REQUIRED_FACTS'), $matches)) {
81*7c7d1e03SGreg Roach            foreach ($matches[1] as $match) {
82*7c7d1e03SGreg Roach                $indi_gedcom .= $this->gedcom_edit_service->addNewFact($request, $tree, $match);
83*7c7d1e03SGreg Roach            }
84*7c7d1e03SGreg Roach        }
85*7c7d1e03SGreg Roach        if ($params['SOUR_INDI'] ?? false) {
86*7c7d1e03SGreg Roach            $indi_gedcom = $this->gedcom_edit_service->handleUpdates($indi_gedcom);
87*7c7d1e03SGreg Roach        } else {
88*7c7d1e03SGreg Roach            $indi_gedcom = $this->gedcom_edit_service->updateRest($indi_gedcom);
89*7c7d1e03SGreg Roach        }
90*7c7d1e03SGreg Roach
91*7c7d1e03SGreg Roach        $fam_gedcom = '';
92*7c7d1e03SGreg Roach        if (preg_match_all('/([A-Z0-9_]+)/', $tree->getPreference('QUICK_REQUIRED_FAMFACTS'), $matches)) {
93*7c7d1e03SGreg Roach            foreach ($matches[1] as $match) {
94*7c7d1e03SGreg Roach                $fam_gedcom .= $this->gedcom_edit_service->addNewFact($request, $tree, $match);
95*7c7d1e03SGreg Roach            }
96*7c7d1e03SGreg Roach        }
97*7c7d1e03SGreg Roach        if ($params['SOUR_FAM'] ?? false) {
98*7c7d1e03SGreg Roach            $fam_gedcom = $this->gedcom_edit_service->handleUpdates($fam_gedcom);
99*7c7d1e03SGreg Roach        } else {
100*7c7d1e03SGreg Roach            $fam_gedcom = $this->gedcom_edit_service->updateRest($fam_gedcom);
101*7c7d1e03SGreg Roach        }
102*7c7d1e03SGreg Roach
103*7c7d1e03SGreg Roach        // Create the new spouse
104*7c7d1e03SGreg Roach        $spouse = $tree->createIndividual($indi_gedcom);
105*7c7d1e03SGreg Roach        // Create a new family
106*7c7d1e03SGreg Roach        if ($sex === 'F') {
107*7c7d1e03SGreg Roach            $family = $tree->createFamily("0 @@ FAM\n1 WIFE @" . $spouse->xref() . "@\n1 HUSB @" . $individual->xref() . '@' . $fam_gedcom);
108*7c7d1e03SGreg Roach        } else {
109*7c7d1e03SGreg Roach            $family = $tree->createFamily("0 @@ FAM\n1 HUSB @" . $spouse->xref() . "@\n1 WIFE @" . $individual->xref() . '@' . $fam_gedcom);
110*7c7d1e03SGreg Roach        }
111*7c7d1e03SGreg Roach        // Link the spouses to the family
112*7c7d1e03SGreg Roach        $spouse->createFact('1 FAMS @' . $family->xref() . '@', true);
113*7c7d1e03SGreg Roach        $individual->createFact('1 FAMS @' . $family->xref() . '@', true);
114*7c7d1e03SGreg Roach
115*7c7d1e03SGreg Roach        if (($params['goto'] ?? '') === 'new') {
116*7c7d1e03SGreg Roach            return redirect($spouse->url());
117*7c7d1e03SGreg Roach        }
118*7c7d1e03SGreg Roach
119*7c7d1e03SGreg Roach        return redirect($individual->url());
120*7c7d1e03SGreg Roach    }
121*7c7d1e03SGreg Roach}
122