Passed
Push — master ( 9b5c95...5229ea )
by Greg
06:55
created

EditIndividualController::addParentAction()   B

Complexity

Conditions 6
Paths 16

Size

Total Lines 53
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 31
nc 16
nop 1
dl 0
loc 53
rs 8.8017
c 1
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
declare(strict_types=1);
18
19
namespace Fisharebest\Webtrees\Http\Controllers;
20
21
use Fisharebest\Webtrees\Auth;
22
use Fisharebest\Webtrees\Family;
23
use Fisharebest\Webtrees\GedcomCode\GedcomCodePedi;
24
use Fisharebest\Webtrees\I18N;
25
use Fisharebest\Webtrees\Individual;
26
use Fisharebest\Webtrees\Tree;
27
use InvalidArgumentException;
28
use Psr\Http\Message\ResponseInterface;
29
use Psr\Http\Message\ServerRequestInterface;
30
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
31
32
use function assert;
33
34
/**
35
 * Controller for edit forms and responses.
36
 */
37
class EditIndividualController extends AbstractEditController
38
{
39
   /**
40
     * Add a child to an existing individual (creating a one-parent family).
41
     *
42
     * @param ServerRequestInterface $request
43
     *
44
     * @return ResponseInterface
45
     */
46
    public function addChild(ServerRequestInterface $request): ResponseInterface
47
    {
48
        $tree       = $request->getAttribute('tree');
49
        $xref       = $request->getQueryParams()['xref'];
50
        $individual = Individual::getInstance($xref, $tree);
51
52
        Auth::checkIndividualAccess($individual, true);
53
54
        $title = $individual->fullName() . ' - ' . I18N::translate('Add a child to create a one-parent family');
55
56
        return $this->viewResponse('edit/new-individual', [
57
            'next_action' => 'add-child-to-individual-action',
58
            'tree'        => $tree,
59
            'title'       => $title,
60
            'individual'  => $individual,
61
            'family'      => null,
62
            'name_fact'   => null,
63
            'famtag'      => 'CHIL',
64
            'gender'      => 'U',
65
        ]);
66
    }
67
68
    /**
69
     * @param ServerRequestInterface $request
70
     *
71
     * @return ResponseInterface
72
     */
73
    public function addChildAction(ServerRequestInterface $request): ResponseInterface
74
    {
75
        $tree   = $request->getAttribute('tree');
76
        $xref   = $request->getParsedBody()['xref'];
77
78
        $individual = Individual::getInstance($xref, $tree);
79
80
        Auth::checkIndividualAccess($individual, true);
81
82
        $PEDI = $request->getParsedBody()['PEDI'];
83
84
        $this->glevels = $request->getParsedBody()['glevels'] ?? [];
85
        $this->tag     = $request->getParsedBody()['tag'] ?? [];
86
        $this->text    = $request->getParsedBody()['text'] ?? [];
87
        $this->islink  = $request->getParsedBody()['islink'] ?? [];
88
89
        // Create a family
90
        if ($individual->sex() === 'F') {
91
            $gedcom = "0 @@ FAM\n1 WIFE @" . $individual->xref() . '@';
92
        } else {
93
            $gedcom = "0 @@ FAM\n1 HUSB @" . $individual->xref() . '@';
94
        }
95
        $family = $tree->createFamily($gedcom);
96
97
        // Link the parent to the family
98
        $individual->createFact('1 FAMS @' . $family->xref() . '@', true);
99
100
        // Create a child
101
        $this->splitSource(); // separate SOUR record from the rest
102
103
        $gedcom = '0 @@ INDI';
104
        $gedcom .= $this->addNewName($request, $tree);
105
        $gedcom .= $this->addNewSex($request);
106
        $gedcom .= "\n" . GedcomCodePedi::createNewFamcPedi($PEDI, $family->xref());
107
        if (preg_match_all('/([A-Z0-9_]+)/', $tree->getPreference('QUICK_REQUIRED_FACTS'), $matches)) {
108
            foreach ($matches[1] as $match) {
109
                $gedcom .= $this->addNewFact($request, $tree, $match);
110
            }
111
        }
112
        if ($request->getParsedBody()['SOUR_INDI'] ?? false) {
113
            $gedcom = $this->handleUpdates($gedcom);
114
        } else {
115
            $gedcom = $this->updateRest($gedcom);
116
        }
117
118
        $child = $tree->createIndividual($gedcom);
119
120
        // Link the family to the child
121
        $family->createFact('1 CHIL @' . $child->xref() . '@', true);
122
123
        if (($request->getParsedBody()['goto'] ?? '') === 'new') {
124
            return redirect($child->url());
125
        }
126
127
        return redirect($individual->url());
128
    }
129
130
    /**
131
     * Add a parent to an existing individual (creating a one-parent family).
132
     *
133
     * @param ServerRequestInterface $request
134
     *
135
     * @return ResponseInterface
136
     */
137
    public function addParent(ServerRequestInterface $request): ResponseInterface
138
    {
139
        $tree   = $request->getAttribute('tree');
140
        $xref   = $request->getQueryParams()['xref'];
141
        $gender = $request->getQueryParams()['gender'];
142
143
        $individual = Individual::getInstance($xref, $tree);
144
145
        Auth::checkIndividualAccess($individual, true);
146
147
        if ($gender === 'F') {
148
            $title  = $individual->fullName() . ' - ' . I18N::translate('Add a mother');
149
            $famtag = 'WIFE';
150
        } else {
151
            $title  = $individual->fullName() . ' - ' . I18N::translate('Add a father');
152
            $famtag = 'HUSB';
153
        }
154
155
        return $this->viewResponse('edit/new-individual', [
156
            'next_action' => 'add-parent-to-individual-action',
157
            'tree'        => $tree,
158
            'title'       => $title,
159
            'individual'  => $individual,
160
            'family'      => null,
161
            'name_fact'   => null,
162
            'famtag'      => $famtag,
163
            'gender'      => $gender,
164
        ]);
165
    }
166
167
    /**
168
     * @param ServerRequestInterface $request
169
     *
170
     * @return ResponseInterface
171
     */
172
    public function addParentAction(ServerRequestInterface $request): ResponseInterface
173
    {
174
        $tree   = $request->getAttribute('tree');
175
        $xref   = $request->getParsedBody()['xref'];
176
177
        $individual = Individual::getInstance($xref, $tree);
178
179
        Auth::checkIndividualAccess($individual, true);
180
181
        $this->glevels = $request->getParsedBody()['glevels'] ?? [];
182
        $this->tag     = $request->getParsedBody()['tag'] ?? [];
183
        $this->text    = $request->getParsedBody()['text'] ?? [];
184
        $this->islink  = $request->getParsedBody()['islink'] ?? [];
185
186
        // Create a new family
187
        $gedcom = "0 @@ FAM\n1 CHIL @" . $individual->xref() . '@';
188
        $family = $tree->createFamily($gedcom);
189
190
        // Link the child to the family
191
        $individual->createFact('1 FAMC @' . $family->xref() . '@', true);
192
193
        // Create a child
194
        $this->splitSource(); // separate SOUR record from the rest
195
196
        $gedcom = '0 @@ INDI';
197
        $gedcom .= $this->addNewName($request, $tree);
198
        $gedcom .= $this->addNewSex($request);
199
        if (preg_match_all('/([A-Z0-9_]+)/', $tree->getPreference('QUICK_REQUIRED_FACTS'), $matches)) {
200
            foreach ($matches[1] as $match) {
201
                $gedcom .= $this->addNewFact($request, $tree, $match);
202
            }
203
        }
204
        if ($request->getParsedBody()['SOUR_INDI'] ?? false) {
205
            $gedcom = $this->handleUpdates($gedcom);
206
        } else {
207
            $gedcom = $this->updateRest($gedcom);
208
        }
209
        $gedcom .= "\n1 FAMS @" . $family->xref() . '@';
210
211
        $parent = $tree->createIndividual($gedcom);
212
213
        // Link the family to the child
214
        if ($parent->sex() === 'F') {
215
            $family->createFact('1 WIFE @' . $parent->xref() . '@', true);
216
        } else {
217
            $family->createFact('1 HUSB @' . $parent->xref() . '@', true);
218
        }
219
220
        if (($request->getParsedBody()['goto'] ?? '') === 'new') {
221
            return redirect($parent->url());
222
        }
223
224
        return redirect($individual->url());
225
    }
226
227
    /**
228
     * Add a spouse to an existing individual (creating a new family).
229
     *
230
     * @param ServerRequestInterface $request
231
     *
232
     * @return ResponseInterface
233
     */
234
    public function addSpouse(ServerRequestInterface $request): ResponseInterface
235
    {
236
        $tree = $request->getAttribute('tree');
237
        assert($tree instanceof Tree, new InvalidArgumentException());
238
239
        $xref = $request->getQueryParams()['xref'];
240
241
        $individual = Individual::getInstance($xref, $tree);
242
243
        Auth::checkIndividualAccess($individual, true);
244
245
        if ($individual->sex() === 'F') {
246
            $title  = $individual->fullName() . ' - ' . I18N::translate('Add a husband');
247
            $famtag = 'HUSB';
248
            $gender = 'M';
249
        } else {
250
            $title  = $individual->fullName() . ' - ' . I18N::translate('Add a wife');
251
            $famtag = 'WIFE';
252
            $gender = 'F';
253
        }
254
255
        return $this->viewResponse('edit/new-individual', [
256
            'next_action' => 'add-spouse-to-individual-action',
257
            'tree'        => $tree,
258
            'title'       => $title,
259
            'individual'  => $individual,
260
            'family'      => null,
261
            'name_fact'   => null,
262
            'famtag'      => $famtag,
263
            'gender'      => $gender,
264
        ]);
265
    }
266
267
    /**
268
     * @param ServerRequestInterface $request
269
     *
270
     * @return ResponseInterface
271
     */
272
    public function addSpouseAction(ServerRequestInterface $request): ResponseInterface
273
    {
274
        $tree   = $request->getAttribute('tree');
275
        $xref   = $request->getParsedBody()['xref'];
276
277
        $individual = Individual::getInstance($xref, $tree);
278
279
        Auth::checkIndividualAccess($individual, true);
280
281
        $sex = $request->getParsedBody()['SEX'];
282
283
        $this->glevels = $request->getParsedBody()['glevels'] ?? [];
284
        $this->tag     = $request->getParsedBody()['tag'] ?? [];
285
        $this->text    = $request->getParsedBody()['text'] ?? [];
286
        $this->islink  = $request->getParsedBody()['islink'] ?? [];
287
288
        $this->splitSource();
289
        $indi_gedcom = '0 @@ INDI';
290
        $indi_gedcom .= $this->addNewName($request, $tree);
291
        $indi_gedcom .= $this->addNewSex($request);
292
        if (preg_match_all('/([A-Z0-9_]+)/', $tree->getPreference('QUICK_REQUIRED_FACTS'), $matches)) {
293
            foreach ($matches[1] as $match) {
294
                $indi_gedcom .= $this->addNewFact($request, $tree, $match);
295
            }
296
        }
297
        if ($request->getParsedBody()['SOUR_INDI'] ?? false) {
298
            $indi_gedcom = $this->handleUpdates($indi_gedcom);
299
        } else {
300
            $indi_gedcom = $this->updateRest($indi_gedcom);
301
        }
302
303
        $fam_gedcom = '';
304
        if (preg_match_all('/([A-Z0-9_]+)/', $tree->getPreference('QUICK_REQUIRED_FAMFACTS'), $matches)) {
305
            foreach ($matches[1] as $match) {
306
                $fam_gedcom .= $this->addNewFact($request, $tree, $match);
307
            }
308
        }
309
        if ($request->getParsedBody()['SOUR_FAM'] ?? false) {
310
            $fam_gedcom = $this->handleUpdates($fam_gedcom);
311
        } else {
312
            $fam_gedcom = $this->updateRest($fam_gedcom);
313
        }
314
315
        // Create the new spouse
316
        $spouse = $tree->createIndividual($indi_gedcom);
317
        // Create a new family
318
        if ($sex === 'F') {
319
            $family = $tree->createFamily("0 @@ FAM\n1 WIFE @" . $spouse->xref() . "@\n1 HUSB @" . $individual->xref() . '@' . $fam_gedcom);
320
        } else {
321
            $family = $tree->createFamily("0 @@ FAM\n1 HUSB @" . $spouse->xref() . "@\n1 WIFE @" . $individual->xref() . '@' . $fam_gedcom);
322
        }
323
        // Link the spouses to the family
324
        $spouse->createFact('1 FAMS @' . $family->xref() . '@', true);
325
        $individual->createFact('1 FAMS @' . $family->xref() . '@', true);
326
327
        if (($request->getParsedBody()['goto'] ?? '') === 'new') {
328
            return redirect($spouse->url());
329
        }
330
331
        return redirect($individual->url());
332
    }
333
334
    /**
335
     * Add an unlinked individual
336
     *
337
     * @param ServerRequestInterface $request
338
     *
339
     * @return ResponseInterface
340
     */
341
    public function addUnlinked(ServerRequestInterface $request): ResponseInterface
342
    {
343
        $tree = $request->getAttribute('tree');
344
        assert($tree instanceof Tree, new InvalidArgumentException());
345
346
        return $this->viewResponse('edit/new-individual', [
347
            'next_action' => 'add-unlinked-individual-action',
348
            'tree'        => $tree,
349
            'title'       => I18N::translate('Create an individual'),
350
            'individual'  => null,
351
            'family'      => null,
352
            'name_fact'   => null,
353
            'famtag'      => '',
354
            'gender'      => 'U',
355
        ]);
356
    }
357
358
    /**
359
     * @param ServerRequestInterface $request
360
     *
361
     * @return ResponseInterface
362
     */
363
    public function addUnlinkedAction(ServerRequestInterface $request): ResponseInterface
364
    {
365
        $tree          = $request->getAttribute('tree');
366
        $this->glevels = $request->getParsedBody()['glevels'] ?? [];
367
        $this->tag     = $request->getParsedBody()['tag'] ?? [];
368
        $this->text    = $request->getParsedBody()['text'] ?? [];
369
        $this->islink  = $request->getParsedBody()['islink'] ?? [];
370
371
        $this->splitSource();
372
        $gedrec = '0 @@ INDI';
373
        $gedrec .= $this->addNewName($request, $tree);
374
        $gedrec .= $this->addNewSex($request);
375
        if (preg_match_all('/([A-Z0-9_]+)/', $tree->getPreference('QUICK_REQUIRED_FACTS'), $matches)) {
376
            foreach ($matches[1] as $match) {
377
                $gedrec .= $this->addNewFact($request, $tree, $match);
378
            }
379
        }
380
        if ($request->getParsedBody()['SOUR_INDI'] ?? false) {
381
            $gedrec = $this->handleUpdates($gedrec);
382
        } else {
383
            $gedrec = $this->updateRest($gedrec);
384
        }
385
386
        $new_indi = $tree->createIndividual($gedrec);
387
388
        if (($request->getParsedBody()['goto'] ?? '') === 'new') {
389
            return redirect($new_indi->url());
390
        }
391
392
        return redirect(route('manage-trees', ['tree' => $tree->name()]));
393
    }
394
395
    /**
396
     * Edit a name record.
397
     *
398
     * @param ServerRequestInterface $request
399
     *
400
     * @return ResponseInterface
401
     */
402
    public function editName(ServerRequestInterface $request): ResponseInterface
403
    {
404
        $tree    = $request->getAttribute('tree');
405
        $fact_id = $request->getQueryParams()['fact_id'];
406
        $xref    = $request->getQueryParams()['xref'];
407
408
        $individual = Individual::getInstance($xref, $tree);
409
410
        Auth::checkIndividualAccess($individual, true);
411
412
        // Find the fact to edit
413
        foreach ($individual->facts() as $fact) {
414
            if ($fact->id() === $fact_id && $fact->canEdit()) {
415
                return $this->viewResponse('edit/new-individual', [
416
                    'next_action' => 'edit-name-action',
417
                    'tree'        => $tree,
418
                    'title'       => I18N::translate('Edit the name'),
419
                    'individual'  => $individual,
420
                    'family'      => null,
421
                    'name_fact'   => $fact,
422
                    'famtag'      => '',
423
                    'gender'      => $individual->sex(),
424
                ]);
425
            }
426
        }
427
428
        throw new NotFoundHttpException();
429
    }
430
431
    /**
432
     * @param ServerRequestInterface $request
433
     *
434
     * @return ResponseInterface
435
     */
436
    public function editNameAction(ServerRequestInterface $request): ResponseInterface
437
    {
438
        $tree = $request->getAttribute('tree');
439
        assert($tree instanceof Tree, new InvalidArgumentException());
440
441
        $xref = $request->getParsedBody()['xref'];
442
443
        $individual = Individual::getInstance($xref, $tree);
444
445
        Auth::checkIndividualAccess($individual, true);
446
447
        // @TODO - Move the name-specific code to this function?
448
        return app(EditGedcomRecordController::class)->updateFact($request, $tree);
0 ignored issues
show
Unused Code introduced by
The call to Fisharebest\Webtrees\Htt...ontroller::updateFact() has too many arguments starting with $tree. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

448
        return app(EditGedcomRecordController::class)->/** @scrutinizer ignore-call */ updateFact($request, $tree);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
449
    }
450
451
    /**
452
     * Add a new name record.
453
     *
454
     * @param ServerRequestInterface $request
455
     *
456
     * @return ResponseInterface
457
     */
458
    public function addName(ServerRequestInterface $request): ResponseInterface
459
    {
460
        $tree = $request->getAttribute('tree');
461
        assert($tree instanceof Tree, new InvalidArgumentException());
462
463
        $xref = $request->getQueryParams()['xref'];
464
465
        $individual = Individual::getInstance($xref, $tree);
466
467
        Auth::checkIndividualAccess($individual, true);
468
469
        $title = $individual->fullName() . ' — ' . I18N::translate('Add a name');
470
471
        return $this->viewResponse('edit/new-individual', [
472
            'next_action' => 'add-name-action',
473
            'tree'        => $tree,
474
            'title'       => $title,
475
            'individual'  => $individual,
476
            'family'      => null,
477
            'name_fact'   => null,
478
            'famtag'      => '',
479
            'gender'      => $individual->sex(),
480
        ]);
481
    }
482
483
    /**
484
     * @param ServerRequestInterface $request
485
     *
486
     * @return ResponseInterface
487
     */
488
    public function addNameAction(ServerRequestInterface $request): ResponseInterface
489
    {
490
        $tree = $request->getAttribute('tree');
491
        assert($tree instanceof Tree, new InvalidArgumentException());
492
493
        $xref = $request->getParsedBody()['xref'];
494
495
        $individual = Individual::getInstance($xref, $tree);
496
497
        Auth::checkIndividualAccess($individual, true);
498
499
        // @TODO - Move the name-specific code to this function?
500
501
        return app(EditGedcomRecordController::class)->updateFact($request, $tree);
0 ignored issues
show
Unused Code introduced by
The call to Fisharebest\Webtrees\Htt...ontroller::updateFact() has too many arguments starting with $tree. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

501
        return app(EditGedcomRecordController::class)->/** @scrutinizer ignore-call */ updateFact($request, $tree);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
502
    }
503
504
    /**
505
     * @param ServerRequestInterface $request
506
     *
507
     * @return ResponseInterface
508
     */
509
    public function linkChildToFamily(ServerRequestInterface $request): ResponseInterface
510
    {
511
        $tree = $request->getAttribute('tree');
512
        assert($tree instanceof Tree, new InvalidArgumentException());
513
514
        $xref = $request->getQueryParams()['xref'];
515
516
        $individual = Individual::getInstance($xref, $tree);
517
518
        Auth::checkIndividualAccess($individual, true);
519
520
        $title = $individual->fullName() . ' - ' . I18N::translate('Link this individual to an existing family as a child');
521
522
        return $this->viewResponse('edit/link-child-to-family', [
523
            'individual' => $individual,
524
            'title'      => $title,
525
        ]);
526
    }
527
528
    /**
529
     * @param ServerRequestInterface $request
530
     *
531
     * @return ResponseInterface
532
     */
533
    public function linkChildToFamilyAction(ServerRequestInterface $request): ResponseInterface
534
    {
535
        $tree   = $request->getAttribute('tree');
536
        $xref   = $request->getParsedBody()['xref'];
537
        $famid  = $request->getParsedBody()['famid'];
538
        $PEDI   = $request->getParsedBody()['PEDI'];
539
540
        $individual = Individual::getInstance($xref, $tree);
541
        Auth::checkIndividualAccess($individual, true);
542
543
        $family = Family::getInstance($famid, $tree);
544
        Auth::checkFamilyAccess($family, true);
545
546
        // Replace any existing child->family link (we may be changing the PEDI);
547
        $fact_id = '';
548
        foreach ($individual->facts(['FAMC']) as $fact) {
549
            if ($family === $fact->target()) {
550
                $fact_id = $fact->id();
551
                break;
552
            }
553
        }
554
555
        $gedcom = GedcomCodePedi::createNewFamcPedi($PEDI, $famid);
556
        $individual->updateFact($fact_id, $gedcom, true);
557
558
        // Only set the family->child link if it does not already exist
559
        $chil_link_exists = false;
560
        foreach ($family->facts(['CHIL']) as $fact) {
561
            if ($individual === $fact->target()) {
562
                $chil_link_exists = true;
563
                break;
564
            }
565
        }
566
567
        if (!$chil_link_exists) {
568
            $family->createFact('1 CHIL @' . $individual->xref() . '@', true);
569
        }
570
571
        return redirect($individual->url());
572
    }
573
574
    /**
575
     * @param ServerRequestInterface $request
576
     *
577
     * @return ResponseInterface
578
     */
579
    public function linkSpouseToIndividual(ServerRequestInterface $request): ResponseInterface
580
    {
581
        $tree = $request->getAttribute('tree');
582
        assert($tree instanceof Tree, new InvalidArgumentException());
583
584
        $xref = $request->getQueryParams()['xref'];
585
586
        $individual = Individual::getInstance($xref, $tree);
587
588
        Auth::checkIndividualAccess($individual, true);
589
590
        if ($individual->sex() === 'F') {
591
            $title = $individual->fullName() . ' - ' . I18N::translate('Add a husband using an existing individual');
592
            $label = I18N::translate('Husband');
593
        } else {
594
            $title = $individual->fullName() . ' - ' . I18N::translate('Add a wife using an existing individual');
595
            $label = I18N::translate('Wife');
596
        }
597
598
        return $this->viewResponse('edit/link-spouse-to-individual', [
599
            'individual' => $individual,
600
            'label'      => $label,
601
            'title'      => $title,
602
        ]);
603
    }
604
605
    /**
606
     * @param ServerRequestInterface $request
607
     *
608
     * @return ResponseInterface
609
     */
610
    public function linkSpouseToIndividualAction(ServerRequestInterface $request): ResponseInterface
611
    {
612
        $tree   = $request->getAttribute('tree');
613
        $xref   = $request->getParsedBody()['xref'];
614
        $spouse = $request->getParsedBody()['spid'];
615
616
        $individual = Individual::getInstance($xref, $tree);
617
        Auth::checkIndividualAccess($individual, true);
618
619
        $spouse = Individual::getInstance($spouse, $tree);
620
        Auth::checkIndividualAccess($spouse, true);
621
622
        if ($individual->sex() === 'M') {
623
            $gedcom = "0 @@ FAM\n1 HUSB @" . $individual->xref() . "@\n1 WIFE @" . $spouse->xref() . '@';
624
        } else {
625
            $gedcom = "0 @@ FAM\n1 WIFE @" . $individual->xref() . "@\n1 HUSB @" . $spouse->xref() . '@';
626
        }
627
628
        $gedcom .= $this->addNewFact($request, $tree, 'MARR');
629
630
        $family = $tree->createFamily($gedcom);
631
632
        $individual->createFact('1 FAMS @' . $family->xref() . '@', true);
633
        $spouse->createFact('1 FAMS @' . $family->xref() . '@', true);
634
635
        return redirect($family->url());
636
    }
637
}
638