xref: /webtrees/app/Http/RequestHandlers/AddUnlinkedAction.php (revision f507cef925e69f3dc0d47102f965899785571595)
17c7d1e03SGreg Roach<?php
27c7d1e03SGreg Roach
37c7d1e03SGreg Roach/**
47c7d1e03SGreg Roach * webtrees: online genealogy
55bfc6897SGreg Roach * Copyright (C) 2022 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\Services\GedcomEditService;
238d9c2b68SGreg Roachuse Fisharebest\Webtrees\Validator;
247c7d1e03SGreg Roachuse Psr\Http\Message\ResponseInterface;
257c7d1e03SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
267c7d1e03SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
277c7d1e03SGreg Roach
287c7d1e03SGreg Roachuse function redirect;
297c7d1e03SGreg Roach
307c7d1e03SGreg Roach/**
317c7d1e03SGreg Roach * Create a new unlinked individual.
327c7d1e03SGreg Roach */
337c7d1e03SGreg Roachclass AddUnlinkedAction implements RequestHandlerInterface
347c7d1e03SGreg Roach{
35c4943cffSGreg Roach    private GedcomEditService $gedcom_edit_service;
367c7d1e03SGreg Roach
377c7d1e03SGreg Roach    /**
387c7d1e03SGreg Roach     * AddChildToFamilyAction constructor.
397c7d1e03SGreg Roach     *
407c7d1e03SGreg Roach     * @param GedcomEditService $gedcom_edit_service
417c7d1e03SGreg Roach     */
427c7d1e03SGreg Roach    public function __construct(GedcomEditService $gedcom_edit_service)
437c7d1e03SGreg Roach    {
447c7d1e03SGreg Roach        $this->gedcom_edit_service = $gedcom_edit_service;
457c7d1e03SGreg Roach    }
467c7d1e03SGreg Roach
477c7d1e03SGreg Roach    /**
487c7d1e03SGreg Roach     * @param ServerRequestInterface $request
497c7d1e03SGreg Roach     *
507c7d1e03SGreg Roach     * @return ResponseInterface
517c7d1e03SGreg Roach     */
527c7d1e03SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
537c7d1e03SGreg Roach    {
54b55cbc6bSGreg Roach        $tree = Validator::attributes($request)->tree();
557c7d1e03SGreg Roach
567c7d1e03SGreg Roach        $params = (array) $request->getParsedBody();
577c7d1e03SGreg Roach
58efd4768bSGreg Roach        $levels = $params['ilevels'] ?? [];
59efd4768bSGreg Roach        $tags   = $params['itags'] ?? [];
60efd4768bSGreg Roach        $values = $params['ivalues'] ?? [];
617c7d1e03SGreg Roach
62efd4768bSGreg Roach        $gedcom = $this->gedcom_edit_service->editLinesToGedcom('INDI', $levels, $tags, $values);
637c7d1e03SGreg Roach
64efd4768bSGreg Roach        $individual = $tree->createIndividual("0 @@ INDI\n" . $gedcom);
657c7d1e03SGreg Roach
66*f507cef9SGreg Roach        $url = Validator::parsedBody($request)->isLocalUrl()->string('url', $individual->url());
67551ad4afSGreg Roach
68551ad4afSGreg Roach        return redirect($url);
697c7d1e03SGreg Roach    }
707c7d1e03SGreg Roach}
71