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