xref: /webtrees/app/Note.php (revision 0c0910bf0f275a14f35d2ccdf698f91f79e269d4)
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 */
17declare(strict_types=1);
18
19namespace Fisharebest\Webtrees;
20
21use Closure;
22use Exception;
23use Illuminate\Database\Capsule\Manager as DB;
24use Illuminate\Support\Str;
25use stdClass;
26
27/**
28 * A GEDCOM note (NOTE) object.
29 */
30class Note extends GedcomRecord
31{
32    public const RECORD_TYPE = 'NOTE';
33
34    protected const ROUTE_NAME = 'note';
35
36    /**
37     * A closure which will create a record from a database row.
38     *
39     * @return Closure
40     */
41    public static function rowMapper(): Closure
42    {
43        return static function (stdClass $row): Note {
44            return Note::getInstance($row->o_id, Tree::findById((int) $row->o_file), $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): ?self
62    {
63        $record = parent::getInstance($xref, $tree, $gedcom);
64
65        if ($record instanceof self) {
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(): string
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 string|null
131     */
132    protected static function fetchGedcomRecord(string $xref, int $tree_id): ?string
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(): void
148    {
149        $text = $this->getNote();
150
151        if ($text) {
152            [$text] = explode("\n", $text);
153            $this->addName('NOTE', Str::limit($text, 100, I18N::translate('…')), $this->gedcom());
154        }
155    }
156}
157