155749c76SGreg Roach<?php 255749c76SGreg Roach 355749c76SGreg Roach/** 455749c76SGreg Roach * webtrees: online genealogy 5*d11be702SGreg Roach * Copyright (C) 2023 webtrees development team 655749c76SGreg Roach * This program is free software: you can redistribute it and/or modify 755749c76SGreg Roach * it under the terms of the GNU General Public License as published by 855749c76SGreg Roach * the Free Software Foundation, either version 3 of the License, or 955749c76SGreg Roach * (at your option) any later version. 1055749c76SGreg Roach * This program is distributed in the hope that it will be useful, 1155749c76SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 1255749c76SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1355749c76SGreg Roach * GNU General Public License for more details. 1455749c76SGreg 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/>. 1655749c76SGreg Roach */ 1755749c76SGreg Roach 1855749c76SGreg Roachdeclare(strict_types=1); 1955749c76SGreg Roach 2055749c76SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers; 2155749c76SGreg Roach 2255749c76SGreg Roachuse Fisharebest\Webtrees\Auth; 2355749c76SGreg Roachuse Fisharebest\Webtrees\Individual; 2455749c76SGreg Roachuse Fisharebest\Webtrees\Module\CensusAssistantModule; 256b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry; 2655749c76SGreg Roachuse Fisharebest\Webtrees\Services\GedcomEditService; 2755749c76SGreg Roachuse Fisharebest\Webtrees\Services\ModuleService; 288d9c2b68SGreg Roachuse Fisharebest\Webtrees\Validator; 2955749c76SGreg Roachuse Psr\Http\Message\ResponseInterface; 3055749c76SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 3155749c76SGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 3255749c76SGreg Roach 3355749c76SGreg Roachuse function redirect; 3455749c76SGreg Roach 3555749c76SGreg Roach/** 3655749c76SGreg Roach * Save an updated GEDCOM fact. 3755749c76SGreg Roach */ 3855749c76SGreg Roachclass EditFactAction implements RequestHandlerInterface 3955749c76SGreg Roach{ 40c4943cffSGreg Roach private GedcomEditService $gedcom_edit_service; 4155749c76SGreg Roach 42c4943cffSGreg Roach private ModuleService $module_service; 4355749c76SGreg Roach 4455749c76SGreg Roach /** 4555749c76SGreg Roach * @param GedcomEditService $gedcom_edit_service 4655749c76SGreg Roach * @param ModuleService $module_service 4755749c76SGreg Roach */ 4855749c76SGreg Roach public function __construct(GedcomEditService $gedcom_edit_service, ModuleService $module_service) 4955749c76SGreg Roach { 5055749c76SGreg Roach $this->gedcom_edit_service = $gedcom_edit_service; 5155749c76SGreg Roach $this->module_service = $module_service; 5255749c76SGreg Roach } 5355749c76SGreg Roach 5455749c76SGreg Roach /** 5555749c76SGreg Roach * @param ServerRequestInterface $request 5655749c76SGreg Roach * 5755749c76SGreg Roach * @return ResponseInterface 5855749c76SGreg Roach */ 5955749c76SGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 6055749c76SGreg Roach { 61b55cbc6bSGreg Roach $tree = Validator::attributes($request)->tree(); 62b55cbc6bSGreg Roach $xref = Validator::attributes($request)->isXref()->string('xref'); 63b55cbc6bSGreg Roach $fact_id = Validator::attributes($request)->string('fact_id'); 6455749c76SGreg Roach 656b9cb339SGreg Roach $record = Registry::gedcomRecordFactory()->make($xref, $tree); 6655749c76SGreg Roach $record = Auth::checkRecordAccess($record, true); 6755749c76SGreg Roach 68a92a07c7SGreg Roach $keep_chan = Validator::parsedBody($request)->boolean('keep_chan', false); 69a92a07c7SGreg Roach $levels = Validator::parsedBody($request)->array('levels'); 70a92a07c7SGreg Roach $tags = Validator::parsedBody($request)->array('tags'); 71a92a07c7SGreg Roach $values = Validator::parsedBody($request)->array('values'); 72b389d323SGreg Roach $gedcom = $this->gedcom_edit_service->editLinesToGedcom($record::RECORD_TYPE, $levels, $tags, $values, false); 7355749c76SGreg Roach 7455749c76SGreg Roach $census_assistant = $this->module_service->findByInterface(CensusAssistantModule::class)->first(); 75efd4768bSGreg Roach 7655749c76SGreg Roach if ($census_assistant instanceof CensusAssistantModule && $record instanceof Individual) { 77748dbe15SGreg Roach $ca_individuals = Validator::parsedBody($request)->array('ca_individuals')['xref'] ?? []; 7806707ebbSGreg Roach 7906707ebbSGreg Roach if ($ca_individuals !== []) { 80efd4768bSGreg Roach $gedcom = $census_assistant->updateCensusAssistant($request, $record, $fact_id, $gedcom, $keep_chan); 81e7b54987SGreg Roach 82e7b54987SGreg Roach // Don't copy the AGE/OCCU fields to other individuals 83e7b54987SGreg Roach $gedcom2 = preg_replace('/\n2 (?:AGE|OCCU) .*/', '', $gedcom); 84e7b54987SGreg Roach 8506707ebbSGreg Roach foreach ($ca_individuals as $pid) { 8655749c76SGreg Roach if ($pid !== $xref) { 87efd4768bSGreg Roach $individual = Registry::individualFactory()->make($pid, $tree); 88efd4768bSGreg Roach if ($individual instanceof Individual && $individual->canEdit()) { 89e7b54987SGreg Roach $individual->updateFact('', $gedcom2, !$keep_chan); 9055749c76SGreg Roach } 9155749c76SGreg Roach } 9255749c76SGreg Roach } 9355749c76SGreg Roach } 94efd4768bSGreg Roach } 95efd4768bSGreg Roach 96efd4768bSGreg Roach if ($fact_id === 'new') { 97efd4768bSGreg Roach // Add a new fact 98efd4768bSGreg Roach $record->updateFact('', $gedcom, !$keep_chan); 99efd4768bSGreg Roach } else { 100efd4768bSGreg Roach // Update (only the first copy of) an existing fact 101efd4768bSGreg Roach foreach ($record->facts([], false, null, true) as $fact) { 102efd4768bSGreg Roach if ($fact->id() === $fact_id && $fact->canEdit()) { 103efd4768bSGreg Roach $record->updateFact($fact_id, $gedcom, !$keep_chan); 104efd4768bSGreg Roach break; 105efd4768bSGreg Roach } 106efd4768bSGreg Roach } 107efd4768bSGreg Roach } 10855749c76SGreg Roach 109f507cef9SGreg Roach $url = Validator::parsedBody($request)->isLocalUrl()->string('url', $record->url()); 110551ad4afSGreg Roach 111551ad4afSGreg Roach return redirect($url); 11255749c76SGreg Roach } 11355749c76SGreg Roach} 114