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