xref: /webtrees/app/Note.php (revision afa67798854828b1edc33dd077960ec2b18e6140)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 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\Http\RequestHandlers\NotePage;
23use Illuminate\Database\Capsule\Manager as DB;
24use Illuminate\Support\Str;
25
26/**
27 * A GEDCOM note (NOTE) object.
28 */
29class Note extends GedcomRecord
30{
31    public const RECORD_TYPE = 'NOTE';
32
33    protected const ROUTE_NAME = NotePage::class;
34
35    /**
36     * Get the text contents of the note
37     *
38     * @return string
39     */
40    public function getNote(): string
41    {
42        if (preg_match('/^0 @' . Gedcom::REGEX_XREF . '@ NOTE ?(.*(?:\n1 CONT ?.*)*)/', $this->gedcom . $this->pending, $match)) {
43            return preg_replace("/\n1 CONT ?/", "\n", $match[1]);
44        }
45
46        return '';
47    }
48
49    /**
50     * Each object type may have its own special rules, and re-implement this function.
51     *
52     * @param int $access_level
53     *
54     * @return bool
55     */
56    protected function canShowByType(int $access_level): bool
57    {
58        // Hide notes if they are attached to private records
59        $linked_ids = DB::table('link')
60            ->where('l_file', '=', $this->tree->id())
61            ->where('l_to', '=', $this->xref)
62            ->pluck('l_from');
63
64        foreach ($linked_ids as $linked_id) {
65            $linked_record = Registry::gedcomRecordFactory()->make($linked_id, $this->tree);
66            if ($linked_record instanceof GedcomRecord && !$linked_record->canShow($access_level)) {
67                return false;
68            }
69        }
70
71        // Apply default behavior
72        return parent::canShowByType($access_level);
73    }
74
75    /**
76     * Create a name for this note - apply (and remove) markup, then take
77     * a maximum of 100 characters from the first non-empty line.
78     *
79     * @return void
80     */
81    public function extractNames(): void
82    {
83        $text = trim($this->getNote());
84
85        [$text] = explode("\n", $text);
86
87        if ($text !== '') {
88            $this->addName('NOTE', Str::limit($text, 100, I18N::translate('…')), $this->gedcom());
89        }
90    }
91}
92