xref: /webtrees/app/Http/RequestHandlers/EditFactAction.php (revision ab2c91523aeb171fd76225844c6c3c0d2440dda3)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2022 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22use Fisharebest\Webtrees\Auth;
23use Fisharebest\Webtrees\Individual;
24use Fisharebest\Webtrees\Module\CensusAssistantModule;
25use Fisharebest\Webtrees\Registry;
26use Fisharebest\Webtrees\Services\GedcomEditService;
27use Fisharebest\Webtrees\Services\ModuleService;
28use Fisharebest\Webtrees\Validator;
29use Psr\Http\Message\ResponseInterface;
30use Psr\Http\Message\ServerRequestInterface;
31use Psr\Http\Server\RequestHandlerInterface;
32
33use function ltrim;
34use function redirect;
35
36/**
37 * Save an updated GEDCOM fact.
38 */
39class EditFactAction implements RequestHandlerInterface
40{
41    private GedcomEditService $gedcom_edit_service;
42
43    private ModuleService $module_service;
44
45    /**
46     * EditFactAction constructor.
47     *
48     * @param GedcomEditService $gedcom_edit_service
49     * @param ModuleService     $module_service
50     */
51    public function __construct(GedcomEditService $gedcom_edit_service, ModuleService $module_service)
52    {
53        $this->gedcom_edit_service = $gedcom_edit_service;
54        $this->module_service      = $module_service;
55    }
56
57    /**
58     * @param ServerRequestInterface $request
59     *
60     * @return ResponseInterface
61     */
62    public function handle(ServerRequestInterface $request): ResponseInterface
63    {
64        $tree    = Validator::attributes($request)->tree();
65        $xref    = Validator::attributes($request)->isXref()->string('xref');
66        $fact_id = Validator::attributes($request)->string('fact_id');
67
68        $record = Registry::gedcomRecordFactory()->make($xref, $tree);
69        $record = Auth::checkRecordAccess($record, true);
70
71        $params    = (array) $request->getParsedBody();
72        $keep_chan = Validator::parsedBody($request)->boolean('keep_chan', false);
73        $levels    = Validator::parsedBody($request)->array('levels');
74        $tags      = Validator::parsedBody($request)->array('tags');
75        $values    = Validator::parsedBody($request)->array('values');
76        $gedcom    = $this->gedcom_edit_service->editLinesToGedcom($record::RECORD_TYPE, $levels, $tags, $values, false);
77
78        $census_assistant = $this->module_service->findByInterface(CensusAssistantModule::class)->first();
79
80        if ($census_assistant instanceof CensusAssistantModule && $record instanceof Individual) {
81            $ca_individuals = $params['ca_individuals']['xref'] ?? [];
82
83            if ($ca_individuals !== []) {
84                $gedcom = $census_assistant->updateCensusAssistant($request, $record, $fact_id, $gedcom, $keep_chan);
85
86                // Don't copy the AGE/OCCU fields to other individuals
87                $gedcom2 = preg_replace('/\n2 (?:AGE|OCCU) .*/', '', $gedcom);
88
89                foreach ($ca_individuals as $pid) {
90                    if ($pid !== $xref) {
91                        $individual = Registry::individualFactory()->make($pid, $tree);
92                        if ($individual instanceof Individual && $individual->canEdit()) {
93                            $individual->updateFact('', $gedcom2, !$keep_chan);
94                        }
95                    }
96                }
97            }
98        }
99
100        if ($fact_id === 'new') {
101            // Add a new fact
102            $record->updateFact('', $gedcom, !$keep_chan);
103        } else {
104            // Update (only the first copy of) an existing fact
105            foreach ($record->facts([], false, null, true) as $fact) {
106                if ($fact->id() === $fact_id && $fact->canEdit()) {
107                    $record->updateFact($fact_id, $gedcom, !$keep_chan);
108                    break;
109                }
110            }
111        }
112
113        $url = Validator::parsedBody($request)->isLocalUrl()->string('url', $record->url());
114
115        return redirect($url);
116    }
117}
118