1a53fee79SGreg Roach<?php 2a53fee79SGreg Roach 3a53fee79SGreg Roach/** 4a53fee79SGreg Roach * webtrees: online genealogy 55bfc6897SGreg 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 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 * AddNewFact constructor. 45e22e42f7SGreg Roach * 46e22e42f7SGreg Roach * @param GedcomEditService $gedcom_edit_service 47e22e42f7SGreg Roach */ 48e22e42f7SGreg Roach public function __construct(GedcomEditService $gedcom_edit_service) 49e22e42f7SGreg Roach { 50e22e42f7SGreg Roach $this->gedcom_edit_service = $gedcom_edit_service; 51e22e42f7SGreg Roach } 52e22e42f7SGreg Roach 53a53fee79SGreg Roach /** 54a53fee79SGreg Roach * @param ServerRequestInterface $request 55a53fee79SGreg Roach * 56a53fee79SGreg Roach * @return ResponseInterface 57a53fee79SGreg Roach */ 58a53fee79SGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 59a53fee79SGreg Roach { 60b55cbc6bSGreg Roach $tree = Validator::attributes($request)->tree(); 61b55cbc6bSGreg Roach $xref = Validator::attributes($request)->isXref()->string('xref'); 62b55cbc6bSGreg Roach $fact_id = Validator::attributes($request)->string('fact_id'); 639f0bdfcdSGreg Roach $include_hidden = Validator::queryParams($request)->boolean('include_hidden', false); 64e22e42f7SGreg Roach 656b9cb339SGreg Roach $record = Registry::gedcomRecordFactory()->make($xref, $tree); 66a53fee79SGreg Roach $record = Auth::checkRecordAccess($record, true); 67a53fee79SGreg Roach 68a53fee79SGreg Roach // Find the fact to edit 69148eeb52SGreg Roach $fact = $record->facts()->first(fn (Fact $fact): bool => $fact->id() === $fact_id && $fact->canEdit()); 70a53fee79SGreg Roach 71a53fee79SGreg Roach if ($fact === null) { 72b2e11eb4SGreg Roach return redirect($record->url()); 73a53fee79SGreg Roach } 74a53fee79SGreg Roach 75*bd29d468SGreg Roach $can_edit_raw = Auth::isAdmin() || $tree->getPreference('SHOW_GEDCOM_RECORD') === '1'; 76a53fee79SGreg Roach 77abdaad0dSGreg Roach $gedcom = $this->gedcom_edit_service->insertMissingFactSubtags($fact, $include_hidden); 78abdaad0dSGreg Roach $hidden = $this->gedcom_edit_service->insertMissingFactSubtags($fact, true); 799f0bdfcdSGreg Roach $url = Validator::queryParams($request)->isLocalUrl()->string('url', $record->url()); 80e22e42f7SGreg Roach 81148eeb52SGreg Roach if ($gedcom === $hidden) { 82148eeb52SGreg Roach $hidden_url = ''; 83148eeb52SGreg Roach } else { 84148eeb52SGreg Roach $hidden_url = route(self::class, [ 85148eeb52SGreg Roach 'fact_id' => $fact_id, 86148eeb52SGreg Roach 'include_hidden' => true, 87148eeb52SGreg Roach 'tree' => $tree->name(), 88148eeb52SGreg Roach 'url' => $url, 89148eeb52SGreg Roach 'xref' => $xref, 90148eeb52SGreg Roach ]); 91148eeb52SGreg Roach } 92e22e42f7SGreg Roach 937d70e4a7SGreg Roach $title = $record->fullName() . ' - ' . $fact->label(); 94a53fee79SGreg Roach 95a53fee79SGreg Roach return $this->viewResponse('edit/edit-fact', [ 96a53fee79SGreg Roach 'can_edit_raw' => $can_edit_raw, 979db6d3cbSGreg Roach 'fact' => $fact, 98148eeb52SGreg Roach 'gedcom' => $gedcom, 99148eeb52SGreg Roach 'hidden_url' => $hidden_url, 100a53fee79SGreg Roach 'title' => $title, 101a53fee79SGreg Roach 'tree' => $tree, 102148eeb52SGreg Roach 'url' => $url, 103a53fee79SGreg Roach ]); 104a53fee79SGreg Roach } 105a53fee79SGreg Roach} 106