xref: /webtrees/app/Http/RequestHandlers/ChangeFamilyMembersAction.php (revision b4c5c807eb501a4b2f3da3cb8eb38981bf4a3a25)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 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
47        $params = (array) $request->getParsedBody();
48
49        $xref   = $params['xref'];
50        $family = Registry::familyFactory()->make($xref, $tree);
51        $family = Auth::checkFamilyAccess($family, true);
52
53        $params = (array) $request->getParsedBody();
54
55        $HUSB = $params['HUSB'] ?? '';
56        $WIFE = $params['WIFE'] ?? '';
57        $CHIL = $params['CHIL'] ?? [];
58
59        // Current family members
60        $old_father   = $family->husband();
61        $old_mother   = $family->wife();
62        $old_children = $family->children();
63
64        // New family members
65        $new_father   = Registry::individualFactory()->make($HUSB, $tree);
66        $new_mother   = Registry::individualFactory()->make($WIFE, $tree);
67        $new_children = [];
68        foreach ($CHIL as $child) {
69            $new_children[] = Registry::individualFactory()->make($child, $tree);
70        }
71
72        if ($old_father !== $new_father) {
73            if ($old_father instanceof Individual) {
74                // Remove old FAMS link
75                foreach ($old_father->facts(['FAMS']) as $fact) {
76                    if ($fact->target() === $family) {
77                        $old_father->deleteFact($fact->id(), true);
78                    }
79                }
80                // Remove old HUSB link
81                foreach ($family->facts(['HUSB', 'WIFE']) as $fact) {
82                    if ($fact->target() === $old_father) {
83                        $family->deleteFact($fact->id(), true);
84                    }
85                }
86            }
87            if ($new_father instanceof Individual) {
88                // Add new FAMS link
89                $new_father->createFact('1 FAMS @' . $family->xref() . '@', true);
90                // Add new HUSB link
91                $family->createFact('1 HUSB @' . $new_father->xref() . '@', true);
92            }
93        }
94
95        if ($old_mother !== $new_mother) {
96            if ($old_mother instanceof Individual) {
97                // Remove old FAMS link
98                foreach ($old_mother->facts(['FAMS']) as $fact) {
99                    if ($fact->target() === $family) {
100                        $old_mother->deleteFact($fact->id(), true);
101                    }
102                }
103                // Remove old WIFE link
104                foreach ($family->facts(['HUSB', 'WIFE']) as $fact) {
105                    if ($fact->target() === $old_mother) {
106                        $family->deleteFact($fact->id(), true);
107                    }
108                }
109            }
110            if ($new_mother instanceof Individual) {
111                // Add new FAMS link
112                $new_mother->createFact('1 FAMS @' . $family->xref() . '@', true);
113                // Add new WIFE link
114                $family->createFact('1 WIFE @' . $new_mother->xref() . '@', true);
115            }
116        }
117
118        foreach ($old_children as $old_child) {
119            if (!in_array($old_child, $new_children, true)) {
120                // Remove old FAMC link
121                foreach ($old_child->facts(['FAMC']) as $fact) {
122                    if ($fact->target() === $family) {
123                        $old_child->deleteFact($fact->id(), true);
124                    }
125                }
126                // Remove old CHIL link
127                foreach ($family->facts(['CHIL']) as $fact) {
128                    if ($fact->target() === $old_child) {
129                        $family->deleteFact($fact->id(), true);
130                    }
131                }
132            }
133        }
134
135        foreach ($new_children as $new_child) {
136            if ($new_child instanceof Individual && !$old_children->contains($new_child)) {
137                // Add new FAMC link
138                $new_child->createFact('1 FAMC @' . $family->xref() . '@', true);
139                // Add new CHIL link
140                $family->createFact('1 CHIL @' . $new_child->xref() . '@', true);
141            }
142        }
143
144        return redirect($family->url());
145    }
146}
147