xref: /webtrees/app/Http/RequestHandlers/AddChildToIndividualAction.php (revision 89f7189b61a494347591c99bdb92afb7d8b66e1b)
17c7d1e03SGreg Roach<?php
27c7d1e03SGreg Roach
37c7d1e03SGreg Roach/**
47c7d1e03SGreg Roach * webtrees: online genealogy
5*89f7189bSGreg 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
15*89f7189bSGreg 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;
237c7d1e03SGreg Roachuse Fisharebest\Webtrees\GedcomCode\GedcomCodePedi;
246b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
257c7d1e03SGreg Roachuse Fisharebest\Webtrees\Services\GedcomEditService;
267c7d1e03SGreg Roachuse Fisharebest\Webtrees\Tree;
277c7d1e03SGreg Roachuse Psr\Http\Message\ResponseInterface;
287c7d1e03SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
297c7d1e03SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
307c7d1e03SGreg Roach
317c7d1e03SGreg Roachuse function assert;
327c7d1e03SGreg Roachuse function preg_match_all;
337c7d1e03SGreg Roachuse function redirect;
347c7d1e03SGreg Roach
357c7d1e03SGreg Roach/**
367c7d1e03SGreg Roach * Add a new child to an individual, creating a one-parent family.
377c7d1e03SGreg Roach */
387c7d1e03SGreg Roachclass AddChildToIndividualAction implements RequestHandlerInterface
397c7d1e03SGreg Roach{
407c7d1e03SGreg Roach    /** @var GedcomEditService */
417c7d1e03SGreg Roach    private $gedcom_edit_service;
427c7d1e03SGreg Roach
437c7d1e03SGreg Roach    /**
447c7d1e03SGreg Roach     * AddChildToFamilyAction constructor.
457c7d1e03SGreg Roach     *
467c7d1e03SGreg Roach     * @param GedcomEditService $gedcom_edit_service
477c7d1e03SGreg Roach     */
487c7d1e03SGreg Roach    public function __construct(GedcomEditService $gedcom_edit_service)
497c7d1e03SGreg Roach    {
507c7d1e03SGreg Roach        $this->gedcom_edit_service = $gedcom_edit_service;
517c7d1e03SGreg Roach    }
527c7d1e03SGreg Roach
537c7d1e03SGreg Roach    /**
547c7d1e03SGreg Roach     * @param ServerRequestInterface $request
557c7d1e03SGreg Roach     *
567c7d1e03SGreg Roach     * @return ResponseInterface
577c7d1e03SGreg Roach     */
587c7d1e03SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
597c7d1e03SGreg Roach    {
607c7d1e03SGreg Roach        $tree = $request->getAttribute('tree');
617c7d1e03SGreg Roach        assert($tree instanceof Tree);
627c7d1e03SGreg Roach
637c7d1e03SGreg Roach        $xref = $request->getQueryParams()['xref'];
647c7d1e03SGreg Roach
656b9cb339SGreg Roach        $individual = Registry::individualFactory()->make($xref, $tree);
667c7d1e03SGreg Roach        $individual = Auth::checkIndividualAccess($individual, true);
677c7d1e03SGreg Roach
687c7d1e03SGreg Roach        $params = (array) $request->getParsedBody();
697c7d1e03SGreg Roach
707c7d1e03SGreg Roach        $PEDI = $params['PEDI'];
717c7d1e03SGreg Roach
727c7d1e03SGreg Roach        $this->gedcom_edit_service->glevels = $params['glevels'] ?? [];
737c7d1e03SGreg Roach        $this->gedcom_edit_service->tag     = $params['tag'] ?? [];
747c7d1e03SGreg Roach        $this->gedcom_edit_service->text    = $params['text'] ?? [];
757c7d1e03SGreg Roach        $this->gedcom_edit_service->islink  = $params['islink'] ?? [];
767c7d1e03SGreg Roach
777c7d1e03SGreg Roach        // Create a family
787c7d1e03SGreg Roach        if ($individual->sex() === 'F') {
797c7d1e03SGreg Roach            $gedcom = "0 @@ FAM\n1 WIFE @" . $individual->xref() . '@';
807c7d1e03SGreg Roach        } else {
817c7d1e03SGreg Roach            $gedcom = "0 @@ FAM\n1 HUSB @" . $individual->xref() . '@';
827c7d1e03SGreg Roach        }
837c7d1e03SGreg Roach        $family = $tree->createFamily($gedcom);
847c7d1e03SGreg Roach
857c7d1e03SGreg Roach        // Link the parent to the family
867c7d1e03SGreg Roach        $individual->createFact('1 FAMS @' . $family->xref() . '@', true);
877c7d1e03SGreg Roach
887c7d1e03SGreg Roach        // Create a child
897c7d1e03SGreg Roach        $this->gedcom_edit_service->splitSource(); // separate SOUR record from the rest
907c7d1e03SGreg Roach
917c7d1e03SGreg Roach        $gedcom = '0 @@ INDI';
927c7d1e03SGreg Roach        $gedcom .= $this->gedcom_edit_service->addNewName($request, $tree);
937c7d1e03SGreg Roach        $gedcom .= $this->gedcom_edit_service->addNewSex($request);
947c7d1e03SGreg Roach        $gedcom .= "\n" . GedcomCodePedi::createNewFamcPedi($PEDI, $family->xref());
957c7d1e03SGreg Roach        if (preg_match_all('/([A-Z0-9_]+)/', $tree->getPreference('QUICK_REQUIRED_FACTS'), $matches)) {
967c7d1e03SGreg Roach            foreach ($matches[1] as $match) {
977c7d1e03SGreg Roach                $gedcom .= $this->gedcom_edit_service->addNewFact($request, $tree, $match);
987c7d1e03SGreg Roach            }
997c7d1e03SGreg Roach        }
1007c7d1e03SGreg Roach        if ($params['SOUR_INDI'] ?? false) {
1017c7d1e03SGreg Roach            $gedcom = $this->gedcom_edit_service->handleUpdates($gedcom);
1027c7d1e03SGreg Roach        } else {
1037c7d1e03SGreg Roach            $gedcom = $this->gedcom_edit_service->updateRest($gedcom);
1047c7d1e03SGreg Roach        }
1057c7d1e03SGreg Roach
1067c7d1e03SGreg Roach        $child = $tree->createIndividual($gedcom);
1077c7d1e03SGreg Roach
1087c7d1e03SGreg Roach        // Link the family to the child
1097c7d1e03SGreg Roach        $family->createFact('1 CHIL @' . $child->xref() . '@', true);
1107c7d1e03SGreg Roach
1117c7d1e03SGreg Roach        if (($params['goto'] ?? '') === 'new') {
1127c7d1e03SGreg Roach            return redirect($child->url());
1137c7d1e03SGreg Roach        }
1147c7d1e03SGreg Roach
1157c7d1e03SGreg Roach        return redirect($individual->url());
1167c7d1e03SGreg Roach    }
1177c7d1e03SGreg Roach}
118