Completed
Push — develop ( 0d6c46...c341e4 )
by Greg
15:25 queued 09:07
created

EditFactAction   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 36
c 0
b 0
f 0
dl 0
loc 78
rs 10
wmc 13

2 Methods

Rating   Name   Duplication   Size   Complexity  
C handle() 0 53 12
A __construct() 0 4 1
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2020 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 <http://www.gnu.org/licenses/>.
16
 */
17
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22
use Fisharebest\Webtrees\Auth;
23
use Fisharebest\Webtrees\Individual;
24
use Fisharebest\Webtrees\Module\CensusAssistantModule;
25
use Fisharebest\Webtrees\Registry;
26
use Fisharebest\Webtrees\Services\GedcomEditService;
27
use Fisharebest\Webtrees\Services\ModuleService;
28
use Fisharebest\Webtrees\Tree;
29
use Psr\Http\Message\ResponseInterface;
30
use Psr\Http\Message\ServerRequestInterface;
31
use Psr\Http\Server\RequestHandlerInterface;
32
33
use function assert;
34
use function explode;
35
use function is_string;
36
use function redirect;
37
38
/**
39
 * Save an updated GEDCOM fact.
40
 */
41
class 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