xref: /webtrees/app/Http/RequestHandlers/LinkSpouseToIndividualAction.php (revision 6b9cb339562c8aa4681c7eff2bf3bdf401cc9edd)
17c7d1e03SGreg Roach<?php
27c7d1e03SGreg Roach
37c7d1e03SGreg Roach/**
47c7d1e03SGreg Roach * webtrees: online genealogy
57c7d1e03SGreg Roach * Copyright (C) 2020 webtrees development team
67c7d1e03SGreg Roach * This program is free software: you can redistribute it and/or modify
77c7d1e03SGreg Roach * it under the terms of the GNU General Public License as published by
87c7d1e03SGreg Roach * the Free Software Foundation, either version 3 of the License, or
97c7d1e03SGreg Roach * (at your option) any later version.
107c7d1e03SGreg Roach * This program is distributed in the hope that it will be useful,
117c7d1e03SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
127c7d1e03SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
137c7d1e03SGreg Roach * GNU General Public License for more details.
147c7d1e03SGreg Roach * You should have received a copy of the GNU General Public License
157c7d1e03SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
167c7d1e03SGreg Roach */
177c7d1e03SGreg Roach
187c7d1e03SGreg Roachdeclare(strict_types=1);
197c7d1e03SGreg Roach
207c7d1e03SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
217c7d1e03SGreg Roach
227c7d1e03SGreg Roachuse Fisharebest\Webtrees\Auth;
23*6b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
247c7d1e03SGreg Roachuse Fisharebest\Webtrees\Services\GedcomEditService;
257c7d1e03SGreg Roachuse Fisharebest\Webtrees\Tree;
267c7d1e03SGreg Roachuse Psr\Http\Message\ResponseInterface;
277c7d1e03SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
287c7d1e03SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
297c7d1e03SGreg Roach
307c7d1e03SGreg Roachuse function assert;
317c7d1e03SGreg Roachuse function redirect;
327c7d1e03SGreg Roach
337c7d1e03SGreg Roach/**
347c7d1e03SGreg Roach * Link an existing individual as a new spouse.
357c7d1e03SGreg Roach */
367c7d1e03SGreg Roachclass LinkSpouseToIndividualAction implements RequestHandlerInterface
377c7d1e03SGreg Roach{
387c7d1e03SGreg Roach    /** @var GedcomEditService */
397c7d1e03SGreg Roach    private $gedcom_edit_service;
407c7d1e03SGreg Roach
417c7d1e03SGreg Roach    /**
427c7d1e03SGreg Roach     * AddChildToFamilyAction constructor.
437c7d1e03SGreg Roach     *
447c7d1e03SGreg Roach     * @param GedcomEditService $gedcom_edit_service
457c7d1e03SGreg Roach     */
467c7d1e03SGreg Roach    public function __construct(GedcomEditService $gedcom_edit_service)
477c7d1e03SGreg Roach    {
487c7d1e03SGreg Roach        $this->gedcom_edit_service = $gedcom_edit_service;
497c7d1e03SGreg Roach    }
507c7d1e03SGreg Roach
517c7d1e03SGreg Roach    /**
527c7d1e03SGreg Roach     * @param ServerRequestInterface $request
537c7d1e03SGreg Roach     *
547c7d1e03SGreg Roach     * @return ResponseInterface
557c7d1e03SGreg Roach     */
567c7d1e03SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
577c7d1e03SGreg Roach    {
587c7d1e03SGreg Roach        $tree = $request->getAttribute('tree');
597c7d1e03SGreg Roach        assert($tree instanceof Tree);
607c7d1e03SGreg Roach
617c7d1e03SGreg Roach        $xref = $request->getQueryParams()['xref'];
627c7d1e03SGreg Roach
63*6b9cb339SGreg Roach        $individual = Registry::individualFactory()->make($xref, $tree);
647c7d1e03SGreg Roach        $individual = Auth::checkIndividualAccess($individual, true);
657c7d1e03SGreg Roach
667c7d1e03SGreg Roach        $params = (array) $request->getParsedBody();
677c7d1e03SGreg Roach        $spid   = $params['spid'];
687c7d1e03SGreg Roach
69*6b9cb339SGreg Roach        $spouse = Registry::individualFactory()->make($spid, $tree);
707c7d1e03SGreg Roach        $spouse = Auth::checkIndividualAccess($spouse, true);
717c7d1e03SGreg Roach
727c7d1e03SGreg Roach        if ($individual->sex() === 'M') {
737c7d1e03SGreg Roach            $gedcom = "0 @@ FAM\n1 HUSB @" . $individual->xref() . "@\n1 WIFE @" . $spouse->xref() . '@';
747c7d1e03SGreg Roach        } else {
757c7d1e03SGreg Roach            $gedcom = "0 @@ FAM\n1 WIFE @" . $individual->xref() . "@\n1 HUSB @" . $spouse->xref() . '@';
767c7d1e03SGreg Roach        }
777c7d1e03SGreg Roach
787c7d1e03SGreg Roach        $gedcom .= $this->gedcom_edit_service->addNewFact($request, $tree, 'MARR');
797c7d1e03SGreg Roach
807c7d1e03SGreg Roach        $family = $tree->createFamily($gedcom);
817c7d1e03SGreg Roach
827c7d1e03SGreg Roach        $individual->createFact('1 FAMS @' . $family->xref() . '@', true);
837c7d1e03SGreg Roach        $spouse->createFact('1 FAMS @' . $family->xref() . '@', true);
847c7d1e03SGreg Roach
857c7d1e03SGreg Roach        return redirect($family->url());
867c7d1e03SGreg Roach    }
877c7d1e03SGreg Roach}
88