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