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

factIdOfYoungerSibling()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 2
dl 0
loc 9
rs 10
c 0
b 0
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\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...
24
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...
25
use Fisharebest\Webtrees\Registry;
26
use Fisharebest\Webtrees\Validator;
27
use Psr\Http\Message\ResponseInterface;
28
use Psr\Http\Message\ServerRequestInterface;
29
use Psr\Http\Server\RequestHandlerInterface;
30
31
use function in_array;
32
use function redirect;
33
34
/**
35
 * Change the members of a family.
36
 */
37
class ChangeFamilyMembersAction implements RequestHandlerInterface
38
{
39
    public function handle(ServerRequestInterface $request): ResponseInterface
40
    {
41
        $tree   = Validator::attributes($request)->tree();
42
        $xref   = Validator::parsedBody($request)->isXref()->string('xref');
43
        $family = Registry::familyFactory()->make($xref, $tree);
44
        $family = Auth::checkFamilyAccess($family, true);
45
46
        $HUSB = Validator::parsedBody($request)->isXref()->string('HUSB', '');
47
        $WIFE = Validator::parsedBody($request)->isXref()->string('WIFE', '');
48
        $CHIL = Validator::parsedBody($request)->array('CHIL');
49
50
        // Current family members
51
        $old_father   = $family->husband();
52
        $old_mother   = $family->wife();
53
        $old_children = $family->children();
54
55
        // New family members
56
        $new_father   = Registry::individualFactory()->make($HUSB, $tree);
57
        $new_mother   = Registry::individualFactory()->make($WIFE, $tree);
58
        $new_children = [];
59
        foreach ($CHIL as $child) {
60
            $new_children[] = Registry::individualFactory()->make($child, $tree);
61
        }
62
63
        if ($old_father !== $new_father) {
64
            if ($old_father instanceof Individual) {
65
                // Remove old FAMS link
66
                foreach ($old_father->facts(['FAMS']) as $fact) {
67
                    if ($fact->target() === $family) {
68
                        $old_father->deleteFact($fact->id(), true);
69
                    }
70
                }
71
                // Remove old HUSB link
72
                foreach ($family->facts(['HUSB', 'WIFE']) as $fact) {
73
                    if ($fact->target() === $old_father) {
74
                        $family->deleteFact($fact->id(), true);
75
                    }
76
                }
77
            }
78
            if ($new_father instanceof Individual) {
79
                // Add new FAMS link
80
                $before_id = $this->factIdOfLaterMarriage($new_father, $family);
81
                $new_father->createFact('1 FAMS @' . $family->xref() . '@', true, $before_id);
82
                // Add new HUSB link
83
                $family->createFact('1 HUSB @' . $new_father->xref() . '@', true);
84
            }
85
        }
86
87
        if ($old_mother !== $new_mother) {
88
            if ($old_mother instanceof Individual) {
89
                // Remove old FAMS link
90
                foreach ($old_mother->facts(['FAMS']) as $fact) {
91
                    if ($fact->target() === $family) {
92
                        $old_mother->deleteFact($fact->id(), true);
93
                    }
94
                }
95
                // Remove old WIFE link
96
                foreach ($family->facts(['HUSB', 'WIFE']) as $fact) {
97
                    if ($fact->target() === $old_mother) {
98
                        $family->deleteFact($fact->id(), true);
99
                    }
100
                }
101
            }
102
            if ($new_mother instanceof Individual) {
103
                // Add new FAMS link
104
                $before_id = $this->factIdOfLaterMarriage($new_mother, $family);
105
                $new_mother->createFact('1 FAMS @' . $family->xref() . '@', true, $before_id);
106
                // Add new WIFE link
107
                $family->createFact('1 WIFE @' . $new_mother->xref() . '@', true);
108
            }
109
        }
110
111
        foreach ($old_children as $old_child) {
112
            if (!in_array($old_child, $new_children, true)) {
113
                // Remove old FAMC link
114
                foreach ($old_child->facts(['FAMC']) as $fact) {
115
                    if ($fact->target() === $family) {
116
                        $old_child->deleteFact($fact->id(), true);
117
                    }
118
                }
119
                // Remove old CHIL link
120
                foreach ($family->facts(['CHIL']) as $fact) {
121
                    if ($fact->target() === $old_child) {
122
                        $family->deleteFact($fact->id(), true);
123
                    }
124
                }
125
            }
126
        }
127
128
        foreach ($new_children as $new_child) {
129
            if ($new_child instanceof Individual && !$old_children->contains($new_child)) {
130
                // Add new FAMC link
131
                $new_child->createFact('1 FAMC @' . $family->xref() . '@', true);
132
                // Add new CHIL link
133
                $before_id = $this->factIdOfYoungerSibling($family, $new_child);
134
                $family->createFact('1 CHIL @' . $new_child->xref() . '@', true, $before_id);
135
            }
136
        }
137
138
        return redirect($family->url());
139
    }
140
141
    private function factIdOfYoungerSibling(Family $family, Individual $child): string
142
    {
143
        $child_birth_day = $child->getBirthDate()->julianDay();
144
        foreach ($family->facts(['CHIL'], false, Auth::PRIV_HIDE, true) as $fact) {
145
            if ($child_birth_day < $fact->target()->getBirthDate()->julianDay()) {
146
                return $fact->id();
147
            }
148
        }
149
        return '';
150
    }
151
152
    private function factIdOfLaterMarriage(Individual $partner, Family $family): string
153
    {
154
        $family_marriage_date = $family->getMarriageDate()->julianDay();
155
        foreach ($partner->facts(['FAMS'], false, Auth::PRIV_HIDE, true) as $fact) {
156
            if ($family_marriage_date < $fact->target()->getMarriageDate()->julianDay()) {
157
                return $fact->id();
158
            }
159
        }
160
        return '';
161
    }
162
163
}
164