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 */
16namespace Fisharebest\Webtrees;
17
18/**
19 * A GEDCOM note (NOTE) object.
20 */
21class Note extends GedcomRecord
22{
23    const RECORD_TYPE = 'NOTE';
24    const URL_PREFIX  = 'note.php?nid=';
25
26    /**
27     * Get the text contents of the note
28     *
29     * @return string|null
30     */
31    public function getNote()
32    {
33        if (preg_match('/^0 @' . WT_REGEX_XREF . '@ NOTE ?(.*(?:\n1 CONT ?.*)*)/', $this->gedcom . $this->pending, $match)) {
34            return preg_replace("/\n1 CONT ?/", "\n", $match[1]);
35        } else {
36            return null;
37        }
38    }
39
40    /**
41     * Each object type may have its own special rules, and re-implement this function.
42     *
43     * @param int $access_level
44     *
45     * @return bool
46     */
47    protected function canShowByType($access_level)
48    {
49        // Hide notes if they are attached to private records
50        $linked_ids = Database::prepare(
51            "SELECT l_from FROM `##link` WHERE l_to=? AND l_file=?"
52        )->execute(array(
53            $this->xref, $this->tree->getTreeId(),
54        ))->fetchOneColumn();
55        foreach ($linked_ids as $linked_id) {
56            $linked_record = GedcomRecord::getInstance($linked_id, $this->tree);
57            if ($linked_record && !$linked_record->canShow($access_level)) {
58                return false;
59            }
60        }
61
62        // Apply default behaviour
63        return parent::canShowByType($access_level);
64    }
65
66    /**
67     * Generate a private version of this record
68     *
69     * @param int $access_level
70     *
71     * @return string
72     */
73    protected function createPrivateGedcomRecord($access_level)
74    {
75        return '0 @' . $this->xref . '@ NOTE ' . I18N::translate('Private');
76    }
77
78    /**
79     * Fetch data from the database
80     *
81     * @param string $xref
82     * @param int    $tree_id
83     *
84     * @return null|string
85     */
86    protected static function fetchGedcomRecord($xref, $tree_id)
87    {
88        return Database::prepare(
89            "SELECT o_gedcom FROM `##other` WHERE o_id = :xref AND o_file = :tree_id AND o_type = 'NOTE'"
90        )->execute(array(
91            'xref'    => $xref,
92            'tree_id' => $tree_id,
93        ))->fetchOne();
94    }
95
96    /**
97     * Create a name for this note - apply (and remove) markup, then take
98     * a maximum of 100 characters from the first line.
99     */
100    public function extractNames()
101    {
102        $text = $this->getNote();
103
104        if ($text) {
105            switch ($this->getTree()->getPreference('FORMAT_TEXT')) {
106                case 'markdown':
107                    $text = Filter::markdown($text);
108                    $text = Filter::unescapeHtml($text);
109                    break;
110            }
111
112            list($text) = explode("\n", $text);
113            $this->addName('NOTE', strlen($text) > 100 ? mb_substr($text, 0, 100) . I18N::translate('…') : $text, $this->getGedcom());
114        }
115    }
116}
117