155749c76SGreg Roach<?php 255749c76SGreg Roach 355749c76SGreg Roach/** 455749c76SGreg Roach * webtrees: online genealogy 589f7189bSGreg Roach * Copyright (C) 2021 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; 2855749c76SGreg Roachuse Fisharebest\Webtrees\Tree; 2955749c76SGreg Roachuse Psr\Http\Message\ResponseInterface; 3055749c76SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 3155749c76SGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 3255749c76SGreg Roach 3355749c76SGreg Roachuse function assert; 3455749c76SGreg Roachuse function explode; 3555749c76SGreg Roachuse function is_string; 3655749c76SGreg Roachuse function redirect; 3755749c76SGreg Roach 3855749c76SGreg Roach/** 3955749c76SGreg Roach * Save an updated GEDCOM fact. 4055749c76SGreg Roach */ 4155749c76SGreg Roachclass EditFactAction implements RequestHandlerInterface 4255749c76SGreg Roach{ 43c4943cffSGreg Roach private GedcomEditService $gedcom_edit_service; 4455749c76SGreg Roach 45c4943cffSGreg Roach private ModuleService $module_service; 4655749c76SGreg Roach 4755749c76SGreg Roach /** 4849a2c9dcSGreg Roach * EditFactAction constructor. 4955749c76SGreg Roach * 5055749c76SGreg Roach * @param GedcomEditService $gedcom_edit_service 5155749c76SGreg Roach * @param ModuleService $module_service 5255749c76SGreg Roach */ 5355749c76SGreg Roach public function __construct(GedcomEditService $gedcom_edit_service, ModuleService $module_service) 5455749c76SGreg Roach { 5555749c76SGreg Roach $this->gedcom_edit_service = $gedcom_edit_service; 5655749c76SGreg Roach $this->module_service = $module_service; 5755749c76SGreg Roach } 5855749c76SGreg Roach 5955749c76SGreg Roach /** 6055749c76SGreg Roach * @param ServerRequestInterface $request 6155749c76SGreg Roach * 6255749c76SGreg Roach * @return ResponseInterface 6355749c76SGreg Roach */ 6455749c76SGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 6555749c76SGreg Roach { 6655749c76SGreg Roach $tree = $request->getAttribute('tree'); 6755749c76SGreg Roach assert($tree instanceof Tree); 6855749c76SGreg Roach 6955749c76SGreg Roach $xref = $request->getAttribute('xref'); 7055749c76SGreg Roach assert(is_string($xref)); 7155749c76SGreg Roach 7249a2c9dcSGreg Roach $fact_id = $request->getAttribute('fact_id') ?? ''; 7349a2c9dcSGreg Roach assert(is_string($fact_id)); 7455749c76SGreg Roach 756b9cb339SGreg Roach $record = Registry::gedcomRecordFactory()->make($xref, $tree); 7655749c76SGreg Roach $record = Auth::checkRecordAccess($record, true); 7755749c76SGreg Roach 7849a2c9dcSGreg Roach $params = (array) $request->getParsedBody(); 7955749c76SGreg Roach $keep_chan = (bool) ($params['keep_chan'] ?? false); 80efd4768bSGreg Roach $levels = $params['levels']; 81efd4768bSGreg Roach $tags = $params['tags']; 82efd4768bSGreg Roach $values = $params['values']; 8355749c76SGreg Roach 84efd4768bSGreg Roach $gedcom = $this->gedcom_edit_service->editLinesToGedcom($record::RECORD_TYPE, $levels, $tags, $values); 8555749c76SGreg Roach 8655749c76SGreg Roach $census_assistant = $this->module_service->findByInterface(CensusAssistantModule::class)->first(); 87efd4768bSGreg Roach 8855749c76SGreg Roach if ($census_assistant instanceof CensusAssistantModule && $record instanceof Individual) { 89*06707ebbSGreg Roach $ca_individuals = $params['ca_individuals']['xref'] ?? []; 90*06707ebbSGreg Roach 91*06707ebbSGreg Roach if ($ca_individuals !== []) { 92efd4768bSGreg Roach $gedcom = $census_assistant->updateCensusAssistant($request, $record, $fact_id, $gedcom, $keep_chan); 93*06707ebbSGreg Roach foreach ($ca_individuals as $pid) { 9455749c76SGreg Roach if ($pid !== $xref) { 95efd4768bSGreg Roach $individual = Registry::individualFactory()->make($pid, $tree); 96efd4768bSGreg Roach if ($individual instanceof Individual && $individual->canEdit()) { 97efd4768bSGreg Roach $individual->updateFact('', $gedcom, !$keep_chan); 9855749c76SGreg Roach } 9955749c76SGreg Roach } 10055749c76SGreg Roach } 10155749c76SGreg Roach } 102efd4768bSGreg Roach } 103efd4768bSGreg Roach 104efd4768bSGreg Roach if ($fact_id === 'new') { 105efd4768bSGreg Roach // Add a new fact 106efd4768bSGreg Roach $record->updateFact('', $gedcom, !$keep_chan); 107efd4768bSGreg Roach } else { 108efd4768bSGreg Roach // Update (only the first copy of) an existing fact 109efd4768bSGreg Roach foreach ($record->facts([], false, null, true) as $fact) { 110efd4768bSGreg Roach if ($fact->id() === $fact_id && $fact->canEdit()) { 111efd4768bSGreg Roach $record->updateFact($fact_id, $gedcom, !$keep_chan); 112efd4768bSGreg Roach break; 113efd4768bSGreg Roach } 114efd4768bSGreg Roach } 115efd4768bSGreg Roach } 11655749c76SGreg Roach 1179db6d3cbSGreg Roach return redirect($params['url'] ?? $record->url()); 11855749c76SGreg Roach } 11955749c76SGreg Roach} 120