xref: /webtrees/app/Note.php (revision 4d35caa736b1f4119b8a949d1cbca5644dbf4e23)
1a25f0a04SGreg Roach<?php
23976b470SGreg Roach
3a25f0a04SGreg Roach/**
4a25f0a04SGreg Roach * webtrees: online genealogy
589f7189bSGreg Roach * Copyright (C) 2021 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
22d7daee59SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\NotePage;
232e5b4452SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
249a9e551aSGreg Roachuse Illuminate\Support\Str;
259a9e551aSGreg Roach
26a25f0a04SGreg Roach/**
2776692c8bSGreg Roach * A GEDCOM note (NOTE) object.
28a25f0a04SGreg Roach */
29c1010edaSGreg Roachclass Note extends GedcomRecord
30c1010edaSGreg Roach{
3116d6367aSGreg Roach    public const RECORD_TYPE = 'NOTE';
3216d6367aSGreg Roach
33d7daee59SGreg Roach    protected const ROUTE_NAME = NotePage::class;
34a25f0a04SGreg Roach
35a25f0a04SGreg Roach    /**
36a25f0a04SGreg Roach     * Get the text contents of the note
37a25f0a04SGreg Roach     *
38ff166e64SGreg Roach     * @return string
39a25f0a04SGreg Roach     */
40e364afe4SGreg Roach    public function getNote(): string
41c1010edaSGreg Roach    {
428d0ebef0SGreg Roach        if (preg_match('/^0 @' . Gedcom::REGEX_XREF . '@ NOTE ?(.*(?:\n1 CONT ?.*)*)/', $this->gedcom . $this->pending, $match)) {
43a25f0a04SGreg Roach            return preg_replace("/\n1 CONT ?/", "\n", $match[1]);
44a25f0a04SGreg Roach        }
45b2ce94c6SRico Sonntag
46ff166e64SGreg Roach        return '';
47a25f0a04SGreg Roach    }
48a25f0a04SGreg Roach
4976692c8bSGreg Roach    /**
5076692c8bSGreg Roach     * Each object type may have its own special rules, and re-implement this function.
5176692c8bSGreg Roach     *
5276692c8bSGreg Roach     * @param int $access_level
5376692c8bSGreg Roach     *
5476692c8bSGreg Roach     * @return bool
5576692c8bSGreg Roach     */
5635584196SGreg Roach    protected function canShowByType(int $access_level): bool
57c1010edaSGreg Roach    {
58a25f0a04SGreg Roach        // Hide notes if they are attached to private records
5925366223SGreg Roach        $linked_ids = DB::table('link')
6025366223SGreg Roach            ->where('l_file', '=', $this->tree->id())
6125366223SGreg Roach            ->where('l_to', '=', $this->xref)
6225366223SGreg Roach            ->pluck('l_from');
6325366223SGreg Roach
64a25f0a04SGreg Roach        foreach ($linked_ids as $linked_id) {
656b9cb339SGreg Roach            $linked_record = Registry::gedcomRecordFactory()->make($linked_id, $this->tree);
66bb03c9f0SGreg Roach            if ($linked_record instanceof GedcomRecord && !$linked_record->canShow($access_level)) {
67a25f0a04SGreg Roach                return false;
68a25f0a04SGreg Roach            }
69a25f0a04SGreg Roach        }
70a25f0a04SGreg Roach
71b3a775f6SGreg Roach        // Apply default behavior
72a25f0a04SGreg Roach        return parent::canShowByType($access_level);
73a25f0a04SGreg Roach    }
74a25f0a04SGreg Roach
7576692c8bSGreg Roach    /**
76a25f0a04SGreg Roach     * Create a name for this note - apply (and remove) markup, then take
77fbc72f88SGreg Roach     * a maximum of 100 characters from the first non-empty line.
78c7ff4153SGreg Roach     *
79c7ff4153SGreg Roach     * @return void
80a25f0a04SGreg Roach     */
81e364afe4SGreg Roach    public function extractNames(): void
82c1010edaSGreg Roach    {
83*4d35caa7SGreg Roach        if ($this->tree->getPreference('FORMAT_TEXT') === 'markdown') {
84*4d35caa7SGreg Roach            $text = Registry::markdownFactory()->markdown()->convertToHtml($this->getNote());
85*4d35caa7SGreg Roach        } else {
86*4d35caa7SGreg Roach            $text = Registry::markdownFactory()->autolink()->convertToHtml($this->getNote());
87*4d35caa7SGreg Roach        }
88a25f0a04SGreg Roach
89*4d35caa7SGreg Roach
90*4d35caa7SGreg Roach        // Take the first line
91*4d35caa7SGreg Roach        [$text] = explode("\n", strip_tags(trim($text)));
92fbc72f88SGreg Roach
93fbc72f88SGreg Roach        if ($text !== '') {
949a9e551aSGreg Roach            $this->addName('NOTE', Str::limit($text, 100, I18N::translate('…')), $this->gedcom());
95a25f0a04SGreg Roach        }
96a25f0a04SGreg Roach    }
97a25f0a04SGreg Roach}
98