xref: /webtrees/app/Http/RequestHandlers/AddNewFact.php (revision 43f2f523bcb6d4090564d23802872c0679ede6bc)
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 is_string;
36use function route;
37use function trim;
38
39/**
40 * Add a new fact.
41 */
42class AddNewFact implements RequestHandlerInterface
43{
44    use ViewResponseTrait;
45
46    private AuthorizationService $authorization_service;
47
48    private GedcomEditService $gedcom_edit_service;
49
50    /**
51     * AddNewFact constructor.
52     *
53     * @param AuthorizationService $authorization_service
54     * @param GedcomEditService    $gedcom_edit_service
55     */
56    public function __construct(AuthorizationService $authorization_service, GedcomEditService $gedcom_edit_service)
57    {
58        $this->authorization_service = $authorization_service;
59        $this->gedcom_edit_service   = $gedcom_edit_service;
60    }
61
62    /**
63     * @param ServerRequestInterface $request
64     *
65     * @return ResponseInterface
66     */
67    public function handle(ServerRequestInterface $request): ResponseInterface
68    {
69        $tree = $request->getAttribute('tree');
70        assert($tree instanceof Tree);
71
72        $xref   = (string) $request->getAttribute('xref');
73        $subtag = (string) $request->getAttribute('fact');
74
75        if ($subtag === 'OBJE' && !$this->authorization_service->canUploadMedia($tree, Auth::user())) {
76            throw new HttpAccessDeniedException();
77        }
78
79        $include_hidden = (bool) ($request->getQueryParams()['include_hidden'] ?? false);
80
81        $record  = Registry::gedcomRecordFactory()->make($xref, $tree);
82        $record  = Auth::checkRecordAccess($record, true);
83        $element = Registry::elementFactory()->make($record->tag() . ':' . $subtag);
84        $title   = $record->fullName() . ' - ' . $element->label();
85        $fact    = new Fact(trim('1 ' . $subtag . ' ' . $element->default($tree)), $record, 'new');
86        $gedcom  = $this->gedcom_edit_service->insertMissingFactSubtags($fact, $include_hidden);
87        $hidden  = $this->gedcom_edit_service->insertMissingFactSubtags($fact, true);
88        $url     = $record->url();
89
90        if ($gedcom === $hidden) {
91            $hidden_url = '';
92        } else {
93            $hidden_url = route(self::class, [
94                'fact'           => $subtag,
95                'include_hidden' => true,
96                'tree'           => $tree->name(),
97                'xref'           => $xref,
98            ]);
99        }
100
101        return $this->viewResponse('edit/edit-fact', [
102            'can_edit_raw' => false,
103            'fact'         => $fact,
104            'gedcom'       => $gedcom,
105            'hidden_url'   => $hidden_url,
106            'title'        => $title,
107            'tree'         => $tree,
108            'url'          => $url,
109        ]);
110    }
111}
112