xref: /webtrees/app/Note.php (revision d4ec5ec0fe611c85b1d77527db3e9becde7e473d)
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;
21
22use Fisharebest\Webtrees\Http\RequestHandlers\NotePage;
23use Illuminate\Database\Capsule\Manager as DB;
24use Illuminate\Support\Str;
25
26use function explode;
27use function htmlspecialchars_decode;
28use function preg_match;
29use function preg_replace;
30use function strip_tags;
31use function trim;
32
33use const ENT_QUOTES;
34
35/**
36 * A GEDCOM note (NOTE) object.
37 */
38class Note extends GedcomRecord
39{
40    public const RECORD_TYPE = 'NOTE';
41
42    protected const ROUTE_NAME = NotePage::class;
43
44    /**
45     * Get the text contents of the note
46     *
47     * @return string
48     */
49    public function getNote(): string
50    {
51        if (preg_match('/^0 @' . Gedcom::REGEX_XREF . '@ NOTE ?(.*(?:\n1 CONT ?.*)*)/', $this->gedcom . $this->pending, $match)) {
52            return preg_replace("/\n1 CONT ?/", "\n", $match[1]);
53        }
54
55        return '';
56    }
57
58    /**
59     * Each object type may have its own special rules, and re-implement this function.
60     *
61     * @param int $access_level
62     *
63     * @return bool
64     */
65    protected function canShowByType(int $access_level): bool
66    {
67        // Hide notes if they are attached to private records
68        $linked_ids = DB::table('link')
69            ->where('l_file', '=', $this->tree->id())
70            ->where('l_to', '=', $this->xref)
71            ->pluck('l_from');
72
73        foreach ($linked_ids as $linked_id) {
74            $linked_record = Registry::gedcomRecordFactory()->make($linked_id, $this->tree);
75            if ($linked_record instanceof GedcomRecord && !$linked_record->canShow($access_level)) {
76                return false;
77            }
78        }
79
80        // Apply default behavior
81        return parent::canShowByType($access_level);
82    }
83
84    /**
85     * Create a name for this note - apply (and remove) markup, then take
86     * a maximum of 100 characters from the first non-empty line.
87     *
88     * @return void
89     */
90    public function extractNames(): void
91    {
92        if ($this->tree->getPreference('FORMAT_TEXT') === 'markdown') {
93            $text = Registry::markdownFactory()->markdown($this->getNote());
94        } else {
95            $text = Registry::markdownFactory()->autolink($this->getNote());
96        }
97
98
99        // Take the first line
100        [$text] = explode("\n", strip_tags(trim($text)));
101
102
103        if ($text !== '') {
104            $text = htmlspecialchars_decode($text, ENT_QUOTES);
105            $this->addName('NOTE', Str::limit($text, 100, I18N::translate('…')), $this->gedcom());
106        }
107    }
108}
109