xref: /webtrees/app/Http/RequestHandlers/AddNewFact.php (revision a9866bf217fcbf79695b119a127478d64260caff)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22use Fisharebest\Webtrees\Auth;
23use Fisharebest\Webtrees\Fact;
24use Fisharebest\Webtrees\Http\Exceptions\HttpAccessDeniedException;
25use Fisharebest\Webtrees\Http\ViewResponseTrait;
26use Fisharebest\Webtrees\Registry;
27use Fisharebest\Webtrees\Services\AuthorizationService;
28use Fisharebest\Webtrees\Services\GedcomEditService;
29use Fisharebest\Webtrees\Tree;
30use Psr\Http\Message\ResponseInterface;
31use Psr\Http\Message\ServerRequestInterface;
32use Psr\Http\Server\RequestHandlerInterface;
33
34use function assert;
35use function route;
36use function trim;
37
38/**
39 * Add a new fact.
40 */
41class AddNewFact implements RequestHandlerInterface
42{
43    use ViewResponseTrait;
44
45    private AuthorizationService $authorization_service;
46
47    private GedcomEditService $gedcom_edit_service;
48
49    /**
50     * AddNewFact constructor.
51     *
52     * @param AuthorizationService $authorization_service
53     * @param GedcomEditService    $gedcom_edit_service
54     */
55    public function __construct(AuthorizationService $authorization_service, GedcomEditService $gedcom_edit_service)
56    {
57        $this->authorization_service = $authorization_service;
58        $this->gedcom_edit_service   = $gedcom_edit_service;
59    }
60
61    /**
62     * @param ServerRequestInterface $request
63     *
64     * @return ResponseInterface
65     */
66    public function handle(ServerRequestInterface $request): ResponseInterface
67    {
68        $tree = $request->getAttribute('tree');
69        assert($tree instanceof Tree);
70
71        $xref   = (string) $request->getAttribute('xref');
72        $subtag = (string) $request->getAttribute('fact');
73
74        if ($subtag === 'OBJE' && !$this->authorization_service->canUploadMedia($tree, Auth::user())) {
75            throw new HttpAccessDeniedException();
76        }
77
78        $include_hidden = (bool) ($request->getQueryParams()['include_hidden'] ?? false);
79
80        $record  = Registry::gedcomRecordFactory()->make($xref, $tree);
81        $record  = Auth::checkRecordAccess($record, true);
82        $element = Registry::elementFactory()->make($record->tag() . ':' . $subtag);
83        $title   = $record->fullName() . ' - ' . $element->label();
84        $fact    = new Fact(trim('1 ' . $subtag . ' ' . $element->default($tree)), $record, 'new');
85        $gedcom  = $this->gedcom_edit_service->insertMissingFactSubtags($fact, $include_hidden);
86        $hidden  = $this->gedcom_edit_service->insertMissingFactSubtags($fact, true);
87        $url     = $record->url();
88
89        if ($gedcom === $hidden) {
90            $hidden_url = '';
91        } else {
92            $hidden_url = route(self::class, [
93                'fact'           => $subtag,
94                'include_hidden' => true,
95                'tree'           => $tree->name(),
96                'xref'           => $xref,
97            ]);
98        }
99
100        return $this->viewResponse('edit/edit-fact', [
101            'can_edit_raw' => false,
102            'fact'         => $fact,
103            'gedcom'       => $gedcom,
104            'hidden_url'   => $hidden_url,
105            'title'        => $title,
106            'tree'         => $tree,
107            'url'          => $url,
108        ]);
109    }
110}
111