xref: /webtrees/app/Note.php (revision e7f56f2af615447ab1a7646851f88b465ace9e04)
1a25f0a04SGreg Roach<?php
2a25f0a04SGreg Roach/**
3a25f0a04SGreg Roach * webtrees: online genealogy
41062a142SGreg Roach * Copyright (C) 2018 webtrees development team
5a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify
6a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by
7a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or
8a25f0a04SGreg Roach * (at your option) any later version.
9a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful,
10a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
11a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12a25f0a04SGreg Roach * GNU General Public License for more details.
13a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License
14a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
15a25f0a04SGreg Roach */
16*e7f56f2aSGreg Roachdeclare(strict_types=1);
17*e7f56f2aSGreg Roach
1876692c8bSGreg Roachnamespace Fisharebest\Webtrees;
19a25f0a04SGreg Roach
20a25f0a04SGreg Roach/**
2176692c8bSGreg Roach * A GEDCOM note (NOTE) object.
22a25f0a04SGreg Roach */
23c1010edaSGreg Roachclass Note extends GedcomRecord
24c1010edaSGreg Roach{
25a25f0a04SGreg Roach    const RECORD_TYPE = 'NOTE';
26225e381fSGreg Roach    const ROUTE_NAME  = 'note';
27a25f0a04SGreg Roach
28a25f0a04SGreg Roach    /**
29e71ef9d2SGreg Roach     * Get an instance of a note object. For single records,
30e71ef9d2SGreg Roach     * we just receive the XREF. For bulk records (such as lists
31e71ef9d2SGreg Roach     * and search results) we can receive the GEDCOM data as well.
32e71ef9d2SGreg Roach     *
33e71ef9d2SGreg Roach     * @param string      $xref
34e71ef9d2SGreg Roach     * @param Tree        $tree
35e71ef9d2SGreg Roach     * @param string|null $gedcom
36e71ef9d2SGreg Roach     *
37e71ef9d2SGreg Roach     * @throws \Exception
38e71ef9d2SGreg Roach     *
39e71ef9d2SGreg Roach     * @return Note|null
40e71ef9d2SGreg Roach     */
4176f666f4SGreg Roach    public static function getInstance(string $xref, Tree $tree, string $gedcom = null)
42c1010edaSGreg Roach    {
43e71ef9d2SGreg Roach        $record = parent::getInstance($xref, $tree, $gedcom);
44e71ef9d2SGreg Roach
45e71ef9d2SGreg Roach        if ($record instanceof Note) {
46e71ef9d2SGreg Roach            return $record;
47e71ef9d2SGreg Roach        }
48b2ce94c6SRico Sonntag
49b2ce94c6SRico Sonntag        return null;
50e71ef9d2SGreg Roach    }
51e71ef9d2SGreg Roach
52e71ef9d2SGreg Roach    /**
53a25f0a04SGreg Roach     * Get the text contents of the note
54a25f0a04SGreg Roach     *
55a25f0a04SGreg Roach     * @return string|null
56a25f0a04SGreg Roach     */
57c1010edaSGreg Roach    public function getNote()
58c1010edaSGreg Roach    {
59a25f0a04SGreg Roach        if (preg_match('/^0 @' . WT_REGEX_XREF . '@ NOTE ?(.*(?:\n1 CONT ?.*)*)/', $this->gedcom . $this->pending, $match)) {
60a25f0a04SGreg Roach            return preg_replace("/\n1 CONT ?/", "\n", $match[1]);
61a25f0a04SGreg Roach        }
62b2ce94c6SRico Sonntag
63b2ce94c6SRico Sonntag        return null;
64a25f0a04SGreg Roach    }
65a25f0a04SGreg Roach
6676692c8bSGreg Roach    /**
6776692c8bSGreg Roach     * Each object type may have its own special rules, and re-implement this function.
6876692c8bSGreg Roach     *
6976692c8bSGreg Roach     * @param int $access_level
7076692c8bSGreg Roach     *
7176692c8bSGreg Roach     * @return bool
7276692c8bSGreg Roach     */
7335584196SGreg Roach    protected function canShowByType(int $access_level): bool
74c1010edaSGreg Roach    {
75a25f0a04SGreg Roach        // Hide notes if they are attached to private records
76a25f0a04SGreg Roach        $linked_ids = Database::prepare(
77a25f0a04SGreg Roach            "SELECT l_from FROM `##link` WHERE l_to=? AND l_file=?"
7813abd6f3SGreg Roach        )->execute([
79c1010edaSGreg Roach            $this->xref,
80c1010edaSGreg Roach            $this->tree->getTreeId(),
8113abd6f3SGreg Roach        ])->fetchOneColumn();
82a25f0a04SGreg Roach        foreach ($linked_ids as $linked_id) {
8324ec66ceSGreg Roach            $linked_record = GedcomRecord::getInstance($linked_id, $this->tree);
84a25f0a04SGreg Roach            if ($linked_record && !$linked_record->canShow($access_level)) {
85a25f0a04SGreg Roach                return false;
86a25f0a04SGreg Roach            }
87a25f0a04SGreg Roach        }
88a25f0a04SGreg Roach
89a25f0a04SGreg Roach        // Apply default behaviour
90a25f0a04SGreg Roach        return parent::canShowByType($access_level);
91a25f0a04SGreg Roach    }
92a25f0a04SGreg Roach
9376692c8bSGreg Roach    /**
9476692c8bSGreg Roach     * Generate a private version of this record
9576692c8bSGreg Roach     *
9676692c8bSGreg Roach     * @param int $access_level
9776692c8bSGreg Roach     *
9876692c8bSGreg Roach     * @return string
9976692c8bSGreg Roach     */
1003c90ed31SGreg Roach    protected function createPrivateGedcomRecord(int $access_level): string
101c1010edaSGreg Roach    {
102a25f0a04SGreg Roach        return '0 @' . $this->xref . '@ NOTE ' . I18N::translate('Private');
103a25f0a04SGreg Roach    }
104a25f0a04SGreg Roach
10576692c8bSGreg Roach    /**
10676692c8bSGreg Roach     * Fetch data from the database
10776692c8bSGreg Roach     *
10876692c8bSGreg Roach     * @param string $xref
10976692c8bSGreg Roach     * @param int    $tree_id
11076692c8bSGreg Roach     *
11176692c8bSGreg Roach     * @return null|string
11276692c8bSGreg Roach     */
11376f666f4SGreg Roach    protected static function fetchGedcomRecord(string $xref, int $tree_id)
114c1010edaSGreg Roach    {
11564d9078aSGreg Roach        return Database::prepare(
11664d9078aSGreg Roach            "SELECT o_gedcom FROM `##other` WHERE o_id = :xref AND o_file = :tree_id AND o_type = 'NOTE'"
11713abd6f3SGreg Roach        )->execute([
11864d9078aSGreg Roach            'xref'    => $xref,
119cbc1590aSGreg Roach            'tree_id' => $tree_id,
12013abd6f3SGreg Roach        ])->fetchOne();
121a25f0a04SGreg Roach    }
122a25f0a04SGreg Roach
123a25f0a04SGreg Roach    /**
124a25f0a04SGreg Roach     * Create a name for this note - apply (and remove) markup, then take
125a25f0a04SGreg Roach     * a maximum of 100 characters from the first line.
126c7ff4153SGreg Roach     *
127c7ff4153SGreg Roach     * @return void
128a25f0a04SGreg Roach     */
129c1010edaSGreg Roach    public function extractNames()
130c1010edaSGreg Roach    {
131a25f0a04SGreg Roach        $text = $this->getNote();
132a25f0a04SGreg Roach
133a25f0a04SGreg Roach        if ($text) {
13484caa210SGreg Roach            switch ($this->getTree()->getPreference('FORMAT_TEXT')) {
135a25f0a04SGreg Roach                case 'markdown':
13622acdb08SGreg Roach                    $text = Filter::markdown($text, $this->getTree());
1379524b7b5SGreg Roach                    $text = strip_tags($text);
1389524b7b5SGreg Roach                    $text = html_entity_decode($text, ENT_QUOTES, 'UTF-8');
139a25f0a04SGreg Roach                    break;
140a25f0a04SGreg Roach            }
141a25f0a04SGreg Roach
142a25f0a04SGreg Roach            list($text) = explode("\n", $text);
143a25f0a04SGreg Roach            $this->addName('NOTE', strlen($text) > 100 ? mb_substr($text, 0, 100) . I18N::translate('…') : $text, $this->getGedcom());
144a25f0a04SGreg Roach        }
145a25f0a04SGreg Roach    }
146a25f0a04SGreg Roach}
147