xref: /webtrees/app/Http/RequestHandlers/LinkChildToFamilyAction.php (revision 455a30fe529581f1d53045f9c38a0674472d498f)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22use Fisharebest\Webtrees\Auth;
23use Fisharebest\Webtrees\GedcomCode\GedcomCodePedi;
24use Fisharebest\Webtrees\Registry;
25use Fisharebest\Webtrees\Tree;
26use Psr\Http\Message\ResponseInterface;
27use Psr\Http\Message\ServerRequestInterface;
28use Psr\Http\Server\RequestHandlerInterface;
29
30use function assert;
31use function redirect;
32
33/**
34 * Link an existing individual as child in an existing family.
35 */
36class LinkChildToFamilyAction implements RequestHandlerInterface
37{
38    /**
39     * @param ServerRequestInterface $request
40     *
41     * @return ResponseInterface
42     */
43    public function handle(ServerRequestInterface $request): ResponseInterface
44    {
45        $tree = $request->getAttribute('tree');
46        assert($tree instanceof Tree);
47
48        $xref = $request->getQueryParams()['xref'];
49
50        $individual = Registry::individualFactory()->make($xref, $tree);
51        $individual = Auth::checkIndividualAccess($individual, true);
52
53        $params = (array) $request->getParsedBody();
54
55        $famid = $params['famid'];
56
57        $family = Registry::familyFactory()->make($famid, $tree);
58        $family = Auth::checkFamilyAccess($family, true);
59
60        $PEDI = $params['PEDI'];
61
62        // Replace any existing child->family link (we may be changing the PEDI);
63        $fact_id = '';
64        foreach ($individual->facts(['FAMC']) as $fact) {
65            if ($family === $fact->target()) {
66                $fact_id = $fact->id();
67                break;
68            }
69        }
70
71        $gedcom = GedcomCodePedi::createNewFamcPedi($PEDI, $famid);
72        $individual->updateFact($fact_id, $gedcom, true);
73
74        // Only set the family->child link if it does not already exist
75        $chil_link_exists = false;
76        foreach ($family->facts(['CHIL']) as $fact) {
77            if ($individual === $fact->target()) {
78                $chil_link_exists = true;
79                break;
80            }
81        }
82
83        if (!$chil_link_exists) {
84            $family->createFact('1 CHIL @' . $individual->xref() . '@', true);
85        }
86
87        return redirect($individual->url());
88    }
89}
90