xref: /webtrees/app/Http/RequestHandlers/EditFactPage.php (revision e93a8df2f8d797005750082cc3766c0e80799688)
1a53fee79SGreg Roach<?php
2a53fee79SGreg Roach
3a53fee79SGreg Roach/**
4a53fee79SGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 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
32b2e11eb4SGreg Roachuse function redirect;
33a53fee79SGreg Roach
34a53fee79SGreg Roach/**
35a53fee79SGreg Roach * Edit a fact.
36a53fee79SGreg Roach */
37a53fee79SGreg Roachclass EditFactPage implements RequestHandlerInterface
38a53fee79SGreg Roach{
39a53fee79SGreg Roach    use ViewResponseTrait;
40a53fee79SGreg Roach
41e22e42f7SGreg Roach    private GedcomEditService $gedcom_edit_service;
42e22e42f7SGreg Roach
43e22e42f7SGreg Roach    /**
44e22e42f7SGreg Roach     * @param GedcomEditService $gedcom_edit_service
45e22e42f7SGreg Roach     */
46e22e42f7SGreg Roach    public function __construct(GedcomEditService $gedcom_edit_service)
47e22e42f7SGreg Roach    {
48e22e42f7SGreg Roach        $this->gedcom_edit_service = $gedcom_edit_service;
49e22e42f7SGreg Roach    }
50e22e42f7SGreg Roach
51a53fee79SGreg Roach    /**
52a53fee79SGreg Roach     * @param ServerRequestInterface $request
53a53fee79SGreg Roach     *
54a53fee79SGreg Roach     * @return ResponseInterface
55a53fee79SGreg Roach     */
56a53fee79SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
57a53fee79SGreg Roach    {
58b55cbc6bSGreg Roach        $tree           = Validator::attributes($request)->tree();
59b55cbc6bSGreg Roach        $xref           = Validator::attributes($request)->isXref()->string('xref');
60b55cbc6bSGreg Roach        $fact_id        = Validator::attributes($request)->string('fact_id');
619f0bdfcdSGreg Roach        $include_hidden = Validator::queryParams($request)->boolean('include_hidden', false);
62e22e42f7SGreg Roach
636b9cb339SGreg Roach        $record = Registry::gedcomRecordFactory()->make($xref, $tree);
64a53fee79SGreg Roach        $record = Auth::checkRecordAccess($record, true);
65a53fee79SGreg Roach
66a53fee79SGreg Roach        // Find the fact to edit
67148eeb52SGreg Roach        $fact = $record->facts()->first(fn (Fact $fact): bool => $fact->id() === $fact_id && $fact->canEdit());
68a53fee79SGreg Roach
69a53fee79SGreg Roach        if ($fact === null) {
70b2e11eb4SGreg Roach            return redirect($record->url());
71a53fee79SGreg Roach        }
72a53fee79SGreg Roach
73bd29d468SGreg Roach        $can_edit_raw = Auth::isAdmin() || $tree->getPreference('SHOW_GEDCOM_RECORD') === '1';
74a53fee79SGreg Roach
75abdaad0dSGreg Roach        $gedcom = $this->gedcom_edit_service->insertMissingFactSubtags($fact, $include_hidden);
76abdaad0dSGreg Roach        $hidden = $this->gedcom_edit_service->insertMissingFactSubtags($fact, true);
779f0bdfcdSGreg Roach        $url    = Validator::queryParams($request)->isLocalUrl()->string('url', $record->url());
78e22e42f7SGreg Roach
79148eeb52SGreg Roach        if ($gedcom === $hidden) {
80148eeb52SGreg Roach            $hidden_url = '';
81148eeb52SGreg Roach        } else {
82148eeb52SGreg Roach            $hidden_url = route(self::class, [
83148eeb52SGreg Roach                'fact_id' => $fact_id,
84148eeb52SGreg Roach                'include_hidden'  => true,
85148eeb52SGreg Roach                'tree'    => $tree->name(),
86148eeb52SGreg Roach                'url'     => $url,
87148eeb52SGreg Roach                'xref'    => $xref,
88148eeb52SGreg Roach            ]);
89148eeb52SGreg Roach        }
90e22e42f7SGreg Roach
917d70e4a7SGreg Roach        $title = $record->fullName() . ' - ' . $fact->label();
92a53fee79SGreg Roach
93a53fee79SGreg Roach        return $this->viewResponse('edit/edit-fact', [
94a53fee79SGreg Roach            'can_edit_raw' => $can_edit_raw,
959db6d3cbSGreg Roach            'fact'         => $fact,
96148eeb52SGreg Roach            'gedcom'       => $gedcom,
97148eeb52SGreg Roach            'hidden_url'   => $hidden_url,
98a53fee79SGreg Roach            'title'        => $title,
99a53fee79SGreg Roach            'tree'         => $tree,
100148eeb52SGreg Roach            'url'          => $url,
101a53fee79SGreg Roach        ]);
102a53fee79SGreg Roach    }
103a53fee79SGreg Roach}
104