xref: /webtrees/app/Note.php (revision 52bcc40297e06e51c5cc74d5ddd66c750aefef51)
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 Illuminate\Database\Capsule\Manager as DB;
22use Illuminate\Support\Str;
23use stdClass;
24
25/**
26 * A GEDCOM note (NOTE) object.
27 */
28class Note extends GedcomRecord
29{
30    public const RECORD_TYPE = 'NOTE';
31
32    protected const ROUTE_NAME = 'note';
33
34    /**
35     * A closure which will create a record from a database row.
36     *
37     * @return Closure
38     */
39    public static function rowMapper(): Closure
40    {
41        return function (stdClass $row): Note {
42            return Note::getInstance($row->o_id, Tree::findById((int) $row->o_file), $row->o_gedcom);
43        };
44    }
45
46    /**
47     * Get an instance of a note object. For single records,
48     * we just receive the XREF. For bulk records (such as lists
49     * and search results) we can receive the GEDCOM data as well.
50     *
51     * @param string      $xref
52     * @param Tree        $tree
53     * @param string|null $gedcom
54     *
55     * @throws \Exception
56     *
57     * @return Note|null
58     */
59    public static function getInstance(string $xref, Tree $tree, string $gedcom = null)
60    {
61        $record = parent::getInstance($xref, $tree, $gedcom);
62
63        if ($record instanceof Note) {
64            return $record;
65        }
66
67        return null;
68    }
69
70    /**
71     * Get the text contents of the note
72     *
73     * @return string
74     */
75    public function getNote()
76    {
77        if (preg_match('/^0 @' . Gedcom::REGEX_XREF . '@ NOTE ?(.*(?:\n1 CONT ?.*)*)/', $this->gedcom . $this->pending, $match)) {
78            return preg_replace("/\n1 CONT ?/", "\n", $match[1]);
79        }
80
81        return '';
82    }
83
84    /**
85     * Each object type may have its own special rules, and re-implement this function.
86     *
87     * @param int $access_level
88     *
89     * @return bool
90     */
91    protected function canShowByType(int $access_level): bool
92    {
93        // Hide notes if they are attached to private records
94        $linked_ids = DB::table('link')
95            ->where('l_file', '=', $this->tree->id())
96            ->where('l_to', '=', $this->xref)
97            ->pluck('l_from');
98
99        foreach ($linked_ids as $linked_id) {
100            $linked_record = GedcomRecord::getInstance($linked_id, $this->tree);
101            if ($linked_record && !$linked_record->canShow($access_level)) {
102                return false;
103            }
104        }
105
106        // Apply default behaviour
107        return parent::canShowByType($access_level);
108    }
109
110    /**
111     * Generate a private version of this record
112     *
113     * @param int $access_level
114     *
115     * @return string
116     */
117    protected function createPrivateGedcomRecord(int $access_level): string
118    {
119        return '0 @' . $this->xref . '@ NOTE ' . I18N::translate('Private');
120    }
121
122    /**
123     * Fetch data from the database
124     *
125     * @param string $xref
126     * @param int    $tree_id
127     *
128     * @return null|string
129     */
130    protected static function fetchGedcomRecord(string $xref, int $tree_id)
131    {
132        return DB::table('other')
133            ->where('o_id', '=', $xref)
134            ->where('o_file', '=', $tree_id)
135            ->where('o_type', '=', self::RECORD_TYPE)
136            ->value('o_gedcom');
137    }
138
139    /**
140     * Create a name for this note - apply (and remove) markup, then take
141     * a maximum of 100 characters from the first line.
142     *
143     * @return void
144     */
145    public function extractNames()
146    {
147        $text = $this->getNote();
148
149        if ($text) {
150            switch ($this->tree()->getPreference('FORMAT_TEXT')) {
151                case 'markdown':
152                    $text = Filter::markdown($text, $this->tree());
153                    $text = strip_tags($text);
154                    $text = html_entity_decode($text, ENT_QUOTES, 'UTF-8');
155                    break;
156            }
157
158            [$text] = explode("\n", $text);
159            $this->addName('NOTE', Str::limit($text, 100, I18N::translate('…')), $this->gedcom());
160        }
161    }
162}
163