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