Passed
Push — master ( 69c894...433228 )
by Greg
05:23
created

EditFamilyController::addSpouse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 27
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 21
nc 2
nop 1
dl 0
loc 27
rs 9.584
c 0
b 0
f 0
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\Controllers;
21
22
use Fisharebest\Webtrees\Auth;
23
use Fisharebest\Webtrees\Date;
24
use Fisharebest\Webtrees\Fact;
25
use Fisharebest\Webtrees\Factory;
26
use Fisharebest\Webtrees\GedcomCode\GedcomCodePedi;
27
use Fisharebest\Webtrees\I18N;
28
use Fisharebest\Webtrees\Individual;
29
use Fisharebest\Webtrees\Tree;
30
use Psr\Http\Message\ResponseInterface;
31
use Psr\Http\Message\ServerRequestInterface;
32
33
use function assert;
34
35
/**
36
 * Controller for edit forms and responses.
37
 */
38
class EditFamilyController extends AbstractEditController
39
{
40
    /**
41
     * @param ServerRequestInterface $request
42
     *
43
     * @return ResponseInterface
44
     */
45
    public function addChild(ServerRequestInterface $request): ResponseInterface
46
    {
47
        $tree = $request->getAttribute('tree');
48
        assert($tree instanceof Tree);
49
50
        $xref = $request->getQueryParams()['xref'];
51
52
        $family = Factory::family()->make($xref, $tree);
53
        $family = Auth::checkFamilyAccess($family, true);
54
55
        $gender = $request->getQueryParams()['gender'];
56
57
        $subtitles = [
58
            'M' => I18N::translate('Add a son'),
59
            'F' => I18N::translate('Add a daughter'),
60
            'U' => I18N::translate('Add a child'),
61
        ];
62
63
        $subtitle = $subtitles[$gender] ?? $subtitles['U'];
64
65
        $title = $family->fullName() . ' - ' . $subtitle;
66
67
        return $this->viewResponse('edit/new-individual', [
68
            'next_action' => 'add-child-to-family-action',
69
            'tree'       => $tree,
70
            'title'      => $title,
71
            'individual' => null,
72
            'family'     => $family,
73
            'name_fact'  => null,
74
            'famtag'     => 'CHIL',
75
            'gender'     => $gender,
76
        ]);
77
    }
78
79
    /**
80
     * @param ServerRequestInterface $request
81
     *
82
     * @return ResponseInterface
83
     */
84
    public function addChildAction(ServerRequestInterface $request): ResponseInterface
85
    {
86
        $tree = $request->getAttribute('tree');
87
        assert($tree instanceof Tree);
88
89
        $xref = $request->getQueryParams()['xref'];
90
91
        $family = Factory::family()->make($xref, $tree);
92
        $family = Auth::checkFamilyAccess($family, true);
93
94
        $params = (array) $request->getParsedBody();
95
96
        $PEDI      = $params['PEDI'];
97
        $keep_chan = (bool) ($params['keep_chan'] ?? false);
98
99
        $this->glevels = $params['glevels'] ?? [];
100
        $this->tag     = $params['tag'] ?? [];
101
        $this->text    = $params['text'] ?? [];
102
        $this->islink  = $params['islink'] ?? [];
103
104
        $this->splitSource();
105
        $gedrec = '0 @@ INDI';
106
        $gedrec .= $this->addNewName($request, $tree);
107
        $gedrec .= $this->addNewSex($request);
108
        if (preg_match_all('/([A-Z0-9_]+)/', $tree->getPreference('QUICK_REQUIRED_FACTS'), $matches)) {
109
            foreach ($matches[1] as $match) {
110
                $gedrec .= $this->addNewFact($request, $tree, $match);
111
            }
112
        }
113
        $gedrec .= "\n" . GedcomCodePedi::createNewFamcPedi($PEDI, $xref);
114
        if ($params['SOUR_INDI'] ?? false) {
115
            $gedrec = $this->handleUpdates($gedrec);
116
        } else {
117
            $gedrec = $this->updateRest($gedrec);
118
        }
119
120
        // Create the new child
121
        $new_child = $tree->createIndividual($gedrec);
122
123
        // Insert new child at the right place
124
        $done = false;
125
        foreach ($family->facts(['CHIL']) as $fact) {
126
            $old_child = $fact->target();
127
            if ($old_child instanceof Individual && Date::compare($new_child->getEstimatedBirthDate(), $old_child->getEstimatedBirthDate()) < 0) {
128
                // Insert before this child
129
                $family->updateFact($fact->id(), '1 CHIL @' . $new_child->xref() . "@\n" . $fact->gedcom(), !$keep_chan);
130
                $done = true;
131
                break;
132
            }
133
        }
134
        if (!$done) {
135
            // Append child at end
136
            $family->createFact('1 CHIL @' . $new_child->xref() . '@', !$keep_chan);
137
        }
138
139
        if (($params['goto'] ?? '') === 'new') {
140
            return redirect($new_child->url());
141
        }
142
143
        return redirect($family->url());
144
    }
145
146
    /**
147
     * @param ServerRequestInterface $request
148
     *
149
     * @return ResponseInterface
150
     */
151
    public function addSpouse(ServerRequestInterface $request): ResponseInterface
152
    {
153
        $tree = $request->getAttribute('tree');
154
        assert($tree instanceof Tree);
155
156
        $xref   = $request->getQueryParams()['xref'];
157
        $famtag = $request->getQueryParams()['famtag'];
158
        $family = Factory::family()->make($xref, $tree);
159
        $family = Auth::checkFamilyAccess($family, true);
160
161
        if ($famtag === 'WIFE') {
162
            $title  = I18N::translate('Add a wife');
163
            $gender = 'F';
164
        } else {
165
            $title  = I18N::translate('Add a husband');
166
            $gender = 'M';
167
        }
168
169
        return $this->viewResponse('edit/new-individual', [
170
            'next_action' => 'add-spouse-to-family-action',
171
            'tree'       => $tree,
172
            'title'      => $title,
173
            'individual' => null,
174
            'family'     => $family,
175
            'name_fact'  => null,
176
            'famtag'     => $famtag,
177
            'gender'     => $gender,
178
        ]);
179
    }
180
181
    /**
182
     * @param ServerRequestInterface $request
183
     *
184
     * @return ResponseInterface
185
     */
186
    public function addSpouseAction(ServerRequestInterface $request): ResponseInterface
187
    {
188
        $tree = $request->getAttribute('tree');
189
        assert($tree instanceof Tree);
190
191
        $xref   = $request->getQueryParams()['xref'];
192
        $family = Factory::family()->make($xref, $tree);
193
        $family = Auth::checkFamilyAccess($family, true);
194
195
        $params = (array) $request->getParsedBody();
196
197
        $this->glevels = $params['glevels'] ?? [];
198
        $this->tag     = $params['tag'] ?? [];
199
        $this->text    = $params['text'] ?? [];
200
        $this->islink  = $params['islink'] ?? [];
201
202
        // Create the new spouse
203
        $this->splitSource(); // separate SOUR record from the rest
204
205
        $gedrec = '0 @@ INDI';
206
        $gedrec .= $this->addNewName($request, $tree);
207
        $gedrec .= $this->addNewSex($request);
208
        if (preg_match_all('/([A-Z0-9_]+)/', $tree->getPreference('QUICK_REQUIRED_FACTS'), $matches)) {
209
            foreach ($matches[1] as $match) {
210
                $gedrec .= $this->addNewFact($request, $tree, $match);
211
            }
212
        }
213
214
        if ($params['SOUR_INDI'] ?? false) {
215
            $gedrec = $this->handleUpdates($gedrec);
216
        } else {
217
            $gedrec = $this->updateRest($gedrec);
218
        }
219
        $gedrec .= "\n1 FAMS @" . $family->xref() . '@';
220
        $spouse = $tree->createIndividual($gedrec);
221
222
        // Update the existing family - add marriage, etc
223
        if ($family->facts(['HUSB'])->first() instanceof Fact) {
224
            $family->createFact('1 WIFE @' . $spouse->xref() . '@', true);
225
        } else {
226
            $family->createFact('1 HUSB @' . $spouse->xref() . '@', true);
227
        }
228
        $famrec = '';
229
        if (preg_match_all('/([A-Z0-9_]+)/', $tree->getPreference('QUICK_REQUIRED_FAMFACTS'), $matches)) {
230
            foreach ($matches[1] as $match) {
231
                $famrec .= $this->addNewFact($request, $tree, $match);
232
            }
233
        }
234
        if ($params['SOUR_FAM'] ?? false) {
235
            $famrec = $this->handleUpdates($famrec);
236
        } else {
237
            $famrec = $this->updateRest($famrec);
238
        }
239
        $family->createFact(trim($famrec), true); // trim leading \n
240
241
        if (($params['goto'] ?? '') === 'new') {
242
            return redirect($spouse->url());
243
        }
244
245
        return redirect($family->url());
246
    }
247
}
248