Passed
Push — main ( 96eac1...90d3f4 )
by Greg
07:27
created

famsFactOfLaterMarriage()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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