Passed
Push — master ( fc1d53...291777 )
by Greg
05:01
created

EditGedcomRecordController::deleteFact()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 17
rs 9.9332
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2019 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\Controllers;
21
22
use Fisharebest\Webtrees\Auth;
23
use Fisharebest\Webtrees\GedcomRecord;
24
use Fisharebest\Webtrees\GedcomTag;
25
use Fisharebest\Webtrees\Individual;
26
use Fisharebest\Webtrees\Module\CensusAssistantModule;
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 Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
32
33
use function assert;
34
35
/**
36
 * Controller for edit forms and responses.
37
 */
38
class EditGedcomRecordController extends AbstractEditController
39
{
40
    /** @var ModuleService */
41
    private $module_service;
42
43
    /**
44
     * EditGedcomRecordController constructor.
45
     *
46
     * @param ModuleService $module_service
47
     */
48
    public function __construct(ModuleService $module_service)
49
    {
50
        $this->module_service = $module_service;
51
    }
52
53
    /**
54
     * @param ServerRequestInterface $request
55
     *
56
     * @return ResponseInterface
57
     */
58
    public function updateFact(ServerRequestInterface $request): ResponseInterface
59
    {
60
        $tree = $request->getAttribute('tree');
61
        assert($tree instanceof Tree);
62
63
        $xref    = $request->getAttribute('xref');
64
        $fact_id = $request->getParsedBody()['fact_id'] ?? '';
65
66
        $record = GedcomRecord::getInstance($xref, $tree);
67
        Auth::checkRecordAccess($record, true);
68
69
        $keep_chan = (bool) ($request->getParsedBody()['keep_chan'] ?? false);
70
71
        $this->glevels = $request->getParsedBody()['glevels'];
72
        $this->tag     = $request->getParsedBody()['tag'];
73
        $this->text    = $request->getParsedBody()['text'];
74
        $this->islink  = $request->getParsedBody()['islink'];
75
76
        // If the fact has a DATE or PLAC, then delete any value of Y
77
        if ($this->text[0] === 'Y') {
78
            foreach ($this->tag as $n => $value) {
79
                if ($this->glevels[$n] == 2 && ($value === 'DATE' || $value === 'PLAC') && $this->text[$n] !== '') {
80
                    $this->text[0] = '';
81
                    break;
82
                }
83
            }
84
        }
85
86
        $newged = '';
87
88
        $NAME = $request->getParsedBody()['NAME'] ?? '';
89
90
        if ($NAME !== '') {
91
            $newged     .= "\n1 NAME " . $NAME;
92
            $name_facts = [
93
                'TYPE',
94
                'NPFX',
95
                'GIVN',
96
                'NICK',
97
                'SPFX',
98
                'SURN',
99
                'NSFX',
100
            ];
101
            foreach ($name_facts as $name_fact) {
102
                $NAME_FACT = $request->getParsedBody()[$name_fact] ?? '';
103
                if ($NAME_FACT !== '') {
104
                    $newged .= "\n2 " . $name_fact . ' ' . $NAME_FACT;
105
                }
106
            }
107
        }
108
109
        $newged = $this->handleUpdates($newged);
110
111
        // Add new names after existing names
112
        if ($NAME !== '') {
113
            preg_match_all('/[_0-9A-Z]+/', $tree->getPreference('ADVANCED_NAME_FACTS'), $match);
114
            $name_facts = array_unique(array_merge(['_MARNM'], $match[0]));
115
            foreach ($name_facts as $name_fact) {
116
                $NAME_FACT = $request->getParsedBody()[$name_fact] ?? '';
117
                // Ignore advanced facts that duplicate standard facts.
118
                if ($NAME_FACT !== '' && !in_array($name_fact, ['TYPE', 'NPFX', 'GIVN', 'NICK', 'SPFX', 'SURN', 'NSFX'], true)) {
119
                    $newged .= "\n2 " . $name_fact . ' ' . $NAME_FACT;
120
                }
121
            }
122
        }
123
124
        $newged = trim($newged); // Remove leading newline
125
126
        $census_assistant = $this->module_service->findByInterface(CensusAssistantModule::class)->first();
127
        if ($census_assistant instanceof CensusAssistantModule && $record instanceof Individual) {
128
            $newged = $census_assistant->updateCensusAssistant($request, $record, $fact_id, $newged, $keep_chan);
129
        }
130
131
        $record->updateFact($fact_id, $newged, !$keep_chan);
132
133
        // For the GEDFact_assistant module
134
        $pid_array = $request->getParsedBody()['pid_array'] ?? '';
135
        if ($pid_array !== '') {
136
            foreach (explode(',', $pid_array) as $pid) {
137
                if ($pid !== $xref) {
138
                    $indi = Individual::getInstance($pid, $tree);
139
                    if ($indi && $indi->canEdit()) {
140
                        $indi->updateFact($fact_id, $newged, !$keep_chan);
141
                    }
142
                }
143
            }
144
        }
145
146
        return redirect($record->url());
147
    }
148
}
149