xref: /webtrees/app/Note.php (revision 24931b29a0237a5f5f1b8620af661ea530451af0)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 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\Factories\MarkdownFactory;
23use Fisharebest\Webtrees\Http\RequestHandlers\NotePage;
24use Illuminate\Support\Str;
25
26use function explode;
27use function htmlspecialchars_decode;
28use function preg_match;
29use function preg_replace;
30use function strip_tags;
31
32use const ENT_QUOTES;
33
34/**
35 * A GEDCOM note (NOTE) object.
36 */
37class Note extends GedcomRecord
38{
39    public const RECORD_TYPE = 'NOTE';
40
41    protected const ROUTE_NAME = NotePage::class;
42
43    /**
44     * Get the text contents of the note
45     *
46     * @return string
47     */
48    public function getNote(): string
49    {
50        if (preg_match('/^0 @' . Gedcom::REGEX_XREF . '@ ' . static::RECORD_TYPE . ' ?(.*(?:\n1 CONT ?.*)*)/', $this->gedcom . $this->pending, $match)) {
51            return preg_replace("/\n1 CONT ?/", "\n", $match[1]);
52        }
53
54        return '';
55    }
56
57    /**
58     * Each object type may have its own special rules, and re-implement this function.
59     *
60     * @param int $access_level
61     *
62     * @return bool
63     */
64    protected function canShowByType(int $access_level): bool
65    {
66        // Hide notes if they are attached to private records
67        $linked_ids = DB::table('link')
68            ->where('l_file', '=', $this->tree->id())
69            ->where('l_to', '=', $this->xref)
70            ->pluck('l_from');
71
72        foreach ($linked_ids as $linked_id) {
73            $linked_record = Registry::gedcomRecordFactory()->make($linked_id, $this->tree);
74            if ($linked_record instanceof GedcomRecord && !$linked_record->canShow($access_level)) {
75                return false;
76            }
77        }
78
79        // Apply default behavior
80        return parent::canShowByType($access_level);
81    }
82
83    /**
84     * Create a name for this note - apply (and remove) markup, then take
85     * a maximum of 100 characters from the first non-empty line.
86     *
87     * @return void
88     */
89    public function extractNames(): void
90    {
91        if ($this->tree->getPreference('FORMAT_TEXT') === 'markdown') {
92            $html = Registry::markdownFactory()->markdown($this->getNote());
93        } else {
94            $html = Registry::markdownFactory()->autolink($this->getNote());
95        }
96
97        $first_line = self::firstLineOfTextFromHtml($html);
98
99        if ($first_line !== '') {
100            $this->addName(static::RECORD_TYPE, Str::limit($first_line, 100, I18N::translate('…')), $this->gedcom());
101        }
102    }
103
104    /**
105     * Notes are converted to HTML for display.  We want the first line
106     *
107     * @param string $html
108     *
109     * @return string
110     */
111    public static function firstLineOfTextFromHtml(string $html): string
112    {
113        $html = strtr($html, [
114            '</blockquote>' => MarkdownFactory::BREAK,
115            '</h1>'         => MarkdownFactory::BREAK,
116            '</h2>'         => MarkdownFactory::BREAK,
117            '</h3>'         => MarkdownFactory::BREAK,
118            '</h4>'         => MarkdownFactory::BREAK,
119            '</h5>'         => MarkdownFactory::BREAK,
120            '</h6>'         => MarkdownFactory::BREAK,
121            '</li>'         => MarkdownFactory::BREAK,
122            '</p>'          => MarkdownFactory::BREAK,
123            '</pre>'        => MarkdownFactory::BREAK,
124            '</td>'         => ' ',
125            '</th>'         => ' ',
126            '<hr>'          => MarkdownFactory::BREAK,
127        ]);
128
129        $html = strip_tags($html, ['br']);
130
131        [$first] = explode(MarkdownFactory::BREAK, $html, 2);
132
133        return htmlspecialchars_decode($first, ENT_QUOTES);
134    }
135}
136