xref: /webtrees/app/Http/RequestHandlers/AddSpouseToFamilyAction.php (revision 551ad4afbcef2a72a6cf6461f1747762180b12c5)
17c7d1e03SGreg Roach<?php
27c7d1e03SGreg Roach
37c7d1e03SGreg Roach/**
47c7d1e03SGreg Roach * webtrees: online genealogy
589f7189bSGreg Roach * Copyright (C) 2021 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;
236b9cb339SGreg 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;
31efd4768bSGreg Roachuse function is_string;
327c7d1e03SGreg Roachuse function redirect;
337c7d1e03SGreg Roach
347c7d1e03SGreg Roach/**
357c7d1e03SGreg Roach * Add a new spouse to a family.
367c7d1e03SGreg Roach */
377c7d1e03SGreg Roachclass AddSpouseToFamilyAction implements RequestHandlerInterface
387c7d1e03SGreg Roach{
39c4943cffSGreg Roach    private GedcomEditService $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
61efd4768bSGreg Roach        $xref = $request->getAttribute('xref');
62efd4768bSGreg Roach        assert(is_string($xref));
637c7d1e03SGreg Roach
647c7d1e03SGreg Roach        $params = (array) $request->getParsedBody();
657c7d1e03SGreg Roach
66efd4768bSGreg Roach        $family = Registry::familyFactory()->make($xref, $tree);
67efd4768bSGreg Roach        $family = Auth::checkFamilyAccess($family, true);
68efd4768bSGreg Roach
69efd4768bSGreg Roach        $levels = $params['ilevels'] ?? [];
70efd4768bSGreg Roach        $tags   = $params['itags'] ?? [];
71efd4768bSGreg Roach        $values = $params['ivalues'] ?? [];
727c7d1e03SGreg Roach
737c7d1e03SGreg Roach        // Create the new spouse
74efd4768bSGreg Roach        $gedcom = "0 @@ INDI\n1 FAMS @" . $family->xref() . "@\n" . $this->gedcom_edit_service->editLinesToGedcom('INDI', $levels, $tags, $values);
75efd4768bSGreg Roach        $spouse = $tree->createIndividual($gedcom);
767c7d1e03SGreg Roach
77efd4768bSGreg Roach        // Link the spouse to the family
78efd4768bSGreg Roach        $husb = $family->facts(['HUSB'], false, null, true)->first();
79efd4768bSGreg Roach        $wife = $family->facts(['WIFE'], false, null, true)->first();
807c7d1e03SGreg Roach
81efd4768bSGreg Roach        if ($husb === null && $spouse->sex() === 'M') {
82efd4768bSGreg Roach            $link = 'HUSB';
83efd4768bSGreg Roach        } elseif ($wife === null && $spouse->sex() === 'F') {
84efd4768bSGreg Roach            $link = 'WIFE';
85efd4768bSGreg Roach        } elseif ($husb === null) {
86efd4768bSGreg Roach            $link = 'HUSB';
87efd4768bSGreg Roach        } elseif ($wife === null) {
88efd4768bSGreg Roach            $link = 'WIFE';
897c7d1e03SGreg Roach        } else {
90efd4768bSGreg Roach            // Family already has husband and wife
917c7d1e03SGreg Roach            return redirect($family->url());
927c7d1e03SGreg Roach        }
93efd4768bSGreg Roach
94efd4768bSGreg Roach        // Link the spouse to the family
95efd4768bSGreg Roach        $family->createFact('1 ' . $link . ' @' . $spouse->xref() . '@', false);
96efd4768bSGreg Roach
97*551ad4afSGreg Roach        $base_url = $request->getAttribute('base_url');
98*551ad4afSGreg Roach        $url      = str_starts_with($params['url'], $base_url) ? $params['url'] : $spouse->url();
99*551ad4afSGreg Roach
100*551ad4afSGreg Roach        return redirect($url);
101efd4768bSGreg Roach    }
1027c7d1e03SGreg Roach}
103