xref: /webtrees/app/Http/RequestHandlers/SourcePage.php (revision 0f5fd22fb1857ad87285e5357592434d47b1f3bf)
154186344SGreg Roach<?php
254186344SGreg Roach
354186344SGreg Roach/**
454186344SGreg Roach * webtrees: online genealogy
589f7189bSGreg Roach * Copyright (C) 2021 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
1589f7189bSGreg Roach * along with this program. If not, see <https://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\Http\ViewResponseTrait;
256b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
2654186344SGreg Roachuse Fisharebest\Webtrees\Services\ClipboardService;
2754186344SGreg Roachuse Fisharebest\Webtrees\Tree;
2854186344SGreg Roachuse Psr\Http\Message\ResponseInterface;
2954186344SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
3054186344SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
3154186344SGreg Roach
3254186344SGreg Roachuse function assert;
3354186344SGreg Roachuse function is_string;
3454186344SGreg Roachuse function redirect;
3554186344SGreg Roach
3654186344SGreg Roach/**
3754186344SGreg Roach * Show a source's page.
3854186344SGreg Roach */
3954186344SGreg Roachclass SourcePage implements RequestHandlerInterface
4054186344SGreg Roach{
4154186344SGreg Roach    use ViewResponseTrait;
4254186344SGreg Roach
43c4943cffSGreg Roach    private ClipboardService $clipboard_service;
4454186344SGreg Roach
4554186344SGreg Roach    /**
4654186344SGreg Roach     * SourcePage constructor.
4754186344SGreg Roach     *
4854186344SGreg Roach     * @param ClipboardService $clipboard_service
4954186344SGreg Roach     */
5054186344SGreg Roach    public function __construct(ClipboardService $clipboard_service)
5154186344SGreg Roach    {
5254186344SGreg Roach        $this->clipboard_service = $clipboard_service;
5354186344SGreg Roach    }
5454186344SGreg Roach
5554186344SGreg Roach    /**
5654186344SGreg Roach     * @param ServerRequestInterface $request
5754186344SGreg Roach     *
5854186344SGreg Roach     * @return ResponseInterface
5954186344SGreg Roach     */
6054186344SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
6154186344SGreg Roach    {
6254186344SGreg Roach        $tree = $request->getAttribute('tree');
6354186344SGreg Roach        assert($tree instanceof Tree);
6454186344SGreg Roach
6554186344SGreg Roach        $xref = $request->getAttribute('xref');
6654186344SGreg Roach        assert(is_string($xref));
6754186344SGreg Roach
68*0f5fd22fSGreg Roach        $record = Registry::sourceFactory()->make($xref, $tree);
69*0f5fd22fSGreg Roach        $record = Auth::checkSourceAccess($record, false);
7054186344SGreg Roach
7154186344SGreg Roach        // Redirect to correct xref/slug
72*0f5fd22fSGreg Roach        $slug = Registry::slugFactory()->make($record);
73194b0938SGreg Roach
74*0f5fd22fSGreg Roach        if ($record->xref() !== $xref || $request->getAttribute('slug') !== $slug) {
75*0f5fd22fSGreg Roach            return redirect($record->url(), StatusCodeInterface::STATUS_MOVED_PERMANENTLY);
7654186344SGreg Roach        }
7754186344SGreg Roach
78*0f5fd22fSGreg Roach        return $this->viewResponse('record-page', [
79*0f5fd22fSGreg Roach            'clipboard_facts'      => $this->clipboard_service->pastableFacts($record),
80*0f5fd22fSGreg Roach            'linked_families'      => $record->linkedFamilies('SOUR'),
81*0f5fd22fSGreg Roach            'linked_individuals'   => $record->linkedIndividuals('SOUR'),
82*0f5fd22fSGreg Roach            'linked_media_objects' => $record->linkedMedia('SOUR'),
83*0f5fd22fSGreg Roach            'linked_notes'         => $record->linkedNotes('SOUR'),
84*0f5fd22fSGreg Roach            'linked_sources'       => null,
852406e0e0SGreg Roach            'meta_description'     => '',
8654186344SGreg Roach            'meta_robots'          => 'index,follow',
87*0f5fd22fSGreg Roach            'record'               => $record,
88*0f5fd22fSGreg Roach            'title'                => $record->fullName(),
8954186344SGreg Roach            'tree'                 => $tree,
9054186344SGreg Roach        ]);
9154186344SGreg Roach    }
9254186344SGreg Roach}
93