xref: /webtrees/app/Http/RequestHandlers/SubmitterPage.php (revision a091ac74647eab281b25090b737835eeea14ae10)
1ffc0a61fSGreg Roach<?php
2ffc0a61fSGreg Roach
3ffc0a61fSGreg Roach/**
4ffc0a61fSGreg Roach * webtrees: online genealogy
5*a091ac74SGreg Roach * Copyright (C) 2020 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
15ffc0a61fSGreg Roach * along with this program. If not, see <http://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;
25*a091ac74SGreg Roachuse Fisharebest\Webtrees\Factory;
26ffc0a61fSGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
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 = [
50ffc0a61fSGreg Roach        1 => 'NAME',
51ffc0a61fSGreg Roach        'ADDR',
52ffc0a61fSGreg Roach        'PHON',
53ffc0a61fSGreg Roach        'EMAIL',
54ffc0a61fSGreg Roach        'WWW',
55ffc0a61fSGreg Roach        'LANG',
56ffc0a61fSGreg Roach        'OBJE',
57ffc0a61fSGreg Roach        'RFN',
58ffc0a61fSGreg Roach        'RIN',
59ffc0a61fSGreg Roach        'NOTE',
60ffc0a61fSGreg Roach        '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
76*a091ac74SGreg Roach        $submitter = Factory::submitter()->make($xref, $tree);
77ffc0a61fSGreg Roach        $submitter = Auth::checkSubmitterAccess($submitter, false);
78ffc0a61fSGreg Roach
79ffc0a61fSGreg Roach        // Redirect to correct xref/slug
80ffc0a61fSGreg Roach        if ($submitter->xref() !== $xref || $request->getAttribute('slug') !== $submitter->slug()) {
818256f465SGreg Roach            return redirect($submitter->url(), StatusCodeInterface::STATUS_MOVED_PERMANENTLY);
82ffc0a61fSGreg Roach        }
83ffc0a61fSGreg Roach
84ffc0a61fSGreg Roach        return $this->viewResponse('submitter-page', [
85ffc0a61fSGreg Roach            'facts'            => $this->facts($submitter),
86ffc0a61fSGreg Roach            'submitter'        => $submitter,
87ffc0a61fSGreg Roach            'families'         => $submitter->linkedFamilies('SUBM'),
88ffc0a61fSGreg Roach            'individuals'      => $submitter->linkedIndividuals('SUBM'),
892406e0e0SGreg Roach            'meta_description' => '',
902406e0e0SGreg Roach            'meta_robots'      => 'index,follow',
91ffc0a61fSGreg Roach            'title'            => $submitter->fullName(),
92ffc0a61fSGreg Roach            'tree'             => $tree,
93ffc0a61fSGreg Roach        ]);
94ffc0a61fSGreg Roach    }
95ffc0a61fSGreg Roach
96ffc0a61fSGreg Roach    /**
97ffc0a61fSGreg Roach     * @param Submitter $record
98ffc0a61fSGreg Roach     *
99ffc0a61fSGreg Roach     * @return Collection<Fact>
100ffc0a61fSGreg Roach     */
101ffc0a61fSGreg Roach    private function facts(Submitter $record): Collection
102ffc0a61fSGreg Roach    {
103ffc0a61fSGreg Roach        return $record->facts()
104ffc0a61fSGreg Roach            ->sort(static function (Fact $x, Fact $y): int {
105ffc0a61fSGreg Roach                $sort_x = array_search($x->getTag(), self::FACT_ORDER, true) ?: PHP_INT_MAX;
106ffc0a61fSGreg Roach                $sort_y = array_search($y->getTag(), self::FACT_ORDER, true) ?: PHP_INT_MAX;
107ffc0a61fSGreg Roach
108ffc0a61fSGreg Roach                return $sort_x <=> $sort_y;
109ffc0a61fSGreg Roach            });
110ffc0a61fSGreg Roach    }
111ffc0a61fSGreg Roach}
112