xref: /webtrees/app/Note.php (revision e873f434551745f888937263ff89e80db3b0f785)
1a25f0a04SGreg Roach<?php
23976b470SGreg Roach
3a25f0a04SGreg Roach/**
4a25f0a04SGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
6a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify
7a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by
8a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9a25f0a04SGreg Roach * (at your option) any later version.
10a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful,
11a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13a25f0a04SGreg Roach * GNU General Public License for more details.
14a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16a25f0a04SGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees;
21a25f0a04SGreg Roach
2228065790SGreg Roachuse Fisharebest\Webtrees\Factories\MarkdownFactory;
23d7daee59SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\NotePage;
249a9e551aSGreg Roachuse Illuminate\Support\Str;
259a9e551aSGreg Roach
26283375f9SGreg Roachuse function explode;
27283375f9SGreg Roachuse function htmlspecialchars_decode;
28283375f9SGreg Roachuse function preg_match;
29283375f9SGreg Roachuse function preg_replace;
30283375f9SGreg Roachuse function strip_tags;
3163a2c22bSGreg Roach
32283375f9SGreg Roachuse const ENT_QUOTES;
33283375f9SGreg Roach
34a25f0a04SGreg Roach/**
3576692c8bSGreg Roach * A GEDCOM note (NOTE) object.
36a25f0a04SGreg Roach */
37c1010edaSGreg Roachclass Note extends GedcomRecord
38c1010edaSGreg Roach{
39*e873f434SGreg Roach    public const string RECORD_TYPE = 'NOTE';
4016d6367aSGreg Roach
41*e873f434SGreg Roach    protected const string ROUTE_NAME = NotePage::class;
42a25f0a04SGreg Roach
43a25f0a04SGreg Roach    /**
44a25f0a04SGreg Roach     * Get the text contents of the note
45a25f0a04SGreg Roach     *
46ff166e64SGreg Roach     * @return string
47a25f0a04SGreg Roach     */
48e364afe4SGreg Roach    public function getNote(): string
49c1010edaSGreg Roach    {
50701f5d18SGreg Roach        if (preg_match('/^0 @' . Gedcom::REGEX_XREF . '@ ' . static::RECORD_TYPE . ' ?(.*(?:\n1 CONT ?.*)*)/', $this->gedcom . $this->pending, $match)) {
51a25f0a04SGreg Roach            return preg_replace("/\n1 CONT ?/", "\n", $match[1]);
52a25f0a04SGreg Roach        }
53b2ce94c6SRico Sonntag
54ff166e64SGreg Roach        return '';
55a25f0a04SGreg Roach    }
56a25f0a04SGreg Roach
5776692c8bSGreg Roach    /**
5876692c8bSGreg Roach     * Each object type may have its own special rules, and re-implement this function.
5976692c8bSGreg Roach     *
6076692c8bSGreg Roach     * @param int $access_level
6176692c8bSGreg Roach     *
6276692c8bSGreg Roach     * @return bool
6376692c8bSGreg Roach     */
6435584196SGreg Roach    protected function canShowByType(int $access_level): bool
65c1010edaSGreg Roach    {
66a25f0a04SGreg Roach        // Hide notes if they are attached to private records
6725366223SGreg Roach        $linked_ids = DB::table('link')
6825366223SGreg Roach            ->where('l_file', '=', $this->tree->id())
6925366223SGreg Roach            ->where('l_to', '=', $this->xref)
7025366223SGreg Roach            ->pluck('l_from');
7125366223SGreg Roach
72a25f0a04SGreg Roach        foreach ($linked_ids as $linked_id) {
736b9cb339SGreg Roach            $linked_record = Registry::gedcomRecordFactory()->make($linked_id, $this->tree);
74bb03c9f0SGreg Roach            if ($linked_record instanceof GedcomRecord && !$linked_record->canShow($access_level)) {
75a25f0a04SGreg Roach                return false;
76a25f0a04SGreg Roach            }
77a25f0a04SGreg Roach        }
78a25f0a04SGreg Roach
79b3a775f6SGreg Roach        // Apply default behavior
80a25f0a04SGreg Roach        return parent::canShowByType($access_level);
81a25f0a04SGreg Roach    }
82a25f0a04SGreg Roach
8376692c8bSGreg Roach    /**
84a25f0a04SGreg Roach     * Create a name for this note - apply (and remove) markup, then take
85fbc72f88SGreg Roach     * a maximum of 100 characters from the first non-empty line.
86c7ff4153SGreg Roach     *
87c7ff4153SGreg Roach     * @return void
88a25f0a04SGreg Roach     */
89e364afe4SGreg Roach    public function extractNames(): void
90c1010edaSGreg Roach    {
914d35caa7SGreg Roach        if ($this->tree->getPreference('FORMAT_TEXT') === 'markdown') {
92c1d58f74SGreg Roach            $html = Registry::markdownFactory()->markdown($this->getNote());
934d35caa7SGreg Roach        } else {
94c1d58f74SGreg Roach            $html = Registry::markdownFactory()->autolink($this->getNote());
954d35caa7SGreg Roach        }
96a25f0a04SGreg Roach
972de327daSGreg Roach        $first_line = self::firstLineOfTextFromHtml($html);
9863a2c22bSGreg Roach
9963a2c22bSGreg Roach        if ($first_line !== '') {
100701f5d18SGreg Roach            $this->addName(static::RECORD_TYPE, Str::limit($first_line, 100, I18N::translate('…')), $this->gedcom());
101a25f0a04SGreg Roach        }
102a25f0a04SGreg Roach    }
10363a2c22bSGreg Roach
10463a2c22bSGreg Roach    /**
10563a2c22bSGreg Roach     * Notes are converted to HTML for display.  We want the first line
10663a2c22bSGreg Roach     *
10763a2c22bSGreg Roach     * @param string $html
10863a2c22bSGreg Roach     *
10963a2c22bSGreg Roach     * @return string
11063a2c22bSGreg Roach     */
11163a2c22bSGreg Roach    public static function firstLineOfTextFromHtml(string $html): string
11263a2c22bSGreg Roach    {
11363a2c22bSGreg Roach        $html = strtr($html, [
11463a2c22bSGreg Roach            '</blockquote>' => MarkdownFactory::BREAK,
11563a2c22bSGreg Roach            '</h1>'         => MarkdownFactory::BREAK,
11663a2c22bSGreg Roach            '</h2>'         => MarkdownFactory::BREAK,
11763a2c22bSGreg Roach            '</h3>'         => MarkdownFactory::BREAK,
11863a2c22bSGreg Roach            '</h4>'         => MarkdownFactory::BREAK,
11963a2c22bSGreg Roach            '</h5>'         => MarkdownFactory::BREAK,
12063a2c22bSGreg Roach            '</h6>'         => MarkdownFactory::BREAK,
12163a2c22bSGreg Roach            '</li>'         => MarkdownFactory::BREAK,
12263a2c22bSGreg Roach            '</p>'          => MarkdownFactory::BREAK,
12363a2c22bSGreg Roach            '</pre>'        => MarkdownFactory::BREAK,
12463a2c22bSGreg Roach            '</td>'         => ' ',
12563a2c22bSGreg Roach            '</th>'         => ' ',
12663a2c22bSGreg Roach            '<hr>'          => MarkdownFactory::BREAK,
12763a2c22bSGreg Roach        ]);
12863a2c22bSGreg Roach
12963a2c22bSGreg Roach        $html = strip_tags($html, ['br']);
13063a2c22bSGreg Roach
13163a2c22bSGreg Roach        [$first] = explode(MarkdownFactory::BREAK, $html, 2);
13263a2c22bSGreg Roach
13363a2c22bSGreg Roach        return htmlspecialchars_decode($first, ENT_QUOTES);
13463a2c22bSGreg Roach    }
135a25f0a04SGreg Roach}
136