xref: /webtrees/app/Http/RequestHandlers/AddNewFact.php (revision 0f18c21bf8d5a588c6c7302e76b5b09686bc43f9)
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;
232917771cSGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
246b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
252917771cSGreg Roachuse Fisharebest\Webtrees\Tree;
262917771cSGreg Roachuse Psr\Http\Message\ResponseInterface;
272917771cSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
282917771cSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
292917771cSGreg Roach
302917771cSGreg Roachuse function assert;
31ddeb3354SGreg Roachuse function is_string;
322917771cSGreg Roach
332917771cSGreg Roach/**
342917771cSGreg Roach * Add a new fact.
352917771cSGreg Roach */
362917771cSGreg Roachclass AddNewFact implements RequestHandlerInterface
372917771cSGreg Roach{
382917771cSGreg Roach    use ViewResponseTrait;
392917771cSGreg Roach
402917771cSGreg Roach    /**
412917771cSGreg Roach     * @param ServerRequestInterface $request
422917771cSGreg Roach     *
432917771cSGreg Roach     * @return ResponseInterface
442917771cSGreg Roach     */
452917771cSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
462917771cSGreg Roach    {
472917771cSGreg Roach        $tree = $request->getAttribute('tree');
482917771cSGreg Roach        assert($tree instanceof Tree);
492917771cSGreg Roach
502917771cSGreg Roach        $xref = $request->getAttribute('xref');
51ddeb3354SGreg Roach        assert(is_string($xref));
52ddeb3354SGreg Roach
532917771cSGreg Roach        $fact = $request->getAttribute('fact');
542917771cSGreg Roach
556b9cb339SGreg Roach        $record = Registry::gedcomRecordFactory()->make($xref, $tree);
56ddeb3354SGreg Roach        $record = Auth::checkRecordAccess($record, true);
572917771cSGreg Roach
58*0f18c21bSGreg Roach        $element = Registry::elementFactory()->make($record->tag() . ':' . $fact);
59*0f18c21bSGreg Roach
60*0f18c21bSGreg Roach        $title = $record->fullName() . ' - ' . $element->label();
612917771cSGreg Roach
622917771cSGreg Roach        return $this->viewResponse('edit/add-fact', [
632917771cSGreg Roach            'fact'   => $fact,
642917771cSGreg Roach            'record' => $record,
652917771cSGreg Roach            'title'  => $title,
662917771cSGreg Roach            'tree'   => $tree,
672917771cSGreg Roach        ]);
682917771cSGreg Roach    }
692917771cSGreg Roach}
70