xref: /webtrees/app/Http/RequestHandlers/SubmitterPage.php (revision 194b0938bfa145814c8ade07169d1464fa285e2a)
1ffc0a61fSGreg Roach<?php
2ffc0a61fSGreg Roach
3ffc0a61fSGreg Roach/**
4ffc0a61fSGreg Roach * webtrees: online genealogy
589f7189bSGreg Roach * Copyright (C) 2021 webtrees development team
6ffc0a61fSGreg Roach * This program is free software: you can redistribute it and/or modify
7ffc0a61fSGreg Roach * it under the terms of the GNU General Public License as published by
8ffc0a61fSGreg Roach * the Free Software Foundation, either version 3 of the License, or
9ffc0a61fSGreg Roach * (at your option) any later version.
10ffc0a61fSGreg Roach * This program is distributed in the hope that it will be useful,
11ffc0a61fSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12ffc0a61fSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13ffc0a61fSGreg Roach * GNU General Public License for more details.
14ffc0a61fSGreg 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/>.
16ffc0a61fSGreg Roach */
17ffc0a61fSGreg Roach
18ffc0a61fSGreg Roachdeclare(strict_types=1);
19ffc0a61fSGreg Roach
20ffc0a61fSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
21ffc0a61fSGreg Roach
228256f465SGreg Roachuse Fig\Http\Message\StatusCodeInterface;
23ffc0a61fSGreg Roachuse Fisharebest\Webtrees\Auth;
24ffc0a61fSGreg Roachuse Fisharebest\Webtrees\Fact;
25ffc0a61fSGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
266b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
27ffc0a61fSGreg Roachuse Fisharebest\Webtrees\Submitter;
28ffc0a61fSGreg Roachuse Fisharebest\Webtrees\Tree;
29ffc0a61fSGreg Roachuse Illuminate\Support\Collection;
30ffc0a61fSGreg Roachuse Psr\Http\Message\ResponseInterface;
31ffc0a61fSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
32ffc0a61fSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
33ffc0a61fSGreg Roach
34ffc0a61fSGreg Roachuse function array_search;
35ffc0a61fSGreg Roachuse function assert;
36ffc0a61fSGreg Roachuse function is_string;
37ffc0a61fSGreg Roachuse function redirect;
38ffc0a61fSGreg Roach
39ffc0a61fSGreg Roachuse const PHP_INT_MAX;
40ffc0a61fSGreg Roach
41ffc0a61fSGreg Roach/**
42ffc0a61fSGreg Roach * Show a submitter's page.
43ffc0a61fSGreg Roach */
44ffc0a61fSGreg Roachclass SubmitterPage implements RequestHandlerInterface
45ffc0a61fSGreg Roach{
46ffc0a61fSGreg Roach    use ViewResponseTrait;
47ffc0a61fSGreg Roach
48ffc0a61fSGreg Roach    // Show the submitter's facts in this order:
49ffc0a61fSGreg Roach    private const FACT_ORDER = [
50870b663fSGreg Roach        1 => 'SUBM:NAME',
51870b663fSGreg Roach        'SUBM:ADDR',
52870b663fSGreg Roach        'SUBM:PHON',
53870b663fSGreg Roach        'SUBM:EMAIL',
54870b663fSGreg Roach        'SUBM:WWW',
55870b663fSGreg Roach        'SUBM:LANG',
56870b663fSGreg Roach        'SUBM:OBJE',
57870b663fSGreg Roach        'SUBM:RFN',
58870b663fSGreg Roach        'SUBM:RIN',
59870b663fSGreg Roach        'SUBM:NOTE',
60870b663fSGreg Roach        'SUBM:CHAN',
61ffc0a61fSGreg Roach    ];
62ffc0a61fSGreg Roach
63ffc0a61fSGreg Roach    /**
64ffc0a61fSGreg Roach     * @param ServerRequestInterface $request
65ffc0a61fSGreg Roach     *
66ffc0a61fSGreg Roach     * @return ResponseInterface
67ffc0a61fSGreg Roach     */
68ffc0a61fSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
69ffc0a61fSGreg Roach    {
70ffc0a61fSGreg Roach        $tree = $request->getAttribute('tree');
71ffc0a61fSGreg Roach        assert($tree instanceof Tree);
72ffc0a61fSGreg Roach
73ffc0a61fSGreg Roach        $xref = $request->getAttribute('xref');
74ffc0a61fSGreg Roach        assert(is_string($xref));
75ffc0a61fSGreg Roach
766b9cb339SGreg Roach        $submitter = Registry::submitterFactory()->make($xref, $tree);
77ffc0a61fSGreg Roach        $submitter = Auth::checkSubmitterAccess($submitter, false);
78ffc0a61fSGreg Roach
79ffc0a61fSGreg Roach        // Redirect to correct xref/slug
80*194b0938SGreg Roach        $slug = Registry::slugFactory()->make($submitter);
81*194b0938SGreg Roach
82*194b0938SGreg Roach        if ($submitter->xref() !== $xref || $request->getAttribute('slug') !== $slug) {
838256f465SGreg Roach            return redirect($submitter->url(), StatusCodeInterface::STATUS_MOVED_PERMANENTLY);
84ffc0a61fSGreg Roach        }
85ffc0a61fSGreg Roach
86ffc0a61fSGreg Roach        return $this->viewResponse('submitter-page', [
87ffc0a61fSGreg Roach            'facts'            => $this->facts($submitter),
88ffc0a61fSGreg Roach            'submitter'        => $submitter,
89ffc0a61fSGreg Roach            'families'         => $submitter->linkedFamilies('SUBM'),
90ffc0a61fSGreg Roach            'individuals'      => $submitter->linkedIndividuals('SUBM'),
912406e0e0SGreg Roach            'meta_description' => '',
922406e0e0SGreg Roach            'meta_robots'      => 'index,follow',
93ffc0a61fSGreg Roach            'title'            => $submitter->fullName(),
94ffc0a61fSGreg Roach            'tree'             => $tree,
95ffc0a61fSGreg Roach        ]);
96ffc0a61fSGreg Roach    }
97ffc0a61fSGreg Roach
98ffc0a61fSGreg Roach    /**
99ffc0a61fSGreg Roach     * @param Submitter $record
100ffc0a61fSGreg Roach     *
101ffc0a61fSGreg Roach     * @return Collection<Fact>
102ffc0a61fSGreg Roach     */
103ffc0a61fSGreg Roach    private function facts(Submitter $record): Collection
104ffc0a61fSGreg Roach    {
105ffc0a61fSGreg Roach        return $record->facts()
106ffc0a61fSGreg Roach            ->sort(static function (Fact $x, Fact $y): int {
107870b663fSGreg Roach                $sort_x = array_search($x->tag(), self::FACT_ORDER, true) ?: PHP_INT_MAX;
108870b663fSGreg Roach                $sort_y = array_search($y->tag(), self::FACT_ORDER, true) ?: PHP_INT_MAX;
109ffc0a61fSGreg Roach
110ffc0a61fSGreg Roach                return $sort_x <=> $sort_y;
111ffc0a61fSGreg Roach            });
112ffc0a61fSGreg Roach    }
113ffc0a61fSGreg Roach}
114