xref: /webtrees/app/Note.php (revision 6ccdf4f0fd1b65a5d54259c969912382ce49629d)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2019 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16declare(strict_types=1);
17
18namespace Fisharebest\Webtrees;
19
20use Closure;
21use Exception;
22use Illuminate\Database\Capsule\Manager as DB;
23use Illuminate\Support\Str;
24use stdClass;
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 = 'note';
34
35    /**
36     * A closure which will create a record from a database row.
37     *
38     * @return Closure
39     */
40    public static function rowMapper(): Closure
41    {
42        return function (stdClass $row): Note {
43            return Note::getInstance($row->o_id, Tree::findById((int) $row->o_file), $row->o_gedcom);
44        };
45    }
46
47    /**
48     * Get an instance of a note object. For single records,
49     * we just receive the XREF. For bulk records (such as lists
50     * and search results) we can receive the GEDCOM data as well.
51     *
52     * @param string      $xref
53     * @param Tree        $tree
54     * @param string|null $gedcom
55     *
56     * @throws Exception
57     *
58     * @return Note|null
59     */
60    public static function getInstance(string $xref, Tree $tree, string $gedcom = null): ?self
61    {
62        $record = parent::getInstance($xref, $tree, $gedcom);
63
64        if ($record instanceof self) {
65            return $record;
66        }
67
68        return null;
69    }
70
71    /**
72     * Get the text contents of the note
73     *
74     * @return string
75     */
76    public function getNote(): string
77    {
78        if (preg_match('/^0 @' . Gedcom::REGEX_XREF . '@ NOTE ?(.*(?:\n1 CONT ?.*)*)/', $this->gedcom . $this->pending, $match)) {
79            return preg_replace("/\n1 CONT ?/", "\n", $match[1]);
80        }
81
82        return '';
83    }
84
85    /**
86     * Each object type may have its own special rules, and re-implement this function.
87     *
88     * @param int $access_level
89     *
90     * @return bool
91     */
92    protected function canShowByType(int $access_level): bool
93    {
94        // Hide notes if they are attached to private records
95        $linked_ids = DB::table('link')
96            ->where('l_file', '=', $this->tree->id())
97            ->where('l_to', '=', $this->xref)
98            ->pluck('l_from');
99
100        foreach ($linked_ids as $linked_id) {
101            $linked_record = GedcomRecord::getInstance($linked_id, $this->tree);
102            if ($linked_record && !$linked_record->canShow($access_level)) {
103                return false;
104            }
105        }
106
107        // Apply default behaviour
108        return parent::canShowByType($access_level);
109    }
110
111    /**
112     * Generate a private version of this record
113     *
114     * @param int $access_level
115     *
116     * @return string
117     */
118    protected function createPrivateGedcomRecord(int $access_level): string
119    {
120        return '0 @' . $this->xref . '@ NOTE ' . I18N::translate('Private');
121    }
122
123    /**
124     * Fetch data from the database
125     *
126     * @param string $xref
127     * @param int    $tree_id
128     *
129     * @return string|null
130     */
131    protected static function fetchGedcomRecord(string $xref, int $tree_id): ?string
132    {
133        return DB::table('other')
134            ->where('o_id', '=', $xref)
135            ->where('o_file', '=', $tree_id)
136            ->where('o_type', '=', self::RECORD_TYPE)
137            ->value('o_gedcom');
138    }
139
140    /**
141     * Create a name for this note - apply (and remove) markup, then take
142     * a maximum of 100 characters from the first line.
143     *
144     * @return void
145     */
146    public function extractNames(): void
147    {
148        $text = $this->getNote();
149
150        if ($text) {
151            switch ($this->tree()->getPreference('FORMAT_TEXT')) {
152                case 'markdown':
153                    $text = Filter::markdown($text, $this->tree());
154                    $text = strip_tags($text);
155                    $text = html_entity_decode($text, ENT_QUOTES, 'UTF-8');
156                    break;
157            }
158
159            [$text] = explode("\n", $text);
160            $this->addName('NOTE', Str::limit($text, 100, I18N::translate('…')), $this->gedcom());
161        }
162    }
163}
164