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