famsFactOfLaterMarriage()   A
last analyzed

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