xref: /webtrees/app/Http/RequestHandlers/AddNewFact.php (revision e93a8df2f8d797005750082cc3766c0e80799688)
12917771cSGreg Roach<?php
22917771cSGreg Roach
32917771cSGreg Roach/**
42917771cSGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 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;
240ea4a3f2SGreg Roachuse Fisharebest\Webtrees\Http\Exceptions\HttpAccessDeniedException;
252917771cSGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
266b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
27e22e42f7SGreg Roachuse Fisharebest\Webtrees\Services\GedcomEditService;
28b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
292917771cSGreg Roachuse Psr\Http\Message\ResponseInterface;
302917771cSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
312917771cSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
322917771cSGreg Roach
33148eeb52SGreg Roachuse function route;
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
43e22e42f7SGreg Roach    private GedcomEditService $gedcom_edit_service;
44e22e42f7SGreg Roach
45e22e42f7SGreg Roach    /**
46e22e42f7SGreg Roach     * @param GedcomEditService $gedcom_edit_service
47e22e42f7SGreg Roach     */
485416d6ddSGreg Roach    public function __construct(GedcomEditService $gedcom_edit_service)
49e22e42f7SGreg Roach    {
50e22e42f7SGreg Roach        $this->gedcom_edit_service = $gedcom_edit_service;
51e22e42f7SGreg Roach    }
52e22e42f7SGreg Roach
532917771cSGreg Roach    /**
542917771cSGreg Roach     * @param ServerRequestInterface $request
552917771cSGreg Roach     *
562917771cSGreg Roach     * @return ResponseInterface
572917771cSGreg Roach     */
582917771cSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
592917771cSGreg Roach    {
60b55cbc6bSGreg Roach        $tree   = Validator::attributes($request)->tree();
61b55cbc6bSGreg Roach        $xref   = Validator::attributes($request)->isXref()->string('xref');
62b55cbc6bSGreg Roach        $subtag = Validator::attributes($request)->isTag()->string('fact');
63ddeb3354SGreg Roach
645416d6ddSGreg Roach        if ($subtag === 'OBJE' && !Auth::canUploadMedia($tree, Auth::user())) {
650ea4a3f2SGreg Roach            throw new HttpAccessDeniedException();
660ea4a3f2SGreg Roach        }
67e22e42f7SGreg Roach
68748dbe15SGreg Roach        $include_hidden = Validator::queryParams($request)->boolean('include_hidden', false);
69e22e42f7SGreg Roach
706b9cb339SGreg Roach        $record  = Registry::gedcomRecordFactory()->make($xref, $tree);
71ddeb3354SGreg Roach        $record  = Auth::checkRecordAccess($record, true);
72efd4768bSGreg Roach        $element = Registry::elementFactory()->make($record->tag() . ':' . $subtag);
730f18c21bSGreg Roach        $title   = $record->fullName() . ' - ' . $element->label();
74efd4768bSGreg Roach        $fact    = new Fact(trim('1 ' . $subtag . ' ' . $element->default($tree)), $record, 'new');
75abdaad0dSGreg Roach        $gedcom  = $this->gedcom_edit_service->insertMissingFactSubtags($fact, $include_hidden);
76abdaad0dSGreg Roach        $hidden  = $this->gedcom_edit_service->insertMissingFactSubtags($fact, true);
77148eeb52SGreg Roach        $url     = $record->url();
78148eeb52SGreg Roach
79148eeb52SGreg Roach        if ($gedcom === $hidden) {
80148eeb52SGreg Roach            $hidden_url = '';
81148eeb52SGreg Roach        } else {
82148eeb52SGreg Roach            $hidden_url = route(self::class, [
83148eeb52SGreg Roach                'fact'           => $subtag,
84148eeb52SGreg Roach                'include_hidden' => true,
85148eeb52SGreg Roach                'tree'           => $tree->name(),
86148eeb52SGreg Roach                'xref'           => $xref,
87148eeb52SGreg Roach            ]);
88148eeb52SGreg Roach        }
892917771cSGreg Roach
90efd4768bSGreg Roach        return $this->viewResponse('edit/edit-fact', [
91efd4768bSGreg Roach            'can_edit_raw' => false,
922917771cSGreg Roach            'fact'         => $fact,
93148eeb52SGreg Roach            'gedcom'       => $gedcom,
94148eeb52SGreg Roach            'hidden_url'   => $hidden_url,
952917771cSGreg Roach            'title'        => $title,
962917771cSGreg Roach            'tree'         => $tree,
97148eeb52SGreg Roach            'url'          => $url,
982917771cSGreg Roach        ]);
992917771cSGreg Roach    }
1002917771cSGreg Roach}
101