12917771cSGreg Roach<?php 22917771cSGreg Roach 32917771cSGreg Roach/** 42917771cSGreg Roach * webtrees: online genealogy 55bfc6897SGreg Roach * Copyright (C) 2022 webtrees development team 62917771cSGreg Roach * This program is free software: you can redistribute it and/or modify 72917771cSGreg Roach * it under the terms of the GNU General Public License as published by 82917771cSGreg Roach * the Free Software Foundation, either version 3 of the License, or 92917771cSGreg Roach * (at your option) any later version. 102917771cSGreg Roach * This program is distributed in the hope that it will be useful, 112917771cSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 122917771cSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 132917771cSGreg Roach * GNU General Public License for more details. 142917771cSGreg 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/>. 162917771cSGreg Roach */ 172917771cSGreg Roach 182917771cSGreg Roachdeclare(strict_types=1); 192917771cSGreg Roach 202917771cSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers; 212917771cSGreg Roach 222917771cSGreg Roachuse Fisharebest\Webtrees\Auth; 23efd4768bSGreg Roachuse Fisharebest\Webtrees\Fact; 240ea4a3f2SGreg Roachuse Fisharebest\Webtrees\Http\Exceptions\HttpAccessDeniedException; 252917771cSGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait; 266b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry; 27e22e42f7SGreg Roachuse Fisharebest\Webtrees\Services\GedcomEditService; 28b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator; 292917771cSGreg Roachuse Psr\Http\Message\ResponseInterface; 302917771cSGreg Roachuse Psr\Http\Message\ServerRequestInterface; 312917771cSGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 322917771cSGreg Roach 33148eeb52SGreg Roachuse function route; 34efd4768bSGreg Roachuse function trim; 352917771cSGreg Roach 362917771cSGreg Roach/** 372917771cSGreg Roach * Add a new fact. 382917771cSGreg Roach */ 392917771cSGreg Roachclass AddNewFact implements RequestHandlerInterface 402917771cSGreg Roach{ 412917771cSGreg Roach use ViewResponseTrait; 422917771cSGreg 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 */ 505416d6ddSGreg Roach public function __construct(GedcomEditService $gedcom_edit_service) 51e22e42f7SGreg Roach { 52e22e42f7SGreg Roach $this->gedcom_edit_service = $gedcom_edit_service; 53e22e42f7SGreg Roach } 54e22e42f7SGreg Roach 552917771cSGreg Roach /** 562917771cSGreg Roach * @param ServerRequestInterface $request 572917771cSGreg Roach * 582917771cSGreg Roach * @return ResponseInterface 592917771cSGreg Roach */ 602917771cSGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 612917771cSGreg Roach { 62b55cbc6bSGreg Roach $tree = Validator::attributes($request)->tree(); 63b55cbc6bSGreg Roach $xref = Validator::attributes($request)->isXref()->string('xref'); 64b55cbc6bSGreg Roach $subtag = Validator::attributes($request)->isTag()->string('fact'); 65ddeb3354SGreg Roach 665416d6ddSGreg Roach if ($subtag === 'OBJE' && !Auth::canUploadMedia($tree, Auth::user())) { 670ea4a3f2SGreg Roach throw new HttpAccessDeniedException(); 680ea4a3f2SGreg Roach } 69e22e42f7SGreg Roach 70*748dbe15SGreg Roach $include_hidden = Validator::queryParams($request)->boolean('include_hidden', false); 71e22e42f7SGreg Roach 726b9cb339SGreg Roach $record = Registry::gedcomRecordFactory()->make($xref, $tree); 73ddeb3354SGreg Roach $record = Auth::checkRecordAccess($record, true); 74efd4768bSGreg Roach $element = Registry::elementFactory()->make($record->tag() . ':' . $subtag); 750f18c21bSGreg Roach $title = $record->fullName() . ' - ' . $element->label(); 76efd4768bSGreg Roach $fact = new Fact(trim('1 ' . $subtag . ' ' . $element->default($tree)), $record, 'new'); 77abdaad0dSGreg Roach $gedcom = $this->gedcom_edit_service->insertMissingFactSubtags($fact, $include_hidden); 78abdaad0dSGreg Roach $hidden = $this->gedcom_edit_service->insertMissingFactSubtags($fact, true); 79148eeb52SGreg Roach $url = $record->url(); 80148eeb52SGreg Roach 81148eeb52SGreg Roach if ($gedcom === $hidden) { 82148eeb52SGreg Roach $hidden_url = ''; 83148eeb52SGreg Roach } else { 84148eeb52SGreg Roach $hidden_url = route(self::class, [ 85148eeb52SGreg Roach 'fact' => $subtag, 86148eeb52SGreg Roach 'include_hidden' => true, 87148eeb52SGreg Roach 'tree' => $tree->name(), 88148eeb52SGreg Roach 'xref' => $xref, 89148eeb52SGreg Roach ]); 90148eeb52SGreg Roach } 912917771cSGreg Roach 92efd4768bSGreg Roach return $this->viewResponse('edit/edit-fact', [ 93efd4768bSGreg Roach 'can_edit_raw' => false, 942917771cSGreg Roach 'fact' => $fact, 95148eeb52SGreg Roach 'gedcom' => $gedcom, 96148eeb52SGreg Roach 'hidden_url' => $hidden_url, 972917771cSGreg Roach 'title' => $title, 982917771cSGreg Roach 'tree' => $tree, 99148eeb52SGreg Roach 'url' => $url, 1002917771cSGreg Roach ]); 1012917771cSGreg Roach } 1022917771cSGreg Roach} 103