xref: /webtrees/app/Http/RequestHandlers/EditFactPage.php (revision 5bfc689774bb9a6401271c4ed15a6d50652c991b)
1a53fee79SGreg Roach<?php
2a53fee79SGreg Roach
3a53fee79SGreg Roach/**
4a53fee79SGreg Roach * webtrees: online genealogy
5*5bfc6897SGreg Roach * Copyright (C) 2022 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
1589f7189bSGreg Roach * along with this program. If not, see <https://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;
26e22e42f7SGreg Roachuse Fisharebest\Webtrees\Services\GedcomEditService;
27b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
28a53fee79SGreg Roachuse Psr\Http\Message\ResponseInterface;
29a53fee79SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
30a53fee79SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
31a53fee79SGreg Roach
32a53fee79SGreg Roachuse function assert;
33fb2382c0SGreg Roachuse function is_string;
34b2e11eb4SGreg Roachuse function redirect;
35a53fee79SGreg Roach
36a53fee79SGreg Roach/**
37a53fee79SGreg Roach * Edit a fact.
38a53fee79SGreg Roach */
39a53fee79SGreg Roachclass EditFactPage implements RequestHandlerInterface
40a53fee79SGreg Roach{
41a53fee79SGreg Roach    use ViewResponseTrait;
42a53fee79SGreg Roach
43e22e42f7SGreg Roach    private GedcomEditService $gedcom_edit_service;
44e22e42f7SGreg Roach
45e22e42f7SGreg Roach    /**
46e22e42f7SGreg Roach     * AddNewFact constructor.
47e22e42f7SGreg Roach     *
48e22e42f7SGreg Roach     * @param GedcomEditService $gedcom_edit_service
49e22e42f7SGreg Roach     */
50e22e42f7SGreg Roach    public function __construct(GedcomEditService $gedcom_edit_service)
51e22e42f7SGreg Roach    {
52e22e42f7SGreg Roach        $this->gedcom_edit_service = $gedcom_edit_service;
53e22e42f7SGreg Roach    }
54e22e42f7SGreg Roach
55a53fee79SGreg Roach    /**
56a53fee79SGreg Roach     * @param ServerRequestInterface $request
57a53fee79SGreg Roach     *
58a53fee79SGreg Roach     * @return ResponseInterface
59a53fee79SGreg Roach     */
60a53fee79SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
61a53fee79SGreg Roach    {
62b55cbc6bSGreg Roach        $tree    = Validator::attributes($request)->tree();
63b55cbc6bSGreg Roach        $xref    = Validator::attributes($request)->isXref()->string('xref');
64b55cbc6bSGreg Roach        $fact_id = Validator::attributes($request)->string('fact_id');
65e22e42f7SGreg Roach        $include_hidden = (bool) ($request->getQueryParams()['include_hidden'] ?? false);
66e22e42f7SGreg Roach
676b9cb339SGreg Roach        $record = Registry::gedcomRecordFactory()->make($xref, $tree);
68a53fee79SGreg Roach        $record = Auth::checkRecordAccess($record, true);
69a53fee79SGreg Roach
70a53fee79SGreg Roach        // Find the fact to edit
71148eeb52SGreg Roach        $fact = $record->facts()->first(fn (Fact $fact): bool => $fact->id() === $fact_id && $fact->canEdit());
72a53fee79SGreg Roach
73a53fee79SGreg Roach        if ($fact === null) {
74b2e11eb4SGreg Roach            return redirect($record->url());
75a53fee79SGreg Roach        }
76a53fee79SGreg Roach
77a53fee79SGreg Roach        $can_edit_raw = Auth::isAdmin() || $tree->getPreference('SHOW_GEDCOM_RECORD');
78a53fee79SGreg Roach
79abdaad0dSGreg Roach        $gedcom = $this->gedcom_edit_service->insertMissingFactSubtags($fact, $include_hidden);
80abdaad0dSGreg Roach        $hidden = $this->gedcom_edit_service->insertMissingFactSubtags($fact, true);
81148eeb52SGreg Roach        $url = $request->getQueryParams()['url'] ?? $record->url();
82e22e42f7SGreg Roach
83148eeb52SGreg Roach        if ($gedcom === $hidden) {
84148eeb52SGreg Roach            $hidden_url = '';
85148eeb52SGreg Roach        } else {
86148eeb52SGreg Roach            $hidden_url = route(self::class, [
87148eeb52SGreg Roach                'fact_id' => $fact_id,
88148eeb52SGreg Roach                'include_hidden'  => true,
89148eeb52SGreg Roach                'tree'    => $tree->name(),
90148eeb52SGreg Roach                'url'     => $url,
91148eeb52SGreg Roach                'xref'    => $xref,
92148eeb52SGreg Roach            ]);
93148eeb52SGreg Roach        }
94e22e42f7SGreg Roach
957d70e4a7SGreg Roach        $title = $record->fullName() . ' - ' . $fact->label();
96a53fee79SGreg Roach
97a53fee79SGreg Roach        return $this->viewResponse('edit/edit-fact', [
98a53fee79SGreg Roach            'can_edit_raw' => $can_edit_raw,
999db6d3cbSGreg Roach            'fact'         => $fact,
100148eeb52SGreg Roach            'gedcom'       => $gedcom,
101148eeb52SGreg Roach            'hidden_url'   => $hidden_url,
102a53fee79SGreg Roach            'title'        => $title,
103a53fee79SGreg Roach            'tree'         => $tree,
104148eeb52SGreg Roach            'url'          => $url,
105a53fee79SGreg Roach        ]);
106a53fee79SGreg Roach    }
107a53fee79SGreg Roach}
108