xref: /webtrees/app/Http/RequestHandlers/AddChildToFamilyAction.php (revision 7c7d1e03066c50e172e4a6239e322c1e0cd62040)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2020 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 <http://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22use Fisharebest\Webtrees\Auth;
23use Fisharebest\Webtrees\Date;
24use Fisharebest\Webtrees\Factory;
25use Fisharebest\Webtrees\GedcomCode\GedcomCodePedi;
26use Fisharebest\Webtrees\Individual;
27use Fisharebest\Webtrees\Services\GedcomEditService;
28use Fisharebest\Webtrees\Tree;
29use Psr\Http\Message\ResponseInterface;
30use Psr\Http\Message\ServerRequestInterface;
31use Psr\Http\Server\RequestHandlerInterface;
32
33use function assert;
34use function preg_match_all;
35use function redirect;
36
37/**
38 * Add a new child to a family.
39 */
40class AddChildToFamilyAction implements RequestHandlerInterface
41{
42    /** @var GedcomEditService */
43    private $gedcom_edit_service;
44
45    /**
46     * AddChildToFamilyAction constructor.
47     *
48     * @param GedcomEditService $gedcom_edit_service
49     */
50    public function __construct(GedcomEditService $gedcom_edit_service)
51    {
52        $this->gedcom_edit_service = $gedcom_edit_service;
53    }
54
55    /**
56     * @param ServerRequestInterface $request
57     *
58     * @return ResponseInterface
59     */
60    public function handle(ServerRequestInterface $request): ResponseInterface
61    {
62        $tree = $request->getAttribute('tree');
63        assert($tree instanceof Tree);
64
65        $xref = $request->getQueryParams()['xref'];
66
67        $family = Factory::family()->make($xref, $tree);
68        $family = Auth::checkFamilyAccess($family, true);
69
70        $params = (array) $request->getParsedBody();
71
72        $PEDI      = $params['PEDI'];
73        $keep_chan = (bool) ($params['keep_chan'] ?? false);
74
75        $this->gedcom_edit_service->glevels = $params['glevels'] ?? [];
76        $this->gedcom_edit_service->tag     = $params['tag'] ?? [];
77        $this->gedcom_edit_service->text    = $params['text'] ?? [];
78        $this->gedcom_edit_service->islink  = $params['islink'] ?? [];
79
80        $this->gedcom_edit_service->splitSource();
81        $gedrec = '0 @@ INDI';
82        $gedrec .= $this->gedcom_edit_service->addNewName($request, $tree);
83        $gedrec .= $this->gedcom_edit_service->addNewSex($request);
84        if (preg_match_all('/([A-Z0-9_]+)/', $tree->getPreference('QUICK_REQUIRED_FACTS'), $matches)) {
85            foreach ($matches[1] as $match) {
86                $gedrec .= $this->gedcom_edit_service->addNewFact($request, $tree, $match);
87            }
88        }
89        $gedrec .= "\n" . GedcomCodePedi::createNewFamcPedi($PEDI, $xref);
90        if ($params['SOUR_INDI'] ?? false) {
91            $gedrec = $this->gedcom_edit_service->handleUpdates($gedrec);
92        } else {
93            $gedrec = $this->gedcom_edit_service->updateRest($gedrec);
94        }
95
96        // Create the new child
97        $new_child = $tree->createIndividual($gedrec);
98
99        // Insert new child at the right place
100        $done = false;
101        foreach ($family->facts(['CHIL']) as $fact) {
102            $old_child = $fact->target();
103            if ($old_child instanceof Individual && Date::compare($new_child->getEstimatedBirthDate(), $old_child->getEstimatedBirthDate()) < 0) {
104                // Insert before this child
105                $family->updateFact($fact->id(), '1 CHIL @' . $new_child->xref() . "@\n" . $fact->gedcom(), !$keep_chan);
106                $done = true;
107                break;
108            }
109        }
110        if (!$done) {
111            // Append child at end
112            $family->createFact('1 CHIL @' . $new_child->xref() . '@', !$keep_chan);
113        }
114
115        if (($params['goto'] ?? '') === 'new') {
116            return redirect($new_child->url());
117        }
118
119        return redirect($family->url());
120    }
121}
122