xref: /webtrees/app/Http/RequestHandlers/SubmitterPage.php (revision 870b663fa3f7d3552f29fec0a1f1dcf1002a2547)
1ffc0a61fSGreg Roach<?php
2ffc0a61fSGreg Roach
3ffc0a61fSGreg Roach/**
4ffc0a61fSGreg Roach * webtrees: online genealogy
5a091ac74SGreg 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;
25a091ac74SGreg 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 = [
50*870b663fSGreg Roach        1 => 'SUBM:NAME',
51*870b663fSGreg Roach        'SUBM:ADDR',
52*870b663fSGreg Roach        'SUBM:PHON',
53*870b663fSGreg Roach        'SUBM:EMAIL',
54*870b663fSGreg Roach        'SUBM:WWW',
55*870b663fSGreg Roach        'SUBM:LANG',
56*870b663fSGreg Roach        'SUBM:OBJE',
57*870b663fSGreg Roach        'SUBM:RFN',
58*870b663fSGreg Roach        'SUBM:RIN',
59*870b663fSGreg Roach        'SUBM:NOTE',
60*870b663fSGreg 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
76a091ac74SGreg 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 {
105*870b663fSGreg Roach                $sort_x = array_search($x->tag(), self::FACT_ORDER, true) ?: PHP_INT_MAX;
106*870b663fSGreg Roach                $sort_y = array_search($y->tag(), self::FACT_ORDER, true) ?: PHP_INT_MAX;
107ffc0a61fSGreg Roach
108ffc0a61fSGreg Roach                return $sort_x <=> $sort_y;
109ffc0a61fSGreg Roach            });
110ffc0a61fSGreg Roach    }
111ffc0a61fSGreg Roach}
112