1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2020 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 <http://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Http\RequestHandlers; 21 22use Fisharebest\Webtrees\Auth; 23use Fisharebest\Webtrees\Factory; 24use Fisharebest\Webtrees\Individual; 25use Fisharebest\Webtrees\Tree; 26use Psr\Http\Message\ResponseInterface; 27use Psr\Http\Message\ServerRequestInterface; 28use Psr\Http\Server\RequestHandlerInterface; 29 30use function assert; 31use function in_array; 32use function redirect; 33 34/** 35 * Change the members of a family. 36 */ 37class ChangeFamilyMembersAction implements RequestHandlerInterface 38{ 39 /** 40 * @param ServerRequestInterface $request 41 * 42 * @return ResponseInterface 43 */ 44 public function handle(ServerRequestInterface $request): ResponseInterface 45 { 46 $tree = $request->getAttribute('tree'); 47 assert($tree instanceof Tree); 48 49 $params = (array) $request->getParsedBody(); 50 51 $xref = $params['xref']; 52 $family = Factory::family()->make($xref, $tree); 53 $family = Auth::checkFamilyAccess($family, true); 54 55 $params = (array) $request->getParsedBody(); 56 57 $HUSB = $params['HUSB'] ?? ''; 58 $WIFE = $params['WIFE'] ?? ''; 59 $CHIL = $params['CHIL'] ?? []; 60 61 // Current family members 62 $old_father = $family->husband(); 63 $old_mother = $family->wife(); 64 $old_children = $family->children(); 65 66 // New family members 67 $new_father = Factory::individual()->make($HUSB, $tree); 68 $new_mother = Factory::individual()->make($WIFE, $tree); 69 $new_children = []; 70 foreach ($CHIL as $child) { 71 $new_children[] = Factory::individual()->make($child, $tree); 72 } 73 74 if ($old_father !== $new_father) { 75 if ($old_father instanceof Individual) { 76 // Remove old FAMS link 77 foreach ($old_father->facts(['FAMS']) as $fact) { 78 if ($fact->target() === $family) { 79 $old_father->deleteFact($fact->id(), true); 80 } 81 } 82 // Remove old HUSB link 83 foreach ($family->facts(['HUSB', 'WIFE']) as $fact) { 84 if ($fact->target() === $old_father) { 85 $family->deleteFact($fact->id(), true); 86 } 87 } 88 } 89 if ($new_father instanceof Individual) { 90 // Add new FAMS link 91 $new_father->createFact('1 FAMS @' . $family->xref() . '@', true); 92 // Add new HUSB link 93 $family->createFact('1 HUSB @' . $new_father->xref() . '@', true); 94 } 95 } 96 97 if ($old_mother !== $new_mother) { 98 if ($old_mother instanceof Individual) { 99 // Remove old FAMS link 100 foreach ($old_mother->facts(['FAMS']) as $fact) { 101 if ($fact->target() === $family) { 102 $old_mother->deleteFact($fact->id(), true); 103 } 104 } 105 // Remove old WIFE link 106 foreach ($family->facts(['HUSB', 'WIFE']) as $fact) { 107 if ($fact->target() === $old_mother) { 108 $family->deleteFact($fact->id(), true); 109 } 110 } 111 } 112 if ($new_mother instanceof Individual) { 113 // Add new FAMS link 114 $new_mother->createFact('1 FAMS @' . $family->xref() . '@', true); 115 // Add new WIFE link 116 $family->createFact('1 WIFE @' . $new_mother->xref() . '@', true); 117 } 118 } 119 120 foreach ($old_children as $old_child) { 121 if (!in_array($old_child, $new_children, true)) { 122 // Remove old FAMC link 123 foreach ($old_child->facts(['FAMC']) as $fact) { 124 if ($fact->target() === $family) { 125 $old_child->deleteFact($fact->id(), true); 126 } 127 } 128 // Remove old CHIL link 129 foreach ($family->facts(['CHIL']) as $fact) { 130 if ($fact->target() === $old_child) { 131 $family->deleteFact($fact->id(), true); 132 } 133 } 134 } 135 } 136 137 foreach ($new_children as $new_child) { 138 if ($new_child instanceof Individual && !$old_children->contains($new_child)) { 139 // Add new FAMC link 140 $new_child->createFact('1 FAMC @' . $family->xref() . '@', true); 141 // Add new CHIL link 142 $family->createFact('1 CHIL @' . $new_child->xref() . '@', true); 143 } 144 } 145 146 return redirect($family->url()); 147 } 148} 149