1*43322877SGreg Roach<?php 2*43322877SGreg Roach 3*43322877SGreg Roach/** 4*43322877SGreg Roach * webtrees: online genealogy 5*43322877SGreg Roach * Copyright (C) 2020 webtrees development team 6*43322877SGreg Roach * This program is free software: you can redistribute it and/or modify 7*43322877SGreg Roach * it under the terms of the GNU General Public License as published by 8*43322877SGreg Roach * the Free Software Foundation, either version 3 of the License, or 9*43322877SGreg Roach * (at your option) any later version. 10*43322877SGreg Roach * This program is distributed in the hope that it will be useful, 11*43322877SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 12*43322877SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13*43322877SGreg Roach * GNU General Public License for more details. 14*43322877SGreg Roach * You should have received a copy of the GNU General Public License 15*43322877SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 16*43322877SGreg Roach */ 17*43322877SGreg Roach 18*43322877SGreg Roachdeclare(strict_types=1); 19*43322877SGreg Roach 20*43322877SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers; 21*43322877SGreg Roach 22*43322877SGreg Roachuse Fisharebest\Webtrees\Auth; 23*43322877SGreg Roachuse Fisharebest\Webtrees\Factory; 24*43322877SGreg Roachuse Fisharebest\Webtrees\Individual; 25*43322877SGreg Roachuse Fisharebest\Webtrees\Tree; 26*43322877SGreg Roachuse Psr\Http\Message\ResponseInterface; 27*43322877SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 28*43322877SGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 29*43322877SGreg Roach 30*43322877SGreg Roachuse function assert; 31*43322877SGreg Roachuse function in_array; 32*43322877SGreg Roachuse function redirect; 33*43322877SGreg Roach 34*43322877SGreg Roach/** 35*43322877SGreg Roach * Change the members of a family. 36*43322877SGreg Roach */ 37*43322877SGreg Roachclass ChangeFamilyMembersAction implements RequestHandlerInterface 38*43322877SGreg Roach{ 39*43322877SGreg Roach /** 40*43322877SGreg Roach * @param ServerRequestInterface $request 41*43322877SGreg Roach * 42*43322877SGreg Roach * @return ResponseInterface 43*43322877SGreg Roach */ 44*43322877SGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 45*43322877SGreg Roach { 46*43322877SGreg Roach $tree = $request->getAttribute('tree'); 47*43322877SGreg Roach assert($tree instanceof Tree); 48*43322877SGreg Roach 49*43322877SGreg Roach $params = (array) $request->getParsedBody(); 50*43322877SGreg Roach 51*43322877SGreg Roach $xref = $params['xref']; 52*43322877SGreg Roach $family = Factory::family()->make($xref, $tree); 53*43322877SGreg Roach $family = Auth::checkFamilyAccess($family, true); 54*43322877SGreg Roach 55*43322877SGreg Roach $params = (array) $request->getParsedBody(); 56*43322877SGreg Roach 57*43322877SGreg Roach $HUSB = $params['HUSB'] ?? ''; 58*43322877SGreg Roach $WIFE = $params['WIFE'] ?? ''; 59*43322877SGreg Roach $CHIL = $params['CHIL'] ?? []; 60*43322877SGreg Roach 61*43322877SGreg Roach // Current family members 62*43322877SGreg Roach $old_father = $family->husband(); 63*43322877SGreg Roach $old_mother = $family->wife(); 64*43322877SGreg Roach $old_children = $family->children(); 65*43322877SGreg Roach 66*43322877SGreg Roach // New family members 67*43322877SGreg Roach $new_father = Factory::individual()->make($HUSB, $tree); 68*43322877SGreg Roach $new_mother = Factory::individual()->make($WIFE, $tree); 69*43322877SGreg Roach $new_children = []; 70*43322877SGreg Roach foreach ($CHIL as $child) { 71*43322877SGreg Roach $new_children[] = Factory::individual()->make($child, $tree); 72*43322877SGreg Roach } 73*43322877SGreg Roach 74*43322877SGreg Roach if ($old_father !== $new_father) { 75*43322877SGreg Roach if ($old_father instanceof Individual) { 76*43322877SGreg Roach // Remove old FAMS link 77*43322877SGreg Roach foreach ($old_father->facts(['FAMS']) as $fact) { 78*43322877SGreg Roach if ($fact->target() === $family) { 79*43322877SGreg Roach $old_father->deleteFact($fact->id(), true); 80*43322877SGreg Roach } 81*43322877SGreg Roach } 82*43322877SGreg Roach // Remove old HUSB link 83*43322877SGreg Roach foreach ($family->facts(['HUSB', 'WIFE']) as $fact) { 84*43322877SGreg Roach if ($fact->target() === $old_father) { 85*43322877SGreg Roach $family->deleteFact($fact->id(), true); 86*43322877SGreg Roach } 87*43322877SGreg Roach } 88*43322877SGreg Roach } 89*43322877SGreg Roach if ($new_father instanceof Individual) { 90*43322877SGreg Roach // Add new FAMS link 91*43322877SGreg Roach $new_father->createFact('1 FAMS @' . $family->xref() . '@', true); 92*43322877SGreg Roach // Add new HUSB link 93*43322877SGreg Roach $family->createFact('1 HUSB @' . $new_father->xref() . '@', true); 94*43322877SGreg Roach } 95*43322877SGreg Roach } 96*43322877SGreg Roach 97*43322877SGreg Roach if ($old_mother !== $new_mother) { 98*43322877SGreg Roach if ($old_mother instanceof Individual) { 99*43322877SGreg Roach // Remove old FAMS link 100*43322877SGreg Roach foreach ($old_mother->facts(['FAMS']) as $fact) { 101*43322877SGreg Roach if ($fact->target() === $family) { 102*43322877SGreg Roach $old_mother->deleteFact($fact->id(), true); 103*43322877SGreg Roach } 104*43322877SGreg Roach } 105*43322877SGreg Roach // Remove old WIFE link 106*43322877SGreg Roach foreach ($family->facts(['HUSB', 'WIFE']) as $fact) { 107*43322877SGreg Roach if ($fact->target() === $old_mother) { 108*43322877SGreg Roach $family->deleteFact($fact->id(), true); 109*43322877SGreg Roach } 110*43322877SGreg Roach } 111*43322877SGreg Roach } 112*43322877SGreg Roach if ($new_mother instanceof Individual) { 113*43322877SGreg Roach // Add new FAMS link 114*43322877SGreg Roach $new_mother->createFact('1 FAMS @' . $family->xref() . '@', true); 115*43322877SGreg Roach // Add new WIFE link 116*43322877SGreg Roach $family->createFact('1 WIFE @' . $new_mother->xref() . '@', true); 117*43322877SGreg Roach } 118*43322877SGreg Roach } 119*43322877SGreg Roach 120*43322877SGreg Roach foreach ($old_children as $old_child) { 121*43322877SGreg Roach if (!in_array($old_child, $new_children, true)) { 122*43322877SGreg Roach // Remove old FAMC link 123*43322877SGreg Roach foreach ($old_child->facts(['FAMC']) as $fact) { 124*43322877SGreg Roach if ($fact->target() === $family) { 125*43322877SGreg Roach $old_child->deleteFact($fact->id(), true); 126*43322877SGreg Roach } 127*43322877SGreg Roach } 128*43322877SGreg Roach // Remove old CHIL link 129*43322877SGreg Roach foreach ($family->facts(['CHIL']) as $fact) { 130*43322877SGreg Roach if ($fact->target() === $old_child) { 131*43322877SGreg Roach $family->deleteFact($fact->id(), true); 132*43322877SGreg Roach } 133*43322877SGreg Roach } 134*43322877SGreg Roach } 135*43322877SGreg Roach } 136*43322877SGreg Roach 137*43322877SGreg Roach foreach ($new_children as $new_child) { 138*43322877SGreg Roach if ($new_child instanceof Individual && !$old_children->contains($new_child)) { 139*43322877SGreg Roach // Add new FAMC link 140*43322877SGreg Roach $new_child->createFact('1 FAMC @' . $family->xref() . '@', true); 141*43322877SGreg Roach // Add new CHIL link 142*43322877SGreg Roach $family->createFact('1 CHIL @' . $new_child->xref() . '@', true); 143*43322877SGreg Roach } 144*43322877SGreg Roach } 145*43322877SGreg Roach 146*43322877SGreg Roach return redirect($family->url()); 147*43322877SGreg Roach } 148*43322877SGreg Roach} 149