143322877SGreg Roach<?php 243322877SGreg Roach 343322877SGreg Roach/** 443322877SGreg Roach * webtrees: online genealogy 589f7189bSGreg Roach * Copyright (C) 2021 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; 25*b55cbc6bSGreg 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 { 45*b55cbc6bSGreg Roach $tree = Validator::attributes($request)->tree(); 4643322877SGreg Roach 4743322877SGreg Roach $params = (array) $request->getParsedBody(); 4843322877SGreg Roach 4943322877SGreg Roach $xref = $params['xref']; 506b9cb339SGreg Roach $family = Registry::familyFactory()->make($xref, $tree); 5143322877SGreg Roach $family = Auth::checkFamilyAccess($family, true); 5243322877SGreg Roach 5343322877SGreg Roach $params = (array) $request->getParsedBody(); 5443322877SGreg Roach 5543322877SGreg Roach $HUSB = $params['HUSB'] ?? ''; 5643322877SGreg Roach $WIFE = $params['WIFE'] ?? ''; 5743322877SGreg Roach $CHIL = $params['CHIL'] ?? []; 5843322877SGreg Roach 5943322877SGreg Roach // Current family members 6043322877SGreg Roach $old_father = $family->husband(); 6143322877SGreg Roach $old_mother = $family->wife(); 6243322877SGreg Roach $old_children = $family->children(); 6343322877SGreg Roach 6443322877SGreg Roach // New family members 656b9cb339SGreg Roach $new_father = Registry::individualFactory()->make($HUSB, $tree); 666b9cb339SGreg Roach $new_mother = Registry::individualFactory()->make($WIFE, $tree); 6743322877SGreg Roach $new_children = []; 6843322877SGreg Roach foreach ($CHIL as $child) { 696b9cb339SGreg Roach $new_children[] = Registry::individualFactory()->make($child, $tree); 7043322877SGreg Roach } 7143322877SGreg Roach 7243322877SGreg Roach if ($old_father !== $new_father) { 7343322877SGreg Roach if ($old_father instanceof Individual) { 7443322877SGreg Roach // Remove old FAMS link 7543322877SGreg Roach foreach ($old_father->facts(['FAMS']) as $fact) { 7643322877SGreg Roach if ($fact->target() === $family) { 7743322877SGreg Roach $old_father->deleteFact($fact->id(), true); 7843322877SGreg Roach } 7943322877SGreg Roach } 8043322877SGreg Roach // Remove old HUSB link 8143322877SGreg Roach foreach ($family->facts(['HUSB', 'WIFE']) as $fact) { 8243322877SGreg Roach if ($fact->target() === $old_father) { 8343322877SGreg Roach $family->deleteFact($fact->id(), true); 8443322877SGreg Roach } 8543322877SGreg Roach } 8643322877SGreg Roach } 8743322877SGreg Roach if ($new_father instanceof Individual) { 8843322877SGreg Roach // Add new FAMS link 8943322877SGreg Roach $new_father->createFact('1 FAMS @' . $family->xref() . '@', true); 9043322877SGreg Roach // Add new HUSB link 9143322877SGreg Roach $family->createFact('1 HUSB @' . $new_father->xref() . '@', true); 9243322877SGreg Roach } 9343322877SGreg Roach } 9443322877SGreg Roach 9543322877SGreg Roach if ($old_mother !== $new_mother) { 9643322877SGreg Roach if ($old_mother instanceof Individual) { 9743322877SGreg Roach // Remove old FAMS link 9843322877SGreg Roach foreach ($old_mother->facts(['FAMS']) as $fact) { 9943322877SGreg Roach if ($fact->target() === $family) { 10043322877SGreg Roach $old_mother->deleteFact($fact->id(), true); 10143322877SGreg Roach } 10243322877SGreg Roach } 10343322877SGreg Roach // Remove old WIFE link 10443322877SGreg Roach foreach ($family->facts(['HUSB', 'WIFE']) as $fact) { 10543322877SGreg Roach if ($fact->target() === $old_mother) { 10643322877SGreg Roach $family->deleteFact($fact->id(), true); 10743322877SGreg Roach } 10843322877SGreg Roach } 10943322877SGreg Roach } 11043322877SGreg Roach if ($new_mother instanceof Individual) { 11143322877SGreg Roach // Add new FAMS link 11243322877SGreg Roach $new_mother->createFact('1 FAMS @' . $family->xref() . '@', true); 11343322877SGreg Roach // Add new WIFE link 11443322877SGreg Roach $family->createFact('1 WIFE @' . $new_mother->xref() . '@', true); 11543322877SGreg Roach } 11643322877SGreg Roach } 11743322877SGreg Roach 11843322877SGreg Roach foreach ($old_children as $old_child) { 11943322877SGreg Roach if (!in_array($old_child, $new_children, true)) { 12043322877SGreg Roach // Remove old FAMC link 12143322877SGreg Roach foreach ($old_child->facts(['FAMC']) as $fact) { 12243322877SGreg Roach if ($fact->target() === $family) { 12343322877SGreg Roach $old_child->deleteFact($fact->id(), true); 12443322877SGreg Roach } 12543322877SGreg Roach } 12643322877SGreg Roach // Remove old CHIL link 12743322877SGreg Roach foreach ($family->facts(['CHIL']) as $fact) { 12843322877SGreg Roach if ($fact->target() === $old_child) { 12943322877SGreg Roach $family->deleteFact($fact->id(), true); 13043322877SGreg Roach } 13143322877SGreg Roach } 13243322877SGreg Roach } 13343322877SGreg Roach } 13443322877SGreg Roach 13543322877SGreg Roach foreach ($new_children as $new_child) { 13643322877SGreg Roach if ($new_child instanceof Individual && !$old_children->contains($new_child)) { 13743322877SGreg Roach // Add new FAMC link 13843322877SGreg Roach $new_child->createFact('1 FAMC @' . $family->xref() . '@', true); 13943322877SGreg Roach // Add new CHIL link 14043322877SGreg Roach $family->createFact('1 CHIL @' . $new_child->xref() . '@', true); 14143322877SGreg Roach } 14243322877SGreg Roach } 14343322877SGreg Roach 14443322877SGreg Roach return redirect($family->url()); 14543322877SGreg Roach } 14643322877SGreg Roach} 147