xref: /webtrees/app/Http/RequestHandlers/SubmitterPage.php (revision ffc0a61f636c2f449ee87acaba2c9b803ca6e758)
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\Fact;
24use Fisharebest\Webtrees\Http\ViewResponseTrait;
25use Fisharebest\Webtrees\Services\ClipboardService;
26use Fisharebest\Webtrees\Submitter;
27use Fisharebest\Webtrees\Tree;
28use Illuminate\Support\Collection;
29use Psr\Http\Message\ResponseInterface;
30use Psr\Http\Message\ServerRequestInterface;
31use Psr\Http\Server\RequestHandlerInterface;
32
33use function array_search;
34use function assert;
35use function is_string;
36use function redirect;
37
38use const PHP_INT_MAX;
39
40/**
41 * Show a submitter's page.
42 */
43class SubmitterPage implements RequestHandlerInterface
44{
45    use ViewResponseTrait;
46
47    // Show the submitter's facts in this order:
48    private const FACT_ORDER = [
49        1 => 'NAME',
50        'ADDR',
51        'PHON',
52        'EMAIL',
53        'WWW',
54        'LANG',
55        'OBJE',
56        'RFN',
57        'RIN',
58        'NOTE',
59        'CHAN',
60    ];
61
62    /** @var ClipboardService */
63    private $clipboard_service;
64
65    /**
66     * @param ServerRequestInterface $request
67     *
68     * @return ResponseInterface
69     */
70    public function handle(ServerRequestInterface $request): ResponseInterface
71    {
72        $tree = $request->getAttribute('tree');
73        assert($tree instanceof Tree);
74
75        $xref = $request->getAttribute('xref');
76        assert(is_string($xref));
77
78        $submitter = Submitter::getInstance($xref, $tree);
79        $submitter = Auth::checkSubmitterAccess($submitter, false);
80
81        // Redirect to correct xref/slug
82        if ($submitter->xref() !== $xref || $request->getAttribute('slug') !== $submitter->slug()) {
83            return redirect($submitter->url());
84        }
85
86        return $this->viewResponse('submitter-page', [
87            'facts'       => $this->facts($submitter),
88            'submitter'   => $submitter,
89            'families'    => $submitter->linkedFamilies('SUBM'),
90            'individuals' => $submitter->linkedIndividuals('SUBM'),
91            'title'       => $submitter->fullName(),
92            'tree'        => $tree,
93        ]);
94    }
95
96    /**
97     * @param Submitter $record
98     *
99     * @return Collection<Fact>
100     */
101    private function facts(Submitter $record): Collection
102    {
103        return $record->facts()
104            ->sort(static function (Fact $x, Fact $y): int {
105                $sort_x = array_search($x->getTag(), self::FACT_ORDER, true) ?: PHP_INT_MAX;
106                $sort_y = array_search($y->getTag(), self::FACT_ORDER, true) ?: PHP_INT_MAX;
107
108                return $sort_x <=> $sort_y;
109            });
110    }
111}
112