xref: /webtrees/app/Http/RequestHandlers/NotePage.php (revision b55cbc6b43247e8b2ad14af6f6d24dc6747195ff)
1d7daee59SGreg Roach<?php
2d7daee59SGreg Roach
3d7daee59SGreg Roach/**
4d7daee59SGreg Roach * webtrees: online genealogy
589f7189bSGreg Roach * Copyright (C) 2021 webtrees development team
6d7daee59SGreg Roach * This program is free software: you can redistribute it and/or modify
7d7daee59SGreg Roach * it under the terms of the GNU General Public License as published by
8d7daee59SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9d7daee59SGreg Roach * (at your option) any later version.
10d7daee59SGreg Roach * This program is distributed in the hope that it will be useful,
11d7daee59SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12d7daee59SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13d7daee59SGreg Roach * GNU General Public License for more details.
14d7daee59SGreg 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/>.
16d7daee59SGreg Roach */
17d7daee59SGreg Roach
18d7daee59SGreg Roachdeclare(strict_types=1);
19d7daee59SGreg Roach
20d7daee59SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
21d7daee59SGreg Roach
22e5d858f5SGreg Roachuse Fig\Http\Message\StatusCodeInterface;
23d7daee59SGreg Roachuse Fisharebest\Webtrees\Auth;
24d7daee59SGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
256b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
26d7daee59SGreg Roachuse Fisharebest\Webtrees\Services\ClipboardService;
27*b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
28d7daee59SGreg Roachuse Psr\Http\Message\ResponseInterface;
29d7daee59SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
30d7daee59SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
31d7daee59SGreg Roach
32d7daee59SGreg Roachuse function assert;
33d7daee59SGreg Roachuse function is_string;
34d7daee59SGreg Roachuse function redirect;
35d7daee59SGreg Roach
36d7daee59SGreg Roach/**
37d7daee59SGreg Roach * Show a note's page.
38d7daee59SGreg Roach */
39d7daee59SGreg Roachclass NotePage implements RequestHandlerInterface
40d7daee59SGreg Roach{
41d7daee59SGreg Roach    use ViewResponseTrait;
42d7daee59SGreg Roach
43c4943cffSGreg Roach    private ClipboardService $clipboard_service;
44d7daee59SGreg Roach
45d7daee59SGreg Roach    /**
46d7daee59SGreg Roach     * NotePage constructor.
47d7daee59SGreg Roach     *
48d7daee59SGreg Roach     * @param ClipboardService $clipboard_service
49d7daee59SGreg Roach     */
50d7daee59SGreg Roach    public function __construct(ClipboardService $clipboard_service)
51d7daee59SGreg Roach    {
52d7daee59SGreg Roach        $this->clipboard_service = $clipboard_service;
53d7daee59SGreg Roach    }
54d7daee59SGreg Roach
55d7daee59SGreg Roach    /**
56d7daee59SGreg Roach     * @param ServerRequestInterface $request
57d7daee59SGreg Roach     *
58d7daee59SGreg Roach     * @return ResponseInterface
59d7daee59SGreg Roach     */
60d7daee59SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
61d7daee59SGreg Roach    {
62*b55cbc6bSGreg Roach        $tree   = Validator::attributes($request)->tree();
63*b55cbc6bSGreg Roach        $xref   = Validator::attributes($request)->isXref()->string('xref');
64*b55cbc6bSGreg Roach        $slug   = Validator::attributes($request)->string('slug', '');
650f5fd22fSGreg Roach        $record = Registry::noteFactory()->make($xref, $tree);
660f5fd22fSGreg Roach        $record = Auth::checkNoteAccess($record, false);
67d7daee59SGreg Roach
68d7daee59SGreg Roach        // Redirect to correct xref/slug
69*b55cbc6bSGreg Roach        if ($record->xref() !== $xref || Registry::slugFactory()->make($record) !== $slug) {
700f5fd22fSGreg Roach            return redirect($record->url(), StatusCodeInterface::STATUS_MOVED_PERMANENTLY);
71d7daee59SGreg Roach        }
72d7daee59SGreg Roach
73d7daee59SGreg Roach        return $this->viewResponse('note-page', [
740f5fd22fSGreg Roach            'clipboard_facts'      => $this->clipboard_service->pastableFacts($record),
750f5fd22fSGreg Roach            'linked_families'      => $record->linkedFamilies('NOTE'),
760f5fd22fSGreg Roach            'linked_individuals'   => $record->linkedIndividuals('NOTE'),
770f5fd22fSGreg Roach            'linked_media_objects' => $record->linkedMedia('NOTE'),
780f5fd22fSGreg Roach            'linked_sources'       => $record->linkedSources('NOTE'),
792406e0e0SGreg Roach            'meta_description'     => '',
80d7daee59SGreg Roach            'meta_robots'          => 'index,follow',
810f5fd22fSGreg Roach            'record'               => $record,
820f5fd22fSGreg Roach            'title'                => $record->fullName(),
8358d1e607SGreg Roach            'tree'                 => $tree,
84d7daee59SGreg Roach        ]);
85d7daee59SGreg Roach    }
86d7daee59SGreg Roach}
87