xref: /webtrees/app/Http/RequestHandlers/DeleteFact.php (revision 3b3db8adf3fd54bb8a196a781da9123905e54adf)
1*3b3db8adSGreg Roach<?php
2*3b3db8adSGreg Roach
3*3b3db8adSGreg Roach/**
4*3b3db8adSGreg Roach * webtrees: online genealogy
5*3b3db8adSGreg Roach * Copyright (C) 2019 webtrees development team
6*3b3db8adSGreg Roach * This program is free software: you can redistribute it and/or modify
7*3b3db8adSGreg Roach * it under the terms of the GNU General Public License as published by
8*3b3db8adSGreg Roach * the Free Software Foundation, either version 3 of the License, or
9*3b3db8adSGreg Roach * (at your option) any later version.
10*3b3db8adSGreg Roach * This program is distributed in the hope that it will be useful,
11*3b3db8adSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12*3b3db8adSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13*3b3db8adSGreg Roach * GNU General Public License for more details.
14*3b3db8adSGreg Roach * You should have received a copy of the GNU General Public License
15*3b3db8adSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
16*3b3db8adSGreg Roach */
17*3b3db8adSGreg Roach
18*3b3db8adSGreg Roachdeclare(strict_types=1);
19*3b3db8adSGreg Roach
20*3b3db8adSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
21*3b3db8adSGreg Roach
22*3b3db8adSGreg Roachuse Fisharebest\Webtrees\Auth;
23*3b3db8adSGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
24*3b3db8adSGreg Roachuse Fisharebest\Webtrees\Tree;
25*3b3db8adSGreg Roachuse Psr\Http\Message\ResponseInterface;
26*3b3db8adSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
27*3b3db8adSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
28*3b3db8adSGreg Roach
29*3b3db8adSGreg Roachuse function assert;
30*3b3db8adSGreg Roachuse function response;
31*3b3db8adSGreg Roach
32*3b3db8adSGreg Roach/**
33*3b3db8adSGreg Roach * Controller for edit forms and responses.
34*3b3db8adSGreg Roach */
35*3b3db8adSGreg Roachclass DeleteFact implements RequestHandlerInterface
36*3b3db8adSGreg Roach{
37*3b3db8adSGreg Roach    /**
38*3b3db8adSGreg Roach     * Delete a fact.
39*3b3db8adSGreg Roach     *
40*3b3db8adSGreg Roach     * @param ServerRequestInterface $request
41*3b3db8adSGreg Roach     *
42*3b3db8adSGreg Roach     * @return ResponseInterface
43*3b3db8adSGreg Roach     */
44*3b3db8adSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
45*3b3db8adSGreg Roach    {
46*3b3db8adSGreg Roach        $tree = $request->getAttribute('tree');
47*3b3db8adSGreg Roach        assert($tree instanceof Tree);
48*3b3db8adSGreg Roach
49*3b3db8adSGreg Roach        $xref    = $request->getAttribute('xref');
50*3b3db8adSGreg Roach        $fact_id = $request->getAttribute('fact_id');
51*3b3db8adSGreg Roach        $record  = GedcomRecord::getInstance($xref, $tree);
52*3b3db8adSGreg Roach
53*3b3db8adSGreg Roach        Auth::checkRecordAccess($record, true);
54*3b3db8adSGreg Roach
55*3b3db8adSGreg Roach        foreach ($record->facts() as $fact) {
56*3b3db8adSGreg Roach            if ($fact->id() === $fact_id && $fact->canEdit()) {
57*3b3db8adSGreg Roach                $record->deleteFact($fact_id, true);
58*3b3db8adSGreg Roach                break;
59*3b3db8adSGreg Roach            }
60*3b3db8adSGreg Roach        }
61*3b3db8adSGreg Roach
62*3b3db8adSGreg Roach        return response();
63*3b3db8adSGreg Roach    }
64*3b3db8adSGreg Roach}
65