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