xref: /webtrees/app/Note.php (revision 9a9e551aa818dd395fff089547e1f671b10094dd)
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 */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg Roach
1876692c8bSGreg Roachnamespace Fisharebest\Webtrees;
19a25f0a04SGreg Roach
20*9a9e551aSGreg Roachuse Illuminate\Support\Str;
21*9a9e551aSGreg Roach
22a25f0a04SGreg Roach/**
2376692c8bSGreg Roach * A GEDCOM note (NOTE) object.
24a25f0a04SGreg Roach */
25c1010edaSGreg Roachclass Note extends GedcomRecord
26c1010edaSGreg Roach{
27a25f0a04SGreg Roach    const RECORD_TYPE = 'NOTE';
28225e381fSGreg Roach    const ROUTE_NAME  = 'note';
29a25f0a04SGreg Roach
30a25f0a04SGreg Roach    /**
31e71ef9d2SGreg Roach     * Get an instance of a note object. For single records,
32e71ef9d2SGreg Roach     * we just receive the XREF. For bulk records (such as lists
33e71ef9d2SGreg Roach     * and search results) we can receive the GEDCOM data as well.
34e71ef9d2SGreg Roach     *
35e71ef9d2SGreg Roach     * @param string      $xref
36e71ef9d2SGreg Roach     * @param Tree        $tree
37e71ef9d2SGreg Roach     * @param string|null $gedcom
38e71ef9d2SGreg Roach     *
39e71ef9d2SGreg Roach     * @throws \Exception
40e71ef9d2SGreg Roach     *
41e71ef9d2SGreg Roach     * @return Note|null
42e71ef9d2SGreg Roach     */
4376f666f4SGreg Roach    public static function getInstance(string $xref, Tree $tree, string $gedcom = null)
44c1010edaSGreg Roach    {
45e71ef9d2SGreg Roach        $record = parent::getInstance($xref, $tree, $gedcom);
46e71ef9d2SGreg Roach
47e71ef9d2SGreg Roach        if ($record instanceof Note) {
48e71ef9d2SGreg Roach            return $record;
49e71ef9d2SGreg Roach        }
50b2ce94c6SRico Sonntag
51b2ce94c6SRico Sonntag        return null;
52e71ef9d2SGreg Roach    }
53e71ef9d2SGreg Roach
54e71ef9d2SGreg Roach    /**
55a25f0a04SGreg Roach     * Get the text contents of the note
56a25f0a04SGreg Roach     *
57ff166e64SGreg Roach     * @return string
58a25f0a04SGreg Roach     */
59c1010edaSGreg Roach    public function getNote()
60c1010edaSGreg Roach    {
618d0ebef0SGreg Roach        if (preg_match('/^0 @' . Gedcom::REGEX_XREF . '@ NOTE ?(.*(?:\n1 CONT ?.*)*)/', $this->gedcom . $this->pending, $match)) {
62a25f0a04SGreg Roach            return preg_replace("/\n1 CONT ?/", "\n", $match[1]);
63a25f0a04SGreg Roach        }
64b2ce94c6SRico Sonntag
65ff166e64SGreg Roach        return '';
66a25f0a04SGreg Roach    }
67a25f0a04SGreg Roach
6876692c8bSGreg Roach    /**
6976692c8bSGreg Roach     * Each object type may have its own special rules, and re-implement this function.
7076692c8bSGreg Roach     *
7176692c8bSGreg Roach     * @param int $access_level
7276692c8bSGreg Roach     *
7376692c8bSGreg Roach     * @return bool
7476692c8bSGreg Roach     */
7535584196SGreg Roach    protected function canShowByType(int $access_level): bool
76c1010edaSGreg Roach    {
77a25f0a04SGreg Roach        // Hide notes if they are attached to private records
78a25f0a04SGreg Roach        $linked_ids = Database::prepare(
79a25f0a04SGreg Roach            "SELECT l_from FROM `##link` WHERE l_to=? AND l_file=?"
8013abd6f3SGreg Roach        )->execute([
81c1010edaSGreg Roach            $this->xref,
8272cf66d4SGreg Roach            $this->tree->id(),
8313abd6f3SGreg Roach        ])->fetchOneColumn();
84a25f0a04SGreg Roach        foreach ($linked_ids as $linked_id) {
8524ec66ceSGreg Roach            $linked_record = GedcomRecord::getInstance($linked_id, $this->tree);
86a25f0a04SGreg Roach            if ($linked_record && !$linked_record->canShow($access_level)) {
87a25f0a04SGreg Roach                return false;
88a25f0a04SGreg Roach            }
89a25f0a04SGreg Roach        }
90a25f0a04SGreg Roach
91a25f0a04SGreg Roach        // Apply default behaviour
92a25f0a04SGreg Roach        return parent::canShowByType($access_level);
93a25f0a04SGreg Roach    }
94a25f0a04SGreg Roach
9576692c8bSGreg Roach    /**
9676692c8bSGreg Roach     * Generate a private version of this record
9776692c8bSGreg Roach     *
9876692c8bSGreg Roach     * @param int $access_level
9976692c8bSGreg Roach     *
10076692c8bSGreg Roach     * @return string
10176692c8bSGreg Roach     */
1023c90ed31SGreg Roach    protected function createPrivateGedcomRecord(int $access_level): string
103c1010edaSGreg Roach    {
104a25f0a04SGreg Roach        return '0 @' . $this->xref . '@ NOTE ' . I18N::translate('Private');
105a25f0a04SGreg Roach    }
106a25f0a04SGreg Roach
10776692c8bSGreg Roach    /**
10876692c8bSGreg Roach     * Fetch data from the database
10976692c8bSGreg Roach     *
11076692c8bSGreg Roach     * @param string $xref
11176692c8bSGreg Roach     * @param int    $tree_id
11276692c8bSGreg Roach     *
11376692c8bSGreg Roach     * @return null|string
11476692c8bSGreg Roach     */
11576f666f4SGreg Roach    protected static function fetchGedcomRecord(string $xref, int $tree_id)
116c1010edaSGreg Roach    {
11764d9078aSGreg Roach        return Database::prepare(
11864d9078aSGreg Roach            "SELECT o_gedcom FROM `##other` WHERE o_id = :xref AND o_file = :tree_id AND o_type = 'NOTE'"
11913abd6f3SGreg Roach        )->execute([
12064d9078aSGreg Roach            'xref'    => $xref,
121cbc1590aSGreg Roach            'tree_id' => $tree_id,
12213abd6f3SGreg Roach        ])->fetchOne();
123a25f0a04SGreg Roach    }
124a25f0a04SGreg Roach
125a25f0a04SGreg Roach    /**
126a25f0a04SGreg Roach     * Create a name for this note - apply (and remove) markup, then take
127a25f0a04SGreg Roach     * a maximum of 100 characters from the first line.
128c7ff4153SGreg Roach     *
129c7ff4153SGreg Roach     * @return void
130a25f0a04SGreg Roach     */
131c1010edaSGreg Roach    public function extractNames()
132c1010edaSGreg Roach    {
133a25f0a04SGreg Roach        $text = $this->getNote();
134a25f0a04SGreg Roach
135a25f0a04SGreg Roach        if ($text) {
136f4afa648SGreg Roach            switch ($this->tree()->getPreference('FORMAT_TEXT')) {
137a25f0a04SGreg Roach                case 'markdown':
138f4afa648SGreg Roach                    $text = Filter::markdown($text, $this->tree());
1399524b7b5SGreg Roach                    $text = strip_tags($text);
1409524b7b5SGreg Roach                    $text = html_entity_decode($text, ENT_QUOTES, 'UTF-8');
141a25f0a04SGreg Roach                    break;
142a25f0a04SGreg Roach            }
143a25f0a04SGreg Roach
14465e02381SGreg Roach            [$text] = explode("\n", $text);
145*9a9e551aSGreg Roach            $this->addName('NOTE', Str::limit($text, 100, I18N::translate('…')), $this->gedcom());
146a25f0a04SGreg Roach        }
147a25f0a04SGreg Roach    }
148a25f0a04SGreg Roach}
149