xref: /webtrees/app/Http/RequestHandlers/EditFactAction.php (revision b3679361640bf9d3bb5424fb15815c51cf0c1513)
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                foreach ($ca_individuals as $pid) {
94                    if ($pid !== $xref) {
95                        $individual = Registry::individualFactory()->make($pid, $tree);
96                        if ($individual instanceof Individual && $individual->canEdit()) {
97                            $individual->updateFact('', $gedcom, !$keep_chan);
98                        }
99                    }
100                }
101            }
102        }
103
104        if ($fact_id === 'new') {
105            // Add a new fact
106            $record->updateFact('', $gedcom, !$keep_chan);
107        } else {
108            // Update (only the first copy of) an existing fact
109            foreach ($record->facts([], false, null, true) as $fact) {
110                if ($fact->id() === $fact_id && $fact->canEdit()) {
111                    $record->updateFact($fact_id, $gedcom, !$keep_chan);
112                    break;
113                }
114            }
115        }
116
117        $base_url = $request->getAttribute('base_url');
118        $url      = Validator::parsedBody($request)->isLocalUrl($base_url)->string('url') ?? $record->url();
119
120        return redirect($url);
121    }
122}
123