1d178350fSGreg Roach<?php 2d178350fSGreg Roach 3d178350fSGreg Roach/** 4d178350fSGreg Roach * webtrees: online genealogy 5*d11be702SGreg Roach * Copyright (C) 2023 webtrees development team 6d178350fSGreg Roach * This program is free software: you can redistribute it and/or modify 7d178350fSGreg Roach * it under the terms of the GNU General Public License as published by 8d178350fSGreg Roach * the Free Software Foundation, either version 3 of the License, or 9d178350fSGreg Roach * (at your option) any later version. 10d178350fSGreg Roach * This program is distributed in the hope that it will be useful, 11d178350fSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 12d178350fSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13d178350fSGreg Roach * GNU General Public License for more details. 14d178350fSGreg Roach * You should have received a copy of the GNU General Public License 15d178350fSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>. 16d178350fSGreg Roach */ 17d178350fSGreg Roach 18d178350fSGreg Roachdeclare(strict_types=1); 19d178350fSGreg Roach 20d178350fSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers; 21d178350fSGreg Roach 22d178350fSGreg Roachuse Fisharebest\Webtrees\Auth; 23d178350fSGreg Roachuse Fisharebest\Webtrees\Registry; 24d178350fSGreg Roachuse Fisharebest\Webtrees\Validator; 25d178350fSGreg Roachuse Psr\Http\Message\ResponseInterface; 26d178350fSGreg Roachuse Psr\Http\Message\ServerRequestInterface; 27d178350fSGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 28d178350fSGreg Roach 29d178350fSGreg Roachuse function array_merge; 30d178350fSGreg Roachuse function array_search; 31d178350fSGreg Roachuse function implode; 32d178350fSGreg Roachuse function redirect; 33d178350fSGreg Roachuse function uksort; 34d178350fSGreg Roach 35d178350fSGreg Roach/** 36d178350fSGreg Roach * Reorder the media files of a media object. 37d178350fSGreg Roach */ 38d178350fSGreg Roachclass ReorderMediaFilesAction implements RequestHandlerInterface 39d178350fSGreg Roach{ 40d178350fSGreg Roach /** 41d178350fSGreg Roach * @param ServerRequestInterface $request 42d178350fSGreg Roach * 43d178350fSGreg Roach * @return ResponseInterface 44d178350fSGreg Roach */ 45d178350fSGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 46d178350fSGreg Roach { 47d178350fSGreg Roach $tree = Validator::attributes($request)->tree(); 48d178350fSGreg Roach $xref = Validator::attributes($request)->isXref()->string('xref'); 49748dbe15SGreg Roach $order = Validator::parsedBody($request)->array('order'); 50748dbe15SGreg Roach 51d178350fSGreg Roach $media = Registry::mediaFactory()->make($xref, $tree); 52d178350fSGreg Roach $media = Auth::checkMediaAccess($media, true); 53d178350fSGreg Roach 54d178350fSGreg Roach $fake_facts = ['0 @' . $media->xref() . '@ OBJE']; 55d178350fSGreg Roach $sort_facts = []; 56d178350fSGreg Roach $keep_facts = []; 57d178350fSGreg Roach 58d178350fSGreg Roach // Split facts into OBJE and other 59d178350fSGreg Roach foreach ($media->facts() as $fact) { 60d178350fSGreg Roach if ($fact->tag() === 'OBJE:FILE') { 61d178350fSGreg Roach $sort_facts[$fact->id()] = $fact->gedcom(); 62d178350fSGreg Roach } else { 63d178350fSGreg Roach $keep_facts[] = $fact->gedcom(); 64d178350fSGreg Roach } 65d178350fSGreg Roach } 66d178350fSGreg Roach 67d178350fSGreg Roach // Sort the facts 68d178350fSGreg Roach $callback = static fn (string $x, string $y): int => array_search($x, $order, true) <=> array_search($y, $order, true); 69d178350fSGreg Roach uksort($sort_facts, $callback); 70d178350fSGreg Roach 71d178350fSGreg Roach // Merge the facts 72d178350fSGreg Roach $gedcom = implode("\n", array_merge($fake_facts, $sort_facts, $keep_facts)); 73d178350fSGreg Roach 74d178350fSGreg Roach $media->updateRecord($gedcom, false); 75d178350fSGreg Roach 76d178350fSGreg Roach return redirect($media->url()); 77d178350fSGreg Roach } 78d178350fSGreg Roach} 79