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