xref: /webtrees/app/Http/RequestHandlers/NotePage.php (revision 05fc85fd27092434b30e184e72b2b4df74bb4c34)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22use Fig\Http\Message\StatusCodeInterface;
23use Fisharebest\Webtrees\Auth;
24use Fisharebest\Webtrees\Filter;
25use Fisharebest\Webtrees\Http\ViewResponseTrait;
26use Fisharebest\Webtrees\Registry;
27use Fisharebest\Webtrees\Services\ClipboardService;
28use Fisharebest\Webtrees\Tree;
29use Psr\Http\Message\ResponseInterface;
30use Psr\Http\Message\ServerRequestInterface;
31use Psr\Http\Server\RequestHandlerInterface;
32
33use function assert;
34use function is_string;
35use function redirect;
36
37/**
38 * Show a note's page.
39 */
40class NotePage implements RequestHandlerInterface
41{
42    use ViewResponseTrait;
43
44    private ClipboardService $clipboard_service;
45
46    /**
47     * NotePage constructor.
48     *
49     * @param ClipboardService $clipboard_service
50     */
51    public function __construct(ClipboardService $clipboard_service)
52    {
53        $this->clipboard_service = $clipboard_service;
54    }
55
56    /**
57     * @param ServerRequestInterface $request
58     *
59     * @return ResponseInterface
60     */
61    public function handle(ServerRequestInterface $request): ResponseInterface
62    {
63        $tree = $request->getAttribute('tree');
64        assert($tree instanceof Tree);
65
66        $xref = $request->getAttribute('xref');
67        assert(is_string($xref));
68
69        $record = Registry::noteFactory()->make($xref, $tree);
70        $record = Auth::checkNoteAccess($record, false);
71
72        // Redirect to correct xref/slug
73        $slug = Registry::slugFactory()->make($record);
74
75        if ($record->xref() !== $xref || $request->getAttribute('slug') !== $slug) {
76            return redirect($record->url(), StatusCodeInterface::STATUS_MOVED_PERMANENTLY);
77        }
78
79        return $this->viewResponse('note-page', [
80            'clipboard_facts'      => $this->clipboard_service->pastableFacts($record),
81            'linked_families'      => $record->linkedFamilies('NOTE'),
82            'linked_individuals'   => $record->linkedIndividuals('NOTE'),
83            'linked_media_objects' => $record->linkedMedia('NOTE'),
84            'linked_sources'       => $record->linkedSources('NOTE'),
85            'meta_description'     => '',
86            'meta_robots'          => 'index,follow',
87            'record'               => $record,
88            'title'                => $record->fullName(),
89            'tree'                 => $tree,
90        ]);
91    }
92}
93