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