15229eadeSGreg Roach<?php 25229eadeSGreg Roach 35229eadeSGreg Roach/** 45229eadeSGreg Roach * webtrees: online genealogy 5*d11be702SGreg Roach * Copyright (C) 2023 webtrees development team 65229eadeSGreg Roach * This program is free software: you can redistribute it and/or modify 75229eadeSGreg Roach * it under the terms of the GNU General Public License as published by 85229eadeSGreg Roach * the Free Software Foundation, either version 3 of the License, or 95229eadeSGreg Roach * (at your option) any later version. 105229eadeSGreg Roach * This program is distributed in the hope that it will be useful, 115229eadeSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 125229eadeSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 135229eadeSGreg Roach * GNU General Public License for more details. 145229eadeSGreg 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/>. 165229eadeSGreg Roach */ 17fcfa147eSGreg Roach 185229eadeSGreg Roachdeclare(strict_types=1); 195229eadeSGreg Roach 205229eadeSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers; 215229eadeSGreg Roach 225229eadeSGreg Roachuse Fisharebest\Webtrees\Auth; 236b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry; 24b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator; 255229eadeSGreg Roachuse Psr\Http\Message\ResponseInterface; 265229eadeSGreg Roachuse Psr\Http\Message\ServerRequestInterface; 275229eadeSGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 285229eadeSGreg Roach 295229eadeSGreg Roachuse function array_merge; 305229eadeSGreg Roachuse function array_search; 315229eadeSGreg Roachuse function implode; 325229eadeSGreg Roachuse function redirect; 335229eadeSGreg Roachuse function uksort; 345229eadeSGreg Roach 355229eadeSGreg Roach/** 365229eadeSGreg Roach * Reorder the children in a family. 375229eadeSGreg Roach */ 385229eadeSGreg Roachclass ReorderChildrenAction implements RequestHandlerInterface 395229eadeSGreg Roach{ 405229eadeSGreg Roach /** 415229eadeSGreg Roach * @param ServerRequestInterface $request 425229eadeSGreg Roach * 435229eadeSGreg Roach * @return ResponseInterface 445229eadeSGreg Roach */ 455229eadeSGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 465229eadeSGreg Roach { 47b55cbc6bSGreg Roach $tree = Validator::attributes($request)->tree(); 48b55cbc6bSGreg Roach $xref = Validator::attributes($request)->isXref()->string('xref'); 49748dbe15SGreg Roach 506b9cb339SGreg Roach $family = Registry::familyFactory()->make($xref, $tree); 51ddeb3354SGreg Roach $family = Auth::checkFamilyAccess($family, true); 525229eadeSGreg Roach 53748dbe15SGreg Roach $order = Validator::parsedBody($request)->array('order'); 54748dbe15SGreg Roach $url = Validator::parsedBody($request)->isLocalUrl()->string('url', $family->url()); 555229eadeSGreg Roach 561f244d77SGreg Roach $fake_facts = ['0 @' . $family->xref() . '@ FAM']; 575229eadeSGreg Roach $sort_facts = []; 585229eadeSGreg Roach $keep_facts = []; 595229eadeSGreg Roach 605229eadeSGreg Roach // Split facts into FAMS and other 615229eadeSGreg Roach foreach ($family->facts() as $fact) { 62d0889c63SGreg Roach if ($fact->tag() === 'FAM:CHIL') { 635229eadeSGreg Roach $sort_facts[$fact->id()] = $fact->gedcom(); 645229eadeSGreg Roach } else { 655229eadeSGreg Roach $keep_facts[] = $fact->gedcom(); 665229eadeSGreg Roach } 675229eadeSGreg Roach } 685229eadeSGreg Roach 695229eadeSGreg 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); 725229eadeSGreg Roach 735229eadeSGreg Roach // Merge the facts 741f244d77SGreg Roach $gedcom = implode("\n", array_merge($fake_facts, $sort_facts, $keep_facts)); 755229eadeSGreg Roach 765229eadeSGreg Roach $family->updateRecord($gedcom, false); 775229eadeSGreg Roach 7850405528SGreg Roach return redirect($url); 795229eadeSGreg Roach } 805229eadeSGreg Roach} 81