xref: /webtrees/app/Http/RequestHandlers/AddChildToFamilyAction.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;
237c7d1e03SGreg Roachuse Fisharebest\Webtrees\Date;
247c7d1e03SGreg Roachuse Fisharebest\Webtrees\GedcomCode\GedcomCodePedi;
257c7d1e03SGreg Roachuse Fisharebest\Webtrees\Individual;
26*6b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
277c7d1e03SGreg Roachuse Fisharebest\Webtrees\Services\GedcomEditService;
287c7d1e03SGreg Roachuse Fisharebest\Webtrees\Tree;
297c7d1e03SGreg Roachuse Psr\Http\Message\ResponseInterface;
307c7d1e03SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
317c7d1e03SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
327c7d1e03SGreg Roach
337c7d1e03SGreg Roachuse function assert;
347c7d1e03SGreg Roachuse function preg_match_all;
357c7d1e03SGreg Roachuse function redirect;
367c7d1e03SGreg Roach
377c7d1e03SGreg Roach/**
387c7d1e03SGreg Roach * Add a new child to a family.
397c7d1e03SGreg Roach */
407c7d1e03SGreg Roachclass AddChildToFamilyAction implements RequestHandlerInterface
417c7d1e03SGreg Roach{
427c7d1e03SGreg Roach    /** @var GedcomEditService */
437c7d1e03SGreg Roach    private $gedcom_edit_service;
447c7d1e03SGreg Roach
457c7d1e03SGreg Roach    /**
467c7d1e03SGreg Roach     * AddChildToFamilyAction constructor.
477c7d1e03SGreg Roach     *
487c7d1e03SGreg Roach     * @param GedcomEditService $gedcom_edit_service
497c7d1e03SGreg Roach     */
507c7d1e03SGreg Roach    public function __construct(GedcomEditService $gedcom_edit_service)
517c7d1e03SGreg Roach    {
527c7d1e03SGreg Roach        $this->gedcom_edit_service = $gedcom_edit_service;
537c7d1e03SGreg Roach    }
547c7d1e03SGreg Roach
557c7d1e03SGreg Roach    /**
567c7d1e03SGreg Roach     * @param ServerRequestInterface $request
577c7d1e03SGreg Roach     *
587c7d1e03SGreg Roach     * @return ResponseInterface
597c7d1e03SGreg Roach     */
607c7d1e03SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
617c7d1e03SGreg Roach    {
627c7d1e03SGreg Roach        $tree = $request->getAttribute('tree');
637c7d1e03SGreg Roach        assert($tree instanceof Tree);
647c7d1e03SGreg Roach
657c7d1e03SGreg Roach        $xref = $request->getQueryParams()['xref'];
667c7d1e03SGreg Roach
67*6b9cb339SGreg Roach        $family = Registry::familyFactory()->make($xref, $tree);
687c7d1e03SGreg Roach        $family = Auth::checkFamilyAccess($family, true);
697c7d1e03SGreg Roach
707c7d1e03SGreg Roach        $params = (array) $request->getParsedBody();
717c7d1e03SGreg Roach
727c7d1e03SGreg Roach        $PEDI      = $params['PEDI'];
737c7d1e03SGreg Roach        $keep_chan = (bool) ($params['keep_chan'] ?? false);
747c7d1e03SGreg Roach
757c7d1e03SGreg Roach        $this->gedcom_edit_service->glevels = $params['glevels'] ?? [];
767c7d1e03SGreg Roach        $this->gedcom_edit_service->tag     = $params['tag'] ?? [];
777c7d1e03SGreg Roach        $this->gedcom_edit_service->text    = $params['text'] ?? [];
787c7d1e03SGreg Roach        $this->gedcom_edit_service->islink  = $params['islink'] ?? [];
797c7d1e03SGreg Roach
807c7d1e03SGreg Roach        $this->gedcom_edit_service->splitSource();
817c7d1e03SGreg Roach        $gedrec = '0 @@ INDI';
827c7d1e03SGreg Roach        $gedrec .= $this->gedcom_edit_service->addNewName($request, $tree);
837c7d1e03SGreg Roach        $gedrec .= $this->gedcom_edit_service->addNewSex($request);
847c7d1e03SGreg Roach        if (preg_match_all('/([A-Z0-9_]+)/', $tree->getPreference('QUICK_REQUIRED_FACTS'), $matches)) {
857c7d1e03SGreg Roach            foreach ($matches[1] as $match) {
867c7d1e03SGreg Roach                $gedrec .= $this->gedcom_edit_service->addNewFact($request, $tree, $match);
877c7d1e03SGreg Roach            }
887c7d1e03SGreg Roach        }
897c7d1e03SGreg Roach        $gedrec .= "\n" . GedcomCodePedi::createNewFamcPedi($PEDI, $xref);
907c7d1e03SGreg Roach        if ($params['SOUR_INDI'] ?? false) {
917c7d1e03SGreg Roach            $gedrec = $this->gedcom_edit_service->handleUpdates($gedrec);
927c7d1e03SGreg Roach        } else {
937c7d1e03SGreg Roach            $gedrec = $this->gedcom_edit_service->updateRest($gedrec);
947c7d1e03SGreg Roach        }
957c7d1e03SGreg Roach
967c7d1e03SGreg Roach        // Create the new child
977c7d1e03SGreg Roach        $new_child = $tree->createIndividual($gedrec);
987c7d1e03SGreg Roach
997c7d1e03SGreg Roach        // Insert new child at the right place
1007c7d1e03SGreg Roach        $done = false;
1017c7d1e03SGreg Roach        foreach ($family->facts(['CHIL']) as $fact) {
1027c7d1e03SGreg Roach            $old_child = $fact->target();
1037c7d1e03SGreg Roach            if ($old_child instanceof Individual && Date::compare($new_child->getEstimatedBirthDate(), $old_child->getEstimatedBirthDate()) < 0) {
1047c7d1e03SGreg Roach                // Insert before this child
1057c7d1e03SGreg Roach                $family->updateFact($fact->id(), '1 CHIL @' . $new_child->xref() . "@\n" . $fact->gedcom(), !$keep_chan);
1067c7d1e03SGreg Roach                $done = true;
1077c7d1e03SGreg Roach                break;
1087c7d1e03SGreg Roach            }
1097c7d1e03SGreg Roach        }
1107c7d1e03SGreg Roach        if (!$done) {
1117c7d1e03SGreg Roach            // Append child at end
1127c7d1e03SGreg Roach            $family->createFact('1 CHIL @' . $new_child->xref() . '@', !$keep_chan);
1137c7d1e03SGreg Roach        }
1147c7d1e03SGreg Roach
1157c7d1e03SGreg Roach        if (($params['goto'] ?? '') === 'new') {
1167c7d1e03SGreg Roach            return redirect($new_child->url());
1177c7d1e03SGreg Roach        }
1187c7d1e03SGreg Roach
1197c7d1e03SGreg Roach        return redirect($family->url());
1207c7d1e03SGreg Roach    }
1217c7d1e03SGreg Roach}
122