xref: /webtrees/app/Http/RequestHandlers/EditFactAction.php (revision f507cef925e69f3dc0d47102f965899785571595)
155749c76SGreg Roach<?php
255749c76SGreg Roach
355749c76SGreg Roach/**
455749c76SGreg Roach * webtrees: online genealogy
55bfc6897SGreg Roach * Copyright (C) 2022 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 assert;
3455749c76SGreg Roachuse function is_string;
3555749c76SGreg Roachuse function redirect;
3655749c76SGreg Roach
3755749c76SGreg Roach/**
3855749c76SGreg Roach * Save an updated GEDCOM fact.
3955749c76SGreg Roach */
4055749c76SGreg Roachclass EditFactAction implements RequestHandlerInterface
4155749c76SGreg Roach{
42c4943cffSGreg Roach    private GedcomEditService $gedcom_edit_service;
4355749c76SGreg Roach
44c4943cffSGreg Roach    private ModuleService $module_service;
4555749c76SGreg Roach
4655749c76SGreg Roach    /**
4749a2c9dcSGreg Roach     * EditFactAction constructor.
4855749c76SGreg Roach     *
4955749c76SGreg Roach     * @param GedcomEditService $gedcom_edit_service
5055749c76SGreg Roach     * @param ModuleService     $module_service
5155749c76SGreg Roach     */
5255749c76SGreg Roach    public function __construct(GedcomEditService $gedcom_edit_service, ModuleService $module_service)
5355749c76SGreg Roach    {
5455749c76SGreg Roach        $this->gedcom_edit_service = $gedcom_edit_service;
5555749c76SGreg Roach        $this->module_service      = $module_service;
5655749c76SGreg Roach    }
5755749c76SGreg Roach
5855749c76SGreg Roach    /**
5955749c76SGreg Roach     * @param ServerRequestInterface $request
6055749c76SGreg Roach     *
6155749c76SGreg Roach     * @return ResponseInterface
6255749c76SGreg Roach     */
6355749c76SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
6455749c76SGreg Roach    {
65b55cbc6bSGreg Roach        $tree    = Validator::attributes($request)->tree();
66b55cbc6bSGreg Roach        $xref    = Validator::attributes($request)->isXref()->string('xref');
67b55cbc6bSGreg Roach        $fact_id = Validator::attributes($request)->string('fact_id');
6855749c76SGreg Roach
696b9cb339SGreg Roach        $record = Registry::gedcomRecordFactory()->make($xref, $tree);
7055749c76SGreg Roach        $record = Auth::checkRecordAccess($record, true);
7155749c76SGreg Roach
7249a2c9dcSGreg Roach        $params    = (array) $request->getParsedBody();
7355749c76SGreg Roach        $keep_chan = (bool) ($params['keep_chan'] ?? false);
74efd4768bSGreg Roach        $levels    = $params['levels'];
75efd4768bSGreg Roach        $tags      = $params['tags'];
76efd4768bSGreg Roach        $values    = $params['values'];
7755749c76SGreg Roach
78efd4768bSGreg Roach        $gedcom = $this->gedcom_edit_service->editLinesToGedcom($record::RECORD_TYPE, $levels, $tags, $values);
7955749c76SGreg Roach
8055749c76SGreg Roach        $census_assistant = $this->module_service->findByInterface(CensusAssistantModule::class)->first();
81efd4768bSGreg Roach
8255749c76SGreg Roach        if ($census_assistant instanceof CensusAssistantModule && $record instanceof Individual) {
8306707ebbSGreg Roach            $ca_individuals = $params['ca_individuals']['xref'] ?? [];
8406707ebbSGreg Roach
8506707ebbSGreg Roach            if ($ca_individuals !== []) {
86efd4768bSGreg Roach                $gedcom = $census_assistant->updateCensusAssistant($request, $record, $fact_id, $gedcom, $keep_chan);
87e7b54987SGreg Roach
88e7b54987SGreg Roach                // Don't copy the AGE/OCCU fields to other individuals
89e7b54987SGreg Roach                $gedcom2 = preg_replace('/\n2 (?:AGE|OCCU) .*/', '', $gedcom);
90e7b54987SGreg Roach
9106707ebbSGreg Roach                foreach ($ca_individuals as $pid) {
9255749c76SGreg Roach                    if ($pid !== $xref) {
93efd4768bSGreg Roach                        $individual = Registry::individualFactory()->make($pid, $tree);
94efd4768bSGreg Roach                        if ($individual instanceof Individual && $individual->canEdit()) {
95e7b54987SGreg Roach                            $individual->updateFact('', $gedcom2, !$keep_chan);
9655749c76SGreg Roach                        }
9755749c76SGreg Roach                    }
9855749c76SGreg Roach                }
9955749c76SGreg Roach            }
100efd4768bSGreg Roach        }
101efd4768bSGreg Roach
102efd4768bSGreg Roach        if ($fact_id === 'new') {
103efd4768bSGreg Roach            // Add a new fact
104efd4768bSGreg Roach            $record->updateFact('', $gedcom, !$keep_chan);
105efd4768bSGreg Roach        } else {
106efd4768bSGreg Roach            // Update (only the first copy of) an existing fact
107efd4768bSGreg Roach            foreach ($record->facts([], false, null, true) as $fact) {
108efd4768bSGreg Roach                if ($fact->id() === $fact_id && $fact->canEdit()) {
109efd4768bSGreg Roach                    $record->updateFact($fact_id, $gedcom, !$keep_chan);
110efd4768bSGreg Roach                    break;
111efd4768bSGreg Roach                }
112efd4768bSGreg Roach            }
113efd4768bSGreg Roach        }
11455749c76SGreg Roach
115*f507cef9SGreg Roach        $url = Validator::parsedBody($request)->isLocalUrl()->string('url', $record->url());
116551ad4afSGreg Roach
117551ad4afSGreg Roach        return redirect($url);
11855749c76SGreg Roach    }
11955749c76SGreg Roach}
120