Passed
Pull Request — main (#5260)
by
unknown
08:19
created

LinkChildToFamilyAction::factIdOfYoungerSibling()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 3
eloc 5
nc 3
nop 2
dl 0
loc 9
rs 10
c 1
b 1
f 0
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2025 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
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22
use Fisharebest\Webtrees\Auth;
0 ignored issues
show
Bug introduced by
The type Fisharebest\Webtrees\Auth was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
use Fisharebest\Webtrees\Elements\PedigreeLinkageType;
0 ignored issues
show
Bug introduced by
The type Fisharebest\Webtrees\Elements\PedigreeLinkageType was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
use Fisharebest\Webtrees\Family;
0 ignored issues
show
Bug introduced by
The type Fisharebest\Webtrees\Family was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
use Fisharebest\Webtrees\Individual;
0 ignored issues
show
Bug introduced by
The type Fisharebest\Webtrees\Individual was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
26
use Fisharebest\Webtrees\Registry;
27
use Fisharebest\Webtrees\Validator;
28
use Psr\Http\Message\ResponseInterface;
29
use Psr\Http\Message\ServerRequestInterface;
30
use Psr\Http\Server\RequestHandlerInterface;
31
32
use function redirect;
33
34
/**
35
 * Link an existing individual as child in an existing family.
36
 */
37
class LinkChildToFamilyAction implements RequestHandlerInterface
38
{
39
40
    public function handle(ServerRequestInterface $request): ResponseInterface
41
    {
42
        $tree       = Validator::attributes($request)->tree();
43
        $xref       = Validator::attributes($request)->isXref()->string('xref');
44
        $individual = Registry::individualFactory()->make($xref, $tree);
45
        $individual = Auth::checkIndividualAccess($individual, true);
46
        $famid      = Validator::parsedBody($request)->isXref()->string('famid');
47
        $PEDI       = Validator::parsedBody($request)->string('PEDI');
48
49
        $family = Registry::familyFactory()->make($famid, $tree);
50
        $family = Auth::checkFamilyAccess($family, true);
51
52
        // Replace any existing child->family link (we may be changing the PEDI);
53
        $fact_id = '';
54
        foreach ($individual->facts(['FAMC']) as $fact) {
55
            if ($family === $fact->target()) {
56
                $fact_id = $fact->id();
57
                break;
58
            }
59
        }
60
61
        switch ($PEDI) {
62
            case '':
63
                $gedcom = "1 FAMC @$famid@";
64
                break;
65
            case PedigreeLinkageType::VALUE_ADOPTED:
66
                $gedcom = "1 FAMC @$famid@\n2 PEDI $PEDI\n1 ADOP\n2 FAMC @$famid@\n3 ADOP BOTH";
67
                break;
68
            case PedigreeLinkageType::VALUE_SEALING:
69
                $gedcom = "1 FAMC @$famid@\n2 PEDI $PEDI\n1 SLGC\n2 FAMC @$famid@";
70
                break;
71
            case PedigreeLinkageType::VALUE_FOSTER:
72
                $gedcom = "1 FAMC @$famid@\n2 PEDI $PEDI\n1 EVEN\n2 TYPE $PEDI";
73
                break;
74
            default:
75
                $gedcom = "1 FAMC @$famid@\n2 PEDI $PEDI";
76
                break;
77
        }
78
79
        if ($fact_id === '') {
80
            $individual->createFact($gedcom, true);
81
        } else {
82
            $individual->updateFact($fact_id, $gedcom, true);
83
        }
84
85
        // Only set the family->child link if it does not already exist
86
        $chil_link_exists = false;
87
        foreach ($family->facts(['CHIL']) as $fact) {
88
            if ($individual === $fact->target()) {
89
                $chil_link_exists = true;
90
                break;
91
            }
92
        }
93
94
        if (!$chil_link_exists) {
95
            $before_id = $this->factIdOfYoungerSibling($family, $individual);
96
            $family->createFact('1 CHIL @' . $individual->xref() . '@', true, $before_id);
97
        }
98
99
        return redirect($individual->url());
100
    }
101
102
    private function factIdOfYoungerSibling(Family $family, Individual $child): string
103
    {
104
        $child_birth_day = $child->getBirthDate()->julianDay();
105
        foreach ($family->facts(['CHIL'], false, Auth::PRIV_HIDE, true) as $fact) {
106
            if ($child_birth_day < $fact->target()->getBirthDate()->julianDay()) {
107
                return $fact->id();
108
            }
109
        }
110
        return '';
111
    }
112
113
}
114