Passed
Push — master ( 541863...c46acb )
by Greg
06:06
created

NotePage::facts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
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
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22
use Fisharebest\Webtrees\Auth;
23
use Fisharebest\Webtrees\Fact;
24
use Fisharebest\Webtrees\Filter;
25
use Fisharebest\Webtrees\Http\ViewResponseTrait;
26
use Fisharebest\Webtrees\Note;
27
use Fisharebest\Webtrees\Services\ClipboardService;
28
use Fisharebest\Webtrees\Tree;
29
use Illuminate\Support\Collection;
30
use Psr\Http\Message\ResponseInterface;
31
use Psr\Http\Message\ServerRequestInterface;
32
use Psr\Http\Server\RequestHandlerInterface;
33
34
use function assert;
35
use function is_string;
36
use function redirect;
37
38
/**
39
 * Show a note's page.
40
 */
41
class 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
        ]);
92
    }
93
94
    /**
95
     * @param Note $record
96
     *
97
     * @return Collection
98
     */
99
    private function facts(Note $record): Collection
100
    {
101
        return $record->facts()
102
            ->filter(static function (Fact $fact): bool {
103
                return $fact->getTag() !== 'CONT';
104
            });
105
    }
106
}
107