xref: /webtrees/app/Http/RequestHandlers/EditFactAction.php (revision 3d9e70a53ebdea3db408ada1769711840cc7d14c)
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 Psr\Http\Message\ResponseInterface;
30use Psr\Http\Message\ServerRequestInterface;
31use Psr\Http\Server\RequestHandlerInterface;
32
33use function assert;
34use function explode;
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            $gedcom = $census_assistant->updateCensusAssistant($request, $record, $fact_id, $gedcom, $keep_chan);
90            $pid_array = $params['pid_array'] ?? '';
91            if ($pid_array !== '') {
92                foreach (explode(',', $pid_array) as $pid) {
93                    if ($pid !== $xref) {
94                        $individual = Registry::individualFactory()->make($pid, $tree);
95                        if ($individual instanceof Individual && $individual->canEdit()) {
96                            $individual->updateFact('', $gedcom, !$keep_chan);
97                        }
98                    }
99                }
100            }
101        }
102
103        if ($fact_id === 'new') {
104            // Add a new fact
105            $record->updateFact('', $gedcom, !$keep_chan);
106        } else {
107            // Update (only the first copy of) an existing fact
108            foreach ($record->facts([], false, null, true) as $fact) {
109                if ($fact->id() === $fact_id && $fact->canEdit()) {
110                    $record->updateFact($fact_id, $gedcom, !$keep_chan);
111                    break;
112                }
113            }
114        }
115
116        return redirect($params['url'] ?? $record->url());
117    }
118}
119