xref: /webtrees/app/Http/RequestHandlers/ChangeFamilyMembersAction.php (revision d11be7027e34e3121be11cc025421873364403f9)
143322877SGreg Roach<?php
243322877SGreg Roach
343322877SGreg Roach/**
443322877SGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
643322877SGreg Roach * This program is free software: you can redistribute it and/or modify
743322877SGreg Roach * it under the terms of the GNU General Public License as published by
843322877SGreg Roach * the Free Software Foundation, either version 3 of the License, or
943322877SGreg Roach * (at your option) any later version.
1043322877SGreg Roach * This program is distributed in the hope that it will be useful,
1143322877SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
1243322877SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1343322877SGreg Roach * GNU General Public License for more details.
1443322877SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
1643322877SGreg Roach */
1743322877SGreg Roach
1843322877SGreg Roachdeclare(strict_types=1);
1943322877SGreg Roach
2043322877SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
2143322877SGreg Roach
2243322877SGreg Roachuse Fisharebest\Webtrees\Auth;
2343322877SGreg Roachuse Fisharebest\Webtrees\Individual;
246b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
25b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
2643322877SGreg Roachuse Psr\Http\Message\ResponseInterface;
2743322877SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
2843322877SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
2943322877SGreg Roach
3043322877SGreg Roachuse function in_array;
3143322877SGreg Roachuse function redirect;
3243322877SGreg Roach
3343322877SGreg Roach/**
3443322877SGreg Roach * Change the members of a family.
3543322877SGreg Roach */
3643322877SGreg Roachclass ChangeFamilyMembersAction implements RequestHandlerInterface
3743322877SGreg Roach{
3843322877SGreg Roach    /**
3943322877SGreg Roach     * @param ServerRequestInterface $request
4043322877SGreg Roach     *
4143322877SGreg Roach     * @return ResponseInterface
4243322877SGreg Roach     */
4343322877SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
4443322877SGreg Roach    {
45b55cbc6bSGreg Roach        $tree   = Validator::attributes($request)->tree();
46748dbe15SGreg Roach        $xref   = Validator::parsedBody($request)->isXref()->string('xref');
476b9cb339SGreg Roach        $family = Registry::familyFactory()->make($xref, $tree);
4843322877SGreg Roach        $family = Auth::checkFamilyAccess($family, true);
4943322877SGreg Roach
50748dbe15SGreg Roach        $HUSB = Validator::parsedBody($request)->isXref()->string('HUSB', '');
51748dbe15SGreg Roach        $WIFE = Validator::parsedBody($request)->isXref()->string('WIFE', '');
526d9950ddSGreg Roach        $CHIL = Validator::parsedBody($request)->array('CHIL');
5343322877SGreg Roach
5443322877SGreg Roach        // Current family members
5543322877SGreg Roach        $old_father   = $family->husband();
5643322877SGreg Roach        $old_mother   = $family->wife();
5743322877SGreg Roach        $old_children = $family->children();
5843322877SGreg Roach
5943322877SGreg Roach        // New family members
606b9cb339SGreg Roach        $new_father   = Registry::individualFactory()->make($HUSB, $tree);
616b9cb339SGreg Roach        $new_mother   = Registry::individualFactory()->make($WIFE, $tree);
6243322877SGreg Roach        $new_children = [];
6343322877SGreg Roach        foreach ($CHIL as $child) {
646b9cb339SGreg Roach            $new_children[] = Registry::individualFactory()->make($child, $tree);
6543322877SGreg Roach        }
6643322877SGreg Roach
6743322877SGreg Roach        if ($old_father !== $new_father) {
6843322877SGreg Roach            if ($old_father instanceof Individual) {
6943322877SGreg Roach                // Remove old FAMS link
7043322877SGreg Roach                foreach ($old_father->facts(['FAMS']) as $fact) {
7143322877SGreg Roach                    if ($fact->target() === $family) {
7243322877SGreg Roach                        $old_father->deleteFact($fact->id(), true);
7343322877SGreg Roach                    }
7443322877SGreg Roach                }
7543322877SGreg Roach                // Remove old HUSB link
7643322877SGreg Roach                foreach ($family->facts(['HUSB', 'WIFE']) as $fact) {
7743322877SGreg Roach                    if ($fact->target() === $old_father) {
7843322877SGreg Roach                        $family->deleteFact($fact->id(), true);
7943322877SGreg Roach                    }
8043322877SGreg Roach                }
8143322877SGreg Roach            }
8243322877SGreg Roach            if ($new_father instanceof Individual) {
8343322877SGreg Roach                // Add new FAMS link
8443322877SGreg Roach                $new_father->createFact('1 FAMS @' . $family->xref() . '@', true);
8543322877SGreg Roach                // Add new HUSB link
8643322877SGreg Roach                $family->createFact('1 HUSB @' . $new_father->xref() . '@', true);
8743322877SGreg Roach            }
8843322877SGreg Roach        }
8943322877SGreg Roach
9043322877SGreg Roach        if ($old_mother !== $new_mother) {
9143322877SGreg Roach            if ($old_mother instanceof Individual) {
9243322877SGreg Roach                // Remove old FAMS link
9343322877SGreg Roach                foreach ($old_mother->facts(['FAMS']) as $fact) {
9443322877SGreg Roach                    if ($fact->target() === $family) {
9543322877SGreg Roach                        $old_mother->deleteFact($fact->id(), true);
9643322877SGreg Roach                    }
9743322877SGreg Roach                }
9843322877SGreg Roach                // Remove old WIFE link
9943322877SGreg Roach                foreach ($family->facts(['HUSB', 'WIFE']) as $fact) {
10043322877SGreg Roach                    if ($fact->target() === $old_mother) {
10143322877SGreg Roach                        $family->deleteFact($fact->id(), true);
10243322877SGreg Roach                    }
10343322877SGreg Roach                }
10443322877SGreg Roach            }
10543322877SGreg Roach            if ($new_mother instanceof Individual) {
10643322877SGreg Roach                // Add new FAMS link
10743322877SGreg Roach                $new_mother->createFact('1 FAMS @' . $family->xref() . '@', true);
10843322877SGreg Roach                // Add new WIFE link
10943322877SGreg Roach                $family->createFact('1 WIFE @' . $new_mother->xref() . '@', true);
11043322877SGreg Roach            }
11143322877SGreg Roach        }
11243322877SGreg Roach
11343322877SGreg Roach        foreach ($old_children as $old_child) {
11443322877SGreg Roach            if (!in_array($old_child, $new_children, true)) {
11543322877SGreg Roach                // Remove old FAMC link
11643322877SGreg Roach                foreach ($old_child->facts(['FAMC']) as $fact) {
11743322877SGreg Roach                    if ($fact->target() === $family) {
11843322877SGreg Roach                        $old_child->deleteFact($fact->id(), true);
11943322877SGreg Roach                    }
12043322877SGreg Roach                }
12143322877SGreg Roach                // Remove old CHIL link
12243322877SGreg Roach                foreach ($family->facts(['CHIL']) as $fact) {
12343322877SGreg Roach                    if ($fact->target() === $old_child) {
12443322877SGreg Roach                        $family->deleteFact($fact->id(), true);
12543322877SGreg Roach                    }
12643322877SGreg Roach                }
12743322877SGreg Roach            }
12843322877SGreg Roach        }
12943322877SGreg Roach
13043322877SGreg Roach        foreach ($new_children as $new_child) {
13143322877SGreg Roach            if ($new_child instanceof Individual && !$old_children->contains($new_child)) {
13243322877SGreg Roach                // Add new FAMC link
13343322877SGreg Roach                $new_child->createFact('1 FAMC @' . $family->xref() . '@', true);
13443322877SGreg Roach                // Add new CHIL link
13543322877SGreg Roach                $family->createFact('1 CHIL @' . $new_child->xref() . '@', true);
13643322877SGreg Roach            }
13743322877SGreg Roach        }
13843322877SGreg Roach
13943322877SGreg Roach        return redirect($family->url());
14043322877SGreg Roach    }
14143322877SGreg Roach}
142