xref: /webtrees/app/Http/RequestHandlers/AddChildToFamilyAction.php (revision e93a8df2f8d797005750082cc3766c0e80799688)
17c7d1e03SGreg Roach<?php
27c7d1e03SGreg Roach
37c7d1e03SGreg Roach/**
47c7d1e03SGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 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
1589f7189bSGreg 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;
2316904038SGreg Roachuse Fisharebest\Webtrees\Individual;
246b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
257c7d1e03SGreg Roachuse Fisharebest\Webtrees\Services\GedcomEditService;
268d9c2b68SGreg Roachuse Fisharebest\Webtrees\Validator;
277c7d1e03SGreg Roachuse Psr\Http\Message\ResponseInterface;
287c7d1e03SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
297c7d1e03SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
307c7d1e03SGreg Roach
317c7d1e03SGreg Roachuse function redirect;
327c7d1e03SGreg Roach
337c7d1e03SGreg Roach/**
347c7d1e03SGreg Roach * Add a new child to a family.
357c7d1e03SGreg Roach */
367c7d1e03SGreg Roachclass AddChildToFamilyAction implements RequestHandlerInterface
377c7d1e03SGreg Roach{
38c4943cffSGreg Roach    private GedcomEditService $gedcom_edit_service;
397c7d1e03SGreg Roach
407c7d1e03SGreg Roach    /**
417c7d1e03SGreg Roach     * @param GedcomEditService $gedcom_edit_service
427c7d1e03SGreg Roach     */
437c7d1e03SGreg Roach    public function __construct(GedcomEditService $gedcom_edit_service)
447c7d1e03SGreg Roach    {
457c7d1e03SGreg Roach        $this->gedcom_edit_service = $gedcom_edit_service;
467c7d1e03SGreg Roach    }
477c7d1e03SGreg Roach
487c7d1e03SGreg Roach    /**
497c7d1e03SGreg Roach     * @param ServerRequestInterface $request
507c7d1e03SGreg Roach     *
517c7d1e03SGreg Roach     * @return ResponseInterface
527c7d1e03SGreg Roach     */
537c7d1e03SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
547c7d1e03SGreg Roach    {
55b55cbc6bSGreg Roach        $tree   = Validator::attributes($request)->tree();
56b55cbc6bSGreg Roach        $xref   = Validator::attributes($request)->isXref()->string('xref');
576b9cb339SGreg Roach        $family = Registry::familyFactory()->make($xref, $tree);
587c7d1e03SGreg Roach        $family = Auth::checkFamilyAccess($family, true);
59a92a07c7SGreg Roach
60a92a07c7SGreg Roach        $levels = Validator::parsedBody($request)->array('ilevels');
61a92a07c7SGreg Roach        $tags   = Validator::parsedBody($request)->array('itags');
62a92a07c7SGreg Roach        $values = Validator::parsedBody($request)->array('ivalues');
63a92a07c7SGreg Roach        $gedcom = $this->gedcom_edit_service->editLinesToGedcom(Individual::RECORD_TYPE, $levels, $tags, $values);
647c7d1e03SGreg Roach
657c7d1e03SGreg Roach        // Create the new child
66a92a07c7SGreg Roach        $child  = $tree->createIndividual("0 @@ INDI\n1 FAMC @" . $xref . '@' . $gedcom);
677c7d1e03SGreg Roach
68efd4768bSGreg Roach        // Link the child to the family
69efd4768bSGreg Roach        $family->createFact('1 CHIL @' . $child->xref() . '@', false);
707c7d1e03SGreg Roach
71f507cef9SGreg Roach        $url = Validator::parsedBody($request)->isLocalUrl()->string('url', $child->url());
72551ad4afSGreg Roach
73551ad4afSGreg Roach        return redirect($url);
747c7d1e03SGreg Roach    }
757c7d1e03SGreg Roach}
76