xref: /webtrees/app/Http/RequestHandlers/EditFactAction.php (revision 273a564e3de4d5154e894fa56a60fe99383cf8b2)
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    /** @var GedcomEditService */
44    private $gedcom_edit_service;
45
46    /** @var ModuleService */
47    private $module_service;
48
49    /**
50     * EditFactAction constructor.
51     *
52     * @param GedcomEditService $gedcom_edit_service
53     * @param ModuleService     $module_service
54     */
55    public function __construct(GedcomEditService $gedcom_edit_service, ModuleService $module_service)
56    {
57        $this->gedcom_edit_service = $gedcom_edit_service;
58        $this->module_service      = $module_service;
59    }
60
61    /**
62     * @param ServerRequestInterface $request
63     *
64     * @return ResponseInterface
65     */
66    public function handle(ServerRequestInterface $request): ResponseInterface
67    {
68        $tree = $request->getAttribute('tree');
69        assert($tree instanceof Tree);
70
71        $xref = $request->getAttribute('xref');
72        assert(is_string($xref));
73
74        $fact_id = $request->getAttribute('fact_id') ?? '';
75        assert(is_string($fact_id));
76
77        $record = Registry::gedcomRecordFactory()->make($xref, $tree);
78        $record = Auth::checkRecordAccess($record, true);
79
80        $params    = (array) $request->getParsedBody();
81        $keep_chan = (bool) ($params['keep_chan'] ?? false);
82        $levels    = $params['levels'];
83        $tags      = $params['tags'];
84        $values    = $params['values'];
85
86        $gedcom = $this->gedcom_edit_service->editLinesToGedcom($record::RECORD_TYPE, $levels, $tags, $values);
87
88        $census_assistant = $this->module_service->findByInterface(CensusAssistantModule::class)->first();
89
90        if ($census_assistant instanceof CensusAssistantModule && $record instanceof Individual) {
91            $gedcom = $census_assistant->updateCensusAssistant($request, $record, $fact_id, $gedcom, $keep_chan);
92            $pid_array = $params['pid_array'] ?? '';
93            if ($pid_array !== '') {
94                foreach (explode(',', $pid_array) 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        return redirect($params['url'] ?? $record->url());
119    }
120}
121