xref: /webtrees/app/Http/RequestHandlers/AddChildToIndividualAction.php (revision 6f68916103931ce3f715eba5c6f55acf120c084e)
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\Factory;
24use Fisharebest\Webtrees\GedcomCode\GedcomCodePedi;
25use Fisharebest\Webtrees\Services\GedcomEditService;
26use Fisharebest\Webtrees\Tree;
27use Psr\Http\Message\ResponseInterface;
28use Psr\Http\Message\ServerRequestInterface;
29use Psr\Http\Server\RequestHandlerInterface;
30
31use function assert;
32use function preg_match_all;
33use function redirect;
34
35/**
36 * Add a new child to an individual, creating a one-parent family.
37 */
38class AddChildToIndividualAction implements RequestHandlerInterface
39{
40    /** @var GedcomEditService */
41    private $gedcom_edit_service;
42
43    /**
44     * AddChildToFamilyAction constructor.
45     *
46     * @param GedcomEditService $gedcom_edit_service
47     */
48    public function __construct(GedcomEditService $gedcom_edit_service)
49    {
50        $this->gedcom_edit_service = $gedcom_edit_service;
51    }
52
53    /**
54     * @param ServerRequestInterface $request
55     *
56     * @return ResponseInterface
57     */
58    public function handle(ServerRequestInterface $request): ResponseInterface
59    {
60        $tree = $request->getAttribute('tree');
61        assert($tree instanceof Tree);
62
63        $xref = $request->getQueryParams()['xref'];
64
65        $individual = Factory::individual()->make($xref, $tree);
66        $individual = Auth::checkIndividualAccess($individual, true);
67
68        $params = (array) $request->getParsedBody();
69
70        $PEDI = $params['PEDI'];
71
72        $this->gedcom_edit_service->glevels = $params['glevels'] ?? [];
73        $this->gedcom_edit_service->tag     = $params['tag'] ?? [];
74        $this->gedcom_edit_service->text    = $params['text'] ?? [];
75        $this->gedcom_edit_service->islink  = $params['islink'] ?? [];
76
77        // Create a family
78        if ($individual->sex() === 'F') {
79            $gedcom = "0 @@ FAM\n1 WIFE @" . $individual->xref() . '@';
80        } else {
81            $gedcom = "0 @@ FAM\n1 HUSB @" . $individual->xref() . '@';
82        }
83        $family = $tree->createFamily($gedcom);
84
85        // Link the parent to the family
86        $individual->createFact('1 FAMS @' . $family->xref() . '@', true);
87
88        // Create a child
89        $this->gedcom_edit_service->splitSource(); // separate SOUR record from the rest
90
91        $gedcom = '0 @@ INDI';
92        $gedcom .= $this->gedcom_edit_service->addNewName($request, $tree);
93        $gedcom .= $this->gedcom_edit_service->addNewSex($request);
94        $gedcom .= "\n" . GedcomCodePedi::createNewFamcPedi($PEDI, $family->xref());
95        if (preg_match_all('/([A-Z0-9_]+)/', $tree->getPreference('QUICK_REQUIRED_FACTS'), $matches)) {
96            foreach ($matches[1] as $match) {
97                $gedcom .= $this->gedcom_edit_service->addNewFact($request, $tree, $match);
98            }
99        }
100        if ($params['SOUR_INDI'] ?? false) {
101            $gedcom = $this->gedcom_edit_service->handleUpdates($gedcom);
102        } else {
103            $gedcom = $this->gedcom_edit_service->updateRest($gedcom);
104        }
105
106        $child = $tree->createIndividual($gedcom);
107
108        // Link the family to the child
109        $family->createFact('1 CHIL @' . $child->xref() . '@', true);
110
111        if (($params['goto'] ?? '') === 'new') {
112            return redirect($child->url());
113        }
114
115        return redirect($individual->url());
116    }
117}
118