11afbbc50SGreg Roach<?php 21afbbc50SGreg Roach 31afbbc50SGreg Roach/** 41afbbc50SGreg Roach * webtrees: online genealogy 5*d11be702SGreg Roach * Copyright (C) 2023 webtrees development team 61afbbc50SGreg Roach * This program is free software: you can redistribute it and/or modify 71afbbc50SGreg Roach * it under the terms of the GNU General Public License as published by 81afbbc50SGreg Roach * the Free Software Foundation, either version 3 of the License, or 91afbbc50SGreg Roach * (at your option) any later version. 101afbbc50SGreg Roach * This program is distributed in the hope that it will be useful, 111afbbc50SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 121afbbc50SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 131afbbc50SGreg Roach * GNU General Public License for more details. 141afbbc50SGreg 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/>. 161afbbc50SGreg Roach */ 171afbbc50SGreg Roach 181afbbc50SGreg Roachdeclare(strict_types=1); 191afbbc50SGreg Roach 201afbbc50SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers; 211afbbc50SGreg Roach 221afbbc50SGreg Roachuse Fisharebest\Webtrees\Auth; 236b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry; 24b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator; 251afbbc50SGreg Roachuse Psr\Http\Message\ResponseInterface; 261afbbc50SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 271afbbc50SGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 281afbbc50SGreg Roach 291afbbc50SGreg Roachuse function array_merge; 301afbbc50SGreg Roachuse function array_search; 311afbbc50SGreg Roachuse function implode; 321afbbc50SGreg Roachuse function redirect; 331afbbc50SGreg Roachuse function uksort; 341afbbc50SGreg Roach 351afbbc50SGreg Roach/** 361afbbc50SGreg Roach * Reorder the parents and/or spouses of an individual. 371afbbc50SGreg Roach */ 381afbbc50SGreg Roachclass ReorderFamiliesAction implements RequestHandlerInterface 391afbbc50SGreg Roach{ 401afbbc50SGreg Roach /** 411afbbc50SGreg Roach * @param ServerRequestInterface $request 421afbbc50SGreg Roach * 431afbbc50SGreg Roach * @return ResponseInterface 441afbbc50SGreg Roach */ 451afbbc50SGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 461afbbc50SGreg Roach { 47b55cbc6bSGreg Roach $tree = Validator::attributes($request)->tree(); 48b55cbc6bSGreg Roach $xref = Validator::attributes($request)->isXref()->string('xref'); 49748dbe15SGreg Roach $order = Validator::parsedBody($request)->array('order'); 50748dbe15SGreg Roach 516b9cb339SGreg Roach $individual = Registry::individualFactory()->make($xref, $tree); 521afbbc50SGreg Roach $individual = Auth::checkIndividualAccess($individual, true); 531afbbc50SGreg Roach 541f244d77SGreg Roach $fake_facts = ['0 @' . $individual->xref() . '@ INDI']; 551afbbc50SGreg Roach $sort_facts = []; 561afbbc50SGreg Roach $keep_facts = []; 571afbbc50SGreg Roach 581afbbc50SGreg Roach // Split facts into FAMS and other 591afbbc50SGreg Roach foreach ($individual->facts() as $fact) { 60d8826850SGreg Roach $tag = $fact->tag(); 611afbbc50SGreg Roach 62d8826850SGreg Roach if ($tag === 'INDI:FAMC' || $tag === 'INDI:FAMS') { 631afbbc50SGreg Roach $sort_facts[$fact->id()] = $fact->gedcom(); 641afbbc50SGreg Roach } else { 651afbbc50SGreg Roach $keep_facts[] = $fact->gedcom(); 661afbbc50SGreg Roach } 671afbbc50SGreg Roach } 681afbbc50SGreg Roach 691afbbc50SGreg Roach // Sort the facts 704c78e066SGreg Roach $callback = static fn (string $x, string $y): int => array_search($x, $order, true) <=> array_search($y, $order, true); 714c78e066SGreg Roach uksort($sort_facts, $callback); 721afbbc50SGreg Roach 731afbbc50SGreg Roach // Merge the facts 741f244d77SGreg Roach $gedcom = implode("\n", array_merge($fake_facts, $sort_facts, $keep_facts)); 751afbbc50SGreg Roach 761afbbc50SGreg Roach $individual->updateRecord($gedcom, false); 771afbbc50SGreg Roach 781afbbc50SGreg Roach return redirect($individual->url()); 791afbbc50SGreg Roach } 801afbbc50SGreg Roach} 81