xref: /webtrees/app/Http/RequestHandlers/EditFactAction.php (revision 1082dae98fac1db8249bca55d2b3fdce9ae0fac3)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 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\Tree;
29use Fisharebest\Webtrees\Validator;
30use Psr\Http\Message\ResponseInterface;
31use Psr\Http\Message\ServerRequestInterface;
32use Psr\Http\Server\RequestHandlerInterface;
33
34use function assert;
35use function is_string;
36use function redirect;
37
38/**
39 * Save an updated GEDCOM fact.
40 */
41class EditFactAction implements RequestHandlerInterface
42{
43    private GedcomEditService $gedcom_edit_service;
44
45    private ModuleService $module_service;
46
47    /**
48     * EditFactAction constructor.
49     *
50     * @param GedcomEditService $gedcom_edit_service
51     * @param ModuleService     $module_service
52     */
53    public function __construct(GedcomEditService $gedcom_edit_service, ModuleService $module_service)
54    {
55        $this->gedcom_edit_service = $gedcom_edit_service;
56        $this->module_service      = $module_service;
57    }
58
59    /**
60     * @param ServerRequestInterface $request
61     *
62     * @return ResponseInterface
63     */
64    public function handle(ServerRequestInterface $request): ResponseInterface
65    {
66        $tree = $request->getAttribute('tree');
67        assert($tree instanceof Tree);
68
69        $xref = $request->getAttribute('xref');
70        assert(is_string($xref));
71
72        $fact_id = $request->getAttribute('fact_id') ?? '';
73        assert(is_string($fact_id));
74
75        $record = Registry::gedcomRecordFactory()->make($xref, $tree);
76        $record = Auth::checkRecordAccess($record, true);
77
78        $params    = (array) $request->getParsedBody();
79        $keep_chan = (bool) ($params['keep_chan'] ?? false);
80        $levels    = $params['levels'];
81        $tags      = $params['tags'];
82        $values    = $params['values'];
83
84        $gedcom = $this->gedcom_edit_service->editLinesToGedcom($record::RECORD_TYPE, $levels, $tags, $values);
85
86        $census_assistant = $this->module_service->findByInterface(CensusAssistantModule::class)->first();
87
88        if ($census_assistant instanceof CensusAssistantModule && $record instanceof Individual) {
89            $ca_individuals = $params['ca_individuals']['xref'] ?? [];
90
91            if ($ca_individuals !== []) {
92                $gedcom = $census_assistant->updateCensusAssistant($request, $record, $fact_id, $gedcom, $keep_chan);
93
94                // Don't copy the AGE/OCCU fields to other individuals
95                $gedcom2 = preg_replace('/\n2 (?:AGE|OCCU) .*/', '', $gedcom);
96
97                foreach ($ca_individuals as $pid) {
98                    if ($pid !== $xref) {
99                        $individual = Registry::individualFactory()->make($pid, $tree);
100                        if ($individual instanceof Individual && $individual->canEdit()) {
101                            $individual->updateFact('', $gedcom2, !$keep_chan);
102                        }
103                    }
104                }
105            }
106        }
107
108        if ($fact_id === 'new') {
109            // Add a new fact
110            $record->updateFact('', $gedcom, !$keep_chan);
111        } else {
112            // Update (only the first copy of) an existing fact
113            foreach ($record->facts([], false, null, true) as $fact) {
114                if ($fact->id() === $fact_id && $fact->canEdit()) {
115                    $record->updateFact($fact_id, $gedcom, !$keep_chan);
116                    break;
117                }
118            }
119        }
120
121        $base_url = $request->getAttribute('base_url');
122        $url      = Validator::parsedBody($request)->isLocalUrl($base_url)->string('url') ?? $record->url();
123
124        return redirect($url);
125    }
126}
127