xref: /webtrees/app/Http/RequestHandlers/AddNewFact.php (revision e22e42f733f4ab630c81dc26a61e9a5470ca33de)
12917771cSGreg Roach<?php
22917771cSGreg Roach
32917771cSGreg Roach/**
42917771cSGreg Roach * webtrees: online genealogy
589f7189bSGreg Roach * Copyright (C) 2021 webtrees development team
62917771cSGreg Roach * This program is free software: you can redistribute it and/or modify
72917771cSGreg Roach * it under the terms of the GNU General Public License as published by
82917771cSGreg Roach * the Free Software Foundation, either version 3 of the License, or
92917771cSGreg Roach * (at your option) any later version.
102917771cSGreg Roach * This program is distributed in the hope that it will be useful,
112917771cSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
122917771cSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
132917771cSGreg Roach * GNU General Public License for more details.
142917771cSGreg 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/>.
162917771cSGreg Roach */
172917771cSGreg Roach
182917771cSGreg Roachdeclare(strict_types=1);
192917771cSGreg Roach
202917771cSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
212917771cSGreg Roach
222917771cSGreg Roachuse Fisharebest\Webtrees\Auth;
23efd4768bSGreg Roachuse Fisharebest\Webtrees\Fact;
242917771cSGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
256b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
26*e22e42f7SGreg Roachuse Fisharebest\Webtrees\Services\GedcomEditService;
272917771cSGreg Roachuse Fisharebest\Webtrees\Tree;
282917771cSGreg Roachuse Psr\Http\Message\ResponseInterface;
292917771cSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
302917771cSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
312917771cSGreg Roach
322917771cSGreg Roachuse function assert;
33ddeb3354SGreg Roachuse function is_string;
34efd4768bSGreg Roachuse function trim;
352917771cSGreg Roach
362917771cSGreg Roach/**
372917771cSGreg Roach * Add a new fact.
382917771cSGreg Roach */
392917771cSGreg Roachclass AddNewFact implements RequestHandlerInterface
402917771cSGreg Roach{
412917771cSGreg Roach    use ViewResponseTrait;
422917771cSGreg Roach
43*e22e42f7SGreg Roach    private GedcomEditService $gedcom_edit_service;
44*e22e42f7SGreg Roach
45*e22e42f7SGreg Roach    /**
46*e22e42f7SGreg Roach     * AddNewFact constructor.
47*e22e42f7SGreg Roach     *
48*e22e42f7SGreg Roach     * @param GedcomEditService $gedcom_edit_service
49*e22e42f7SGreg Roach     */
50*e22e42f7SGreg Roach    public function __construct(GedcomEditService $gedcom_edit_service)
51*e22e42f7SGreg Roach    {
52*e22e42f7SGreg Roach        $this->gedcom_edit_service = $gedcom_edit_service;
53*e22e42f7SGreg Roach    }
54*e22e42f7SGreg Roach
552917771cSGreg Roach    /**
562917771cSGreg Roach     * @param ServerRequestInterface $request
572917771cSGreg Roach     *
582917771cSGreg Roach     * @return ResponseInterface
592917771cSGreg Roach     */
602917771cSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
612917771cSGreg Roach    {
622917771cSGreg Roach        $tree = $request->getAttribute('tree');
632917771cSGreg Roach        assert($tree instanceof Tree);
642917771cSGreg Roach
652917771cSGreg Roach        $xref = $request->getAttribute('xref');
66ddeb3354SGreg Roach        assert(is_string($xref));
67ddeb3354SGreg Roach
68efd4768bSGreg Roach        $subtag  = $request->getAttribute('fact');
69*e22e42f7SGreg Roach
70*e22e42f7SGreg Roach        $include_hidden = (bool) ($request->getQueryParams()['include_hidden'] ?? false);
71*e22e42f7SGreg Roach
726b9cb339SGreg Roach        $record  = Registry::gedcomRecordFactory()->make($xref, $tree);
73ddeb3354SGreg Roach        $record  = Auth::checkRecordAccess($record, true);
74efd4768bSGreg Roach        $element = Registry::elementFactory()->make($record->tag() . ':' . $subtag);
750f18c21bSGreg Roach        $title   = $record->fullName() . ' - ' . $element->label();
76efd4768bSGreg Roach        $fact    = new Fact(trim('1 ' . $subtag . ' ' . $element->default($tree)), $record, 'new');
772917771cSGreg Roach
78efd4768bSGreg Roach        return $this->viewResponse('edit/edit-fact', [
79efd4768bSGreg Roach            'can_edit_raw' => false,
802917771cSGreg Roach            'fact'         => $fact,
81*e22e42f7SGreg Roach            'gedcom'       => $this->gedcom_edit_service->insertMissingSubtags($fact, $include_hidden),
822917771cSGreg Roach            'title'        => $title,
832917771cSGreg Roach            'tree'         => $tree,
84efd4768bSGreg Roach            'url'          => $record->url(),
852917771cSGreg Roach        ]);
862917771cSGreg Roach    }
872917771cSGreg Roach}
88