xref: /webtrees/app/Http/RequestHandlers/ReorderNamesAction.php (revision 66ce3d2365f7eb099897303ad7dc49a8459ceb55)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 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\Individual;
24use Fisharebest\Webtrees\Tree;
25use InvalidArgumentException;
26use Psr\Http\Message\ResponseInterface;
27use Psr\Http\Message\ServerRequestInterface;
28use Psr\Http\Server\RequestHandlerInterface;
29
30use function array_merge;
31use function array_search;
32use function assert;
33use function implode;
34use function is_array;
35use function is_string;
36use function redirect;
37use function uksort;
38
39/**
40 * Reorder the names of an individual.
41 */
42class ReorderNamesAction implements RequestHandlerInterface
43{
44    /**
45     * @param ServerRequestInterface $request
46     *
47     * @return ResponseInterface
48     */
49    public function handle(ServerRequestInterface $request): ResponseInterface
50    {
51        $tree = $request->getAttribute('tree');
52        assert($tree instanceof Tree, new InvalidArgumentException());
53
54        $xref = $request->getAttribute('xref');
55        assert(is_string($xref), new InvalidArgumentException());
56
57        $individual = Individual::getInstance($xref, $tree);
58        assert($individual instanceof Individual, new InvalidArgumentException());
59
60        $order = $request->getParsedBody()['order'];
61        assert(is_array($order), new InvalidArgumentException());
62
63        Auth::checkIndividualAccess($individual, true);
64
65        $dummy_facts = ['0 @' . $individual->xref() . '@ INDI'];
66        $sort_facts  = [];
67        $keep_facts  = [];
68
69        // Split facts into NAME and other
70        foreach ($individual->facts() as $fact) {
71            if ($fact->getTag() === 'NAME') {
72                $sort_facts[$fact->id()] = $fact->gedcom();
73            } else {
74                $keep_facts[] = $fact->gedcom();
75            }
76        }
77
78        // Sort the facts
79        uksort($sort_facts, static function ($x, $y) use ($order) {
80            return array_search($x, $order, true) - array_search($y, $order, true);
81        });
82
83        // Merge the facts
84        $gedcom = implode("\n", array_merge($dummy_facts, $sort_facts, $keep_facts));
85
86        $individual->updateRecord($gedcom, false);
87
88        return redirect($individual->url());
89    }
90}
91