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