xref: /webtrees/app/Http/RequestHandlers/AddSpouseToIndividualAction.php (revision e93a8df2f8d797005750082cc3766c0e80799688)
17c7d1e03SGreg Roach<?php
27c7d1e03SGreg Roach
37c7d1e03SGreg Roach/**
47c7d1e03SGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 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
1589f7189bSGreg Roach * along with this program. If not, see <https://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;
2316904038SGreg Roachuse Fisharebest\Webtrees\Family;
2416904038SGreg Roachuse Fisharebest\Webtrees\Individual;
256b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
267c7d1e03SGreg Roachuse Fisharebest\Webtrees\Services\GedcomEditService;
278d9c2b68SGreg Roachuse Fisharebest\Webtrees\Validator;
287c7d1e03SGreg Roachuse Psr\Http\Message\ResponseInterface;
297c7d1e03SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
307c7d1e03SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
317c7d1e03SGreg Roach
327c7d1e03SGreg Roachuse function redirect;
337c7d1e03SGreg Roach
347c7d1e03SGreg Roach/**
357c7d1e03SGreg Roach * Add a new spouse to an individual, creating a new family.
367c7d1e03SGreg Roach */
377c7d1e03SGreg Roachclass AddSpouseToIndividualAction implements RequestHandlerInterface
387c7d1e03SGreg Roach{
39c4943cffSGreg Roach    private GedcomEditService $gedcom_edit_service;
407c7d1e03SGreg Roach
417c7d1e03SGreg Roach    /**
427c7d1e03SGreg Roach     * @param GedcomEditService $gedcom_edit_service
437c7d1e03SGreg Roach     */
447c7d1e03SGreg Roach    public function __construct(GedcomEditService $gedcom_edit_service)
457c7d1e03SGreg Roach    {
467c7d1e03SGreg Roach        $this->gedcom_edit_service = $gedcom_edit_service;
477c7d1e03SGreg Roach    }
487c7d1e03SGreg Roach
497c7d1e03SGreg Roach    /**
507c7d1e03SGreg Roach     * @param ServerRequestInterface $request
517c7d1e03SGreg Roach     *
527c7d1e03SGreg Roach     * @return ResponseInterface
537c7d1e03SGreg Roach     */
547c7d1e03SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
557c7d1e03SGreg Roach    {
56b55cbc6bSGreg Roach        $tree       = Validator::attributes($request)->tree();
57b55cbc6bSGreg Roach        $xref       = Validator::attributes($request)->isXref()->string('xref');
586b9cb339SGreg Roach        $individual = Registry::individualFactory()->make($xref, $tree);
597c7d1e03SGreg Roach        $individual = Auth::checkIndividualAccess($individual, true);
607c7d1e03SGreg Roach
610c71b2e5SGreg Roach        // Create the new spouse
62a92a07c7SGreg Roach        $levels = Validator::parsedBody($request)->array('ilevels');
63a92a07c7SGreg Roach        $tags   = Validator::parsedBody($request)->array('itags');
64a92a07c7SGreg Roach        $values = Validator::parsedBody($request)->array('ivalues');
6516904038SGreg Roach        $gedcom = $this->gedcom_edit_service->editLinesToGedcom(Individual::RECORD_TYPE, $levels, $tags, $values);
66b389d323SGreg Roach        $spouse = $tree->createIndividual('0 @@ INDI' . $gedcom);
67efd4768bSGreg Roach
680c71b2e5SGreg Roach        // Create the new family
69a92a07c7SGreg Roach        $levels = Validator::parsedBody($request)->array('flevels');
70a92a07c7SGreg Roach        $tags   = Validator::parsedBody($request)->array('ftags');
71a92a07c7SGreg Roach        $values = Validator::parsedBody($request)->array('fvalues');
7216904038SGreg Roach        $gedcom = $this->gedcom_edit_service->editLinesToGedcom(Family::RECORD_TYPE, $levels, $tags, $values);
73efd4768bSGreg Roach        $i_link = "\n1 " . ($individual->sex() === 'F' ? 'WIFE' : 'HUSB') . ' @' . $individual->xref() . '@';
74efd4768bSGreg Roach        $s_link = "\n1 " . ($individual->sex() !== 'F' ? 'WIFE' : 'HUSB') . ' @' . $spouse->xref() . '@';
75b389d323SGreg Roach        $family = $tree->createFamily('0 @@ FAM' . $gedcom . $i_link . $s_link);
767c7d1e03SGreg Roach
77efd4768bSGreg Roach        // Link the individual to the family
78efd4768bSGreg Roach        $individual->createFact('1 FAMS @' . $family->xref() . '@', false);
797c7d1e03SGreg Roach
80efd4768bSGreg Roach        // Link the spouse to the family
81efd4768bSGreg Roach        $spouse->createFact('1 FAMS @' . $family->xref() . '@', false);
82efd4768bSGreg Roach
83f507cef9SGreg Roach        $url = Validator::parsedBody($request)->isLocalUrl()->string('url', $spouse->url());
84551ad4afSGreg Roach
85551ad4afSGreg Roach        return redirect($url);
867c7d1e03SGreg Roach    }
877c7d1e03SGreg Roach}
88