xref: /webtrees/app/Http/RequestHandlers/ChangeFamilyMembersAction.php (revision ac71572d8462e396ed5a307f05b29381e49f9e6e)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2022 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
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22use Fisharebest\Webtrees\Auth;
23use Fisharebest\Webtrees\Individual;
24use Fisharebest\Webtrees\Registry;
25use Fisharebest\Webtrees\Validator;
26use Psr\Http\Message\ResponseInterface;
27use Psr\Http\Message\ServerRequestInterface;
28use Psr\Http\Server\RequestHandlerInterface;
29
30use function in_array;
31use function redirect;
32
33/**
34 * Change the members of a family.
35 */
36class ChangeFamilyMembersAction implements RequestHandlerInterface
37{
38    /**
39     * @param ServerRequestInterface $request
40     *
41     * @return ResponseInterface
42     */
43    public function handle(ServerRequestInterface $request): ResponseInterface
44    {
45        $tree   = Validator::attributes($request)->tree();
46        $xref   = Validator::parsedBody($request)->isXref()->string('xref');
47        $family = Registry::familyFactory()->make($xref, $tree);
48        $family = Auth::checkFamilyAccess($family, true);
49
50        $HUSB = Validator::parsedBody($request)->isXref()->string('HUSB', '');
51        $WIFE = Validator::parsedBody($request)->isXref()->string('WIFE', '');
52        $CHIL = Validator::parsedBody($request)->isXref()->array('CHIL');
53
54        // Current family members
55        $old_father   = $family->husband();
56        $old_mother   = $family->wife();
57        $old_children = $family->children();
58
59        // New family members
60        $new_father   = Registry::individualFactory()->make($HUSB, $tree);
61        $new_mother   = Registry::individualFactory()->make($WIFE, $tree);
62        $new_children = [];
63        foreach ($CHIL as $child) {
64            $new_children[] = Registry::individualFactory()->make($child, $tree);
65        }
66
67        if ($old_father !== $new_father) {
68            if ($old_father instanceof Individual) {
69                // Remove old FAMS link
70                foreach ($old_father->facts(['FAMS']) as $fact) {
71                    if ($fact->target() === $family) {
72                        $old_father->deleteFact($fact->id(), true);
73                    }
74                }
75                // Remove old HUSB link
76                foreach ($family->facts(['HUSB', 'WIFE']) as $fact) {
77                    if ($fact->target() === $old_father) {
78                        $family->deleteFact($fact->id(), true);
79                    }
80                }
81            }
82            if ($new_father instanceof Individual) {
83                // Add new FAMS link
84                $new_father->createFact('1 FAMS @' . $family->xref() . '@', true);
85                // Add new HUSB link
86                $family->createFact('1 HUSB @' . $new_father->xref() . '@', true);
87            }
88        }
89
90        if ($old_mother !== $new_mother) {
91            if ($old_mother instanceof Individual) {
92                // Remove old FAMS link
93                foreach ($old_mother->facts(['FAMS']) as $fact) {
94                    if ($fact->target() === $family) {
95                        $old_mother->deleteFact($fact->id(), true);
96                    }
97                }
98                // Remove old WIFE link
99                foreach ($family->facts(['HUSB', 'WIFE']) as $fact) {
100                    if ($fact->target() === $old_mother) {
101                        $family->deleteFact($fact->id(), true);
102                    }
103                }
104            }
105            if ($new_mother instanceof Individual) {
106                // Add new FAMS link
107                $new_mother->createFact('1 FAMS @' . $family->xref() . '@', true);
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                $family->createFact('1 CHIL @' . $new_child->xref() . '@', true);
136            }
137        }
138
139        return redirect($family->url());
140    }
141}
142