xref: /webtrees/app/Http/RequestHandlers/EditFactPage.php (revision b2e11eb48118d4fe344da52b86d7e015c857e2a6)
1a53fee79SGreg Roach<?php
2a53fee79SGreg Roach
3a53fee79SGreg Roach/**
4a53fee79SGreg Roach * webtrees: online genealogy
5a53fee79SGreg Roach * Copyright (C) 2020 webtrees development team
6a53fee79SGreg Roach * This program is free software: you can redistribute it and/or modify
7a53fee79SGreg Roach * it under the terms of the GNU General Public License as published by
8a53fee79SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9a53fee79SGreg Roach * (at your option) any later version.
10a53fee79SGreg Roach * This program is distributed in the hope that it will be useful,
11a53fee79SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12a53fee79SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13a53fee79SGreg Roach * GNU General Public License for more details.
14a53fee79SGreg Roach * You should have received a copy of the GNU General Public License
15a53fee79SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
16a53fee79SGreg Roach */
17a53fee79SGreg Roach
18a53fee79SGreg Roachdeclare(strict_types=1);
19a53fee79SGreg Roach
20a53fee79SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
21a53fee79SGreg Roach
22a53fee79SGreg Roachuse Fisharebest\Webtrees\Auth;
23a53fee79SGreg Roachuse Fisharebest\Webtrees\Fact;
24a53fee79SGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
256b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
26a53fee79SGreg Roachuse Fisharebest\Webtrees\Tree;
27a53fee79SGreg Roachuse Psr\Http\Message\ResponseInterface;
28a53fee79SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
29a53fee79SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
30a53fee79SGreg Roach
31a53fee79SGreg Roachuse function assert;
32fb2382c0SGreg Roachuse function is_string;
33*b2e11eb4SGreg Roachuse function redirect;
34a53fee79SGreg Roach
35a53fee79SGreg Roach/**
36a53fee79SGreg Roach * Edit a fact.
37a53fee79SGreg Roach */
38a53fee79SGreg Roachclass EditFactPage implements RequestHandlerInterface
39a53fee79SGreg Roach{
40a53fee79SGreg Roach    use ViewResponseTrait;
41a53fee79SGreg Roach
42a53fee79SGreg Roach    /**
43a53fee79SGreg Roach     * @param ServerRequestInterface $request
44a53fee79SGreg Roach     *
45a53fee79SGreg Roach     * @return ResponseInterface
46a53fee79SGreg Roach     */
47a53fee79SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
48a53fee79SGreg Roach    {
49a53fee79SGreg Roach        $tree = $request->getAttribute('tree');
50a53fee79SGreg Roach        assert($tree instanceof Tree);
51a53fee79SGreg Roach
52fb2382c0SGreg Roach        $xref = $request->getAttribute('xref');
53fb2382c0SGreg Roach        assert(is_string($xref));
54fb2382c0SGreg Roach
55fb2382c0SGreg Roach        $fact_id = $request->getAttribute('fact_id');
56fb2382c0SGreg Roach        assert(is_string($fact_id));
57a53fee79SGreg Roach
586b9cb339SGreg Roach        $record = Registry::gedcomRecordFactory()->make($xref, $tree);
59a53fee79SGreg Roach        $record = Auth::checkRecordAccess($record, true);
60a53fee79SGreg Roach
61a53fee79SGreg Roach        // Find the fact to edit
62a53fee79SGreg Roach        $fact = $record->facts()
63a53fee79SGreg Roach            ->first(static function (Fact $fact) use ($fact_id): bool {
64a53fee79SGreg Roach                return $fact->id() === $fact_id && $fact->canEdit();
65a53fee79SGreg Roach            });
66a53fee79SGreg Roach
67a53fee79SGreg Roach        if ($fact === null) {
68*b2e11eb4SGreg Roach            return redirect($record->url());
69a53fee79SGreg Roach        }
70a53fee79SGreg Roach
71a53fee79SGreg Roach        $can_edit_raw = Auth::isAdmin() || $tree->getPreference('SHOW_GEDCOM_RECORD');
72a53fee79SGreg Roach
737d70e4a7SGreg Roach        $title = $record->fullName() . ' - ' . $fact->label();
74a53fee79SGreg Roach
75a53fee79SGreg Roach        return $this->viewResponse('edit/edit-fact', [
76a53fee79SGreg Roach            'can_edit_raw' => $can_edit_raw,
779db6d3cbSGreg Roach            'fact'         => $fact,
78a53fee79SGreg Roach            'title'        => $title,
79a53fee79SGreg Roach            'tree'         => $tree,
80c531f071SGreg Roach            'url'          => $request->getQueryParams()['url'] ?? $record->url(),
81a53fee79SGreg Roach        ]);
82a53fee79SGreg Roach    }
83a53fee79SGreg Roach}
84