xref: /webtrees/app/Note.php (revision 1df7fd9e34904f6a1844410d2fccee8a56936e02)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2022 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\Database\Capsule\Manager as DB;
25use Illuminate\Support\Str;
26
27use function explode;
28use function htmlspecialchars_decode;
29use function preg_match;
30use function preg_replace;
31use function strip_tags;
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            $html = Registry::markdownFactory()->markdown($this->getNote());
94        } else {
95            $html = Registry::markdownFactory()->autolink($this->getNote());
96        }
97
98        $first_line = self::firstLineOfTextFromHtml($html);
99
100        if ($first_line !== '') {
101            $this->addName('NOTE', Str::limit($first_line, 100, I18N::translate('…')), $this->gedcom());
102        }
103    }
104
105    /**
106     * Notes are converted to HTML for display.  We want the first line
107     *
108     * @param string $html
109     *
110     * @return string
111     */
112    public static function firstLineOfTextFromHtml(string $html): string
113    {
114        $html = strtr($html, [
115            '</blockquote>' => MarkdownFactory::BREAK,
116            '</h1>'         => MarkdownFactory::BREAK,
117            '</h2>'         => MarkdownFactory::BREAK,
118            '</h3>'         => MarkdownFactory::BREAK,
119            '</h4>'         => MarkdownFactory::BREAK,
120            '</h5>'         => MarkdownFactory::BREAK,
121            '</h6>'         => MarkdownFactory::BREAK,
122            '</li>'         => MarkdownFactory::BREAK,
123            '</p>'          => MarkdownFactory::BREAK,
124            '</pre>'        => MarkdownFactory::BREAK,
125            '</td>'         => ' ',
126            '</th>'         => ' ',
127            '<hr>'          => MarkdownFactory::BREAK,
128        ]);
129
130        $html = strip_tags($html, ['br']);
131
132        [$first] = explode(MarkdownFactory::BREAK, $html, 2);
133
134        return htmlspecialchars_decode($first, ENT_QUOTES);
135    }
136}
137