xref: /webtrees/app/Http/RequestHandlers/ReorderNamesAction.php (revision b46c87bda4b592cf9252f1db48552a820b1e3d97)
15229eadeSGreg Roach<?php
25229eadeSGreg Roach
35229eadeSGreg Roach/**
45229eadeSGreg Roach * webtrees: online genealogy
55229eadeSGreg Roach * Copyright (C) 2019 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
155229eadeSGreg Roach * along with this program. If not, see <http://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;
235229eadeSGreg Roachuse Fisharebest\Webtrees\Individual;
245229eadeSGreg Roachuse Fisharebest\Webtrees\Tree;
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 assert;
325229eadeSGreg Roachuse function implode;
335229eadeSGreg Roachuse function is_array;
345229eadeSGreg Roachuse function is_string;
355229eadeSGreg Roachuse function redirect;
365229eadeSGreg Roachuse function uksort;
375229eadeSGreg Roach
385229eadeSGreg Roach/**
395229eadeSGreg Roach * Reorder the names of an individual.
405229eadeSGreg Roach */
415229eadeSGreg Roachclass ReorderNamesAction implements RequestHandlerInterface
425229eadeSGreg Roach{
435229eadeSGreg Roach    /**
445229eadeSGreg Roach     * @param ServerRequestInterface $request
455229eadeSGreg Roach     *
465229eadeSGreg Roach     * @return ResponseInterface
475229eadeSGreg Roach     */
485229eadeSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
495229eadeSGreg Roach    {
505229eadeSGreg Roach        $tree = $request->getAttribute('tree');
5175964c75SGreg Roach        assert($tree instanceof Tree);
525229eadeSGreg Roach
53745148ddSGreg Roach        $xref = $request->getAttribute('xref');
5475964c75SGreg Roach        assert(is_string($xref));
555229eadeSGreg Roach
565229eadeSGreg Roach        $individual = Individual::getInstance($xref, $tree);
57ddeb3354SGreg Roach        $individual = Auth::checkIndividualAccess($individual, true);
585229eadeSGreg Roach
59*b46c87bdSGreg Roach        $params = (array) $request->getParsedBody();
60*b46c87bdSGreg Roach        $order  = $params['order'];
6175964c75SGreg Roach        assert(is_array($order));
625229eadeSGreg Roach
635229eadeSGreg Roach        $dummy_facts = ['0 @' . $individual->xref() . '@ INDI'];
645229eadeSGreg Roach        $sort_facts  = [];
655229eadeSGreg Roach        $keep_facts  = [];
665229eadeSGreg Roach
675229eadeSGreg Roach        // Split facts into NAME and other
685229eadeSGreg Roach        foreach ($individual->facts() as $fact) {
695229eadeSGreg Roach            if ($fact->getTag() === 'NAME') {
705229eadeSGreg Roach                $sort_facts[$fact->id()] = $fact->gedcom();
715229eadeSGreg Roach            } else {
725229eadeSGreg Roach                $keep_facts[] = $fact->gedcom();
735229eadeSGreg Roach            }
745229eadeSGreg Roach        }
755229eadeSGreg Roach
765229eadeSGreg Roach        // Sort the facts
7766c7498fSGreg Roach        uksort($sort_facts, static function (string $x, string $y) use ($order): int {
785229eadeSGreg Roach            return array_search($x, $order, true) - array_search($y, $order, true);
795229eadeSGreg Roach        });
805229eadeSGreg Roach
815229eadeSGreg Roach        // Merge the facts
825229eadeSGreg Roach        $gedcom = implode("\n", array_merge($dummy_facts, $sort_facts, $keep_facts));
835229eadeSGreg Roach
845229eadeSGreg Roach        $individual->updateRecord($gedcom, false);
855229eadeSGreg Roach
865229eadeSGreg Roach        return redirect($individual->url());
875229eadeSGreg Roach    }
885229eadeSGreg Roach}
89