xref: /webtrees/app/Http/RequestHandlers/SourcePage.php (revision 6b9cb339562c8aa4681c7eff2bf3bdf401cc9edd)
154186344SGreg Roach<?php
254186344SGreg Roach
354186344SGreg Roach/**
454186344SGreg Roach * webtrees: online genealogy
5a091ac74SGreg Roach * Copyright (C) 2020 webtrees development team
654186344SGreg Roach * This program is free software: you can redistribute it and/or modify
754186344SGreg Roach * it under the terms of the GNU General Public License as published by
854186344SGreg Roach * the Free Software Foundation, either version 3 of the License, or
954186344SGreg Roach * (at your option) any later version.
1054186344SGreg Roach * This program is distributed in the hope that it will be useful,
1154186344SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
1254186344SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1354186344SGreg Roach * GNU General Public License for more details.
1454186344SGreg Roach * You should have received a copy of the GNU General Public License
1554186344SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
1654186344SGreg Roach */
1754186344SGreg Roach
1854186344SGreg Roachdeclare(strict_types=1);
1954186344SGreg Roach
2054186344SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
2154186344SGreg Roach
22e5d858f5SGreg Roachuse Fig\Http\Message\StatusCodeInterface;
2354186344SGreg Roachuse Fisharebest\Webtrees\Auth;
2454186344SGreg Roachuse Fisharebest\Webtrees\Fact;
2554186344SGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
26*6b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
2754186344SGreg Roachuse Fisharebest\Webtrees\Services\ClipboardService;
2854186344SGreg Roachuse Fisharebest\Webtrees\Source;
2954186344SGreg Roachuse Fisharebest\Webtrees\Tree;
3054186344SGreg Roachuse Illuminate\Support\Collection;
3154186344SGreg Roachuse Psr\Http\Message\ResponseInterface;
3254186344SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
3354186344SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
3454186344SGreg Roach
3554186344SGreg Roachuse function array_search;
3654186344SGreg Roachuse function assert;
3754186344SGreg Roachuse function is_string;
3854186344SGreg Roachuse function redirect;
3954186344SGreg Roach
4054186344SGreg Roachuse const PHP_INT_MAX;
4154186344SGreg Roach
4254186344SGreg Roach/**
4354186344SGreg Roach * Show a source's page.
4454186344SGreg Roach */
4554186344SGreg Roachclass SourcePage implements RequestHandlerInterface
4654186344SGreg Roach{
4754186344SGreg Roach    use ViewResponseTrait;
4854186344SGreg Roach
4954186344SGreg Roach    // Show the source's facts in this order:
5054186344SGreg Roach    private const FACT_ORDER = [
51870b663fSGreg Roach        1 => 'SOUR:TITL',
52870b663fSGreg Roach        'SOUR:ABBR',
53870b663fSGreg Roach        'SOUR:AUTH',
54870b663fSGreg Roach        'SOUR:DATA',
55870b663fSGreg Roach        'SOUR:PUBL',
56870b663fSGreg Roach        'SOUR:TEXT',
57870b663fSGreg Roach        'SOUR:REPO',
58870b663fSGreg Roach        'SOUR:NOTE',
59870b663fSGreg Roach        'SOUR:OBJE',
60870b663fSGreg Roach        'SOUR:REFN',
61870b663fSGreg Roach        'SOUR:RIN',
62870b663fSGreg Roach        'SOUR:_UID',
63870b663fSGreg Roach        'SOUR:CHAN',
64870b663fSGreg Roach        'SOUR:RESN',
6554186344SGreg Roach    ];
6654186344SGreg Roach
6754186344SGreg Roach    /** @var ClipboardService */
6854186344SGreg Roach    private $clipboard_service;
6954186344SGreg Roach
7054186344SGreg Roach    /**
7154186344SGreg Roach     * SourcePage constructor.
7254186344SGreg Roach     *
7354186344SGreg Roach     * @param ClipboardService $clipboard_service
7454186344SGreg Roach     */
7554186344SGreg Roach    public function __construct(ClipboardService $clipboard_service)
7654186344SGreg Roach    {
7754186344SGreg Roach        $this->clipboard_service = $clipboard_service;
7854186344SGreg Roach    }
7954186344SGreg Roach
8054186344SGreg Roach    /**
8154186344SGreg Roach     * @param ServerRequestInterface $request
8254186344SGreg Roach     *
8354186344SGreg Roach     * @return ResponseInterface
8454186344SGreg Roach     */
8554186344SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
8654186344SGreg Roach    {
8754186344SGreg Roach        $tree = $request->getAttribute('tree');
8854186344SGreg Roach        assert($tree instanceof Tree);
8954186344SGreg Roach
9054186344SGreg Roach        $xref = $request->getAttribute('xref');
9154186344SGreg Roach        assert(is_string($xref));
9254186344SGreg Roach
93*6b9cb339SGreg Roach        $source = Registry::sourceFactory()->make($xref, $tree);
9454186344SGreg Roach        $source = Auth::checkSourceAccess($source, false);
9554186344SGreg Roach
9654186344SGreg Roach        // Redirect to correct xref/slug
9754186344SGreg Roach        if ($source->xref() !== $xref || $request->getAttribute('slug') !== $source->slug()) {
98e5d858f5SGreg Roach            return redirect($source->url(), StatusCodeInterface::STATUS_MOVED_PERMANENTLY);
9954186344SGreg Roach        }
10054186344SGreg Roach
10154186344SGreg Roach        return $this->viewResponse('source-page', [
10254186344SGreg Roach            'clipboard_facts'  => $this->clipboard_service->pastableFacts($source, new Collection()),
10354186344SGreg Roach            'facts'            => $this->facts($source),
10454186344SGreg Roach            'families'         => $source->linkedFamilies('SOUR'),
10554186344SGreg Roach            'individuals'      => $source->linkedIndividuals('SOUR'),
1062406e0e0SGreg Roach            'meta_description' => '',
10754186344SGreg Roach            'meta_robots'      => 'index,follow',
10854186344SGreg Roach            'notes'            => $source->linkedNotes('SOUR'),
10954186344SGreg Roach            'media_objects'    => $source->linkedMedia('SOUR'),
11054186344SGreg Roach            'source'           => $source,
11154186344SGreg Roach            'title'            => $source->fullName(),
11254186344SGreg Roach            'tree'             => $tree,
11354186344SGreg Roach        ]);
11454186344SGreg Roach    }
11554186344SGreg Roach
11654186344SGreg Roach    /**
11754186344SGreg Roach     * @param Source $record
11854186344SGreg Roach     *
119b5c8fd7eSGreg Roach     * @return Collection<Fact>
12054186344SGreg Roach     */
12154186344SGreg Roach    private function facts(Source $record): Collection
12254186344SGreg Roach    {
12354186344SGreg Roach        return $record->facts()
12454186344SGreg Roach            ->sort(static function (Fact $x, Fact $y): int {
125870b663fSGreg Roach                $sort_x = array_search($x->tag(), self::FACT_ORDER, true) ?: PHP_INT_MAX;
126870b663fSGreg Roach                $sort_y = array_search($y->tag(), self::FACT_ORDER, true) ?: PHP_INT_MAX;
12754186344SGreg Roach
12854186344SGreg Roach                return $sort_x <=> $sort_y;
12954186344SGreg Roach            });
13054186344SGreg Roach    }
13154186344SGreg Roach}
132