xref: /webtrees/app/Http/RequestHandlers/NotePage.php (revision 08362db417156410ee02c6391bd9eb83a2df65fc)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 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 <http://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22use Fisharebest\Webtrees\Auth;
23use Fisharebest\Webtrees\Fact;
24use Fisharebest\Webtrees\Filter;
25use Fisharebest\Webtrees\Http\ViewResponseTrait;
26use Fisharebest\Webtrees\Note;
27use Fisharebest\Webtrees\Services\ClipboardService;
28use Fisharebest\Webtrees\Tree;
29use Illuminate\Support\Collection;
30use Psr\Http\Message\ResponseInterface;
31use Psr\Http\Message\ServerRequestInterface;
32use Psr\Http\Server\RequestHandlerInterface;
33
34use function assert;
35use function is_string;
36use function redirect;
37
38/**
39 * Show a note's page.
40 */
41class NotePage implements RequestHandlerInterface
42{
43    use ViewResponseTrait;
44
45    /** @var ClipboardService */
46    private $clipboard_service;
47
48    /**
49     * NotePage constructor.
50     *
51     * @param ClipboardService $clipboard_service
52     */
53    public function __construct(ClipboardService $clipboard_service)
54    {
55        $this->clipboard_service = $clipboard_service;
56    }
57
58    /**
59     * @param ServerRequestInterface $request
60     *
61     * @return ResponseInterface
62     */
63    public function handle(ServerRequestInterface $request): ResponseInterface
64    {
65        $tree = $request->getAttribute('tree');
66        assert($tree instanceof Tree);
67
68        $xref = $request->getAttribute('xref');
69        assert(is_string($xref));
70
71        $note = Note::getInstance($xref, $tree);
72        $note = Auth::checkNoteAccess($note, false);
73
74        // Redirect to correct xref/slug
75        if ($note->xref() !== $xref || $request->getAttribute('slug') !== $note->slug()) {
76            return redirect($note->url());
77        }
78
79        return $this->viewResponse('note-page', [
80            'clipboard_facts' => $this->clipboard_service->pastableFacts($note, new Collection()),
81            'facts'           => $this->facts($note),
82            'families'        => $note->linkedFamilies('NOTE'),
83            'individuals'     => $note->linkedIndividuals('NOTE'),
84            'note'            => $note,
85            'notes'           => new Collection([]),
86            'media_objects'   => $note->linkedMedia('NOTE'),
87            'meta_robots'     => 'index,follow',
88            'sources'         => $note->linkedSources('NOTE'),
89            'text'            => Filter::formatText($note->getNote(), $tree),
90            'title'           => $note->fullName(),
91            'tree'            => $tree,
92        ]);
93    }
94
95    /**
96     * @param Note $record
97     *
98     * @return Collection
99     */
100    private function facts(Note $record): Collection
101    {
102        return $record->facts()
103            ->filter(static function (Fact $fact): bool {
104                return $fact->getTag() !== 'CONT';
105            });
106    }
107}
108