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