xref: /webtrees/app/Note.php (revision e364afe4ae4e316fc4ebd53eccbaff2d29e419a5)
1a25f0a04SGreg Roach<?php
2a25f0a04SGreg Roach/**
3a25f0a04SGreg Roach * webtrees: online genealogy
48fcd0d32SGreg Roach * Copyright (C) 2019 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
20886b77daSGreg Roachuse Closure;
212e5b4452SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
229a9e551aSGreg Roachuse Illuminate\Support\Str;
23886b77daSGreg Roachuse stdClass;
249a9e551aSGreg Roach
25a25f0a04SGreg Roach/**
2676692c8bSGreg Roach * A GEDCOM note (NOTE) object.
27a25f0a04SGreg Roach */
28c1010edaSGreg Roachclass Note extends GedcomRecord
29c1010edaSGreg Roach{
3016d6367aSGreg Roach    public const RECORD_TYPE = 'NOTE';
3116d6367aSGreg Roach
3216d6367aSGreg Roach    protected const ROUTE_NAME = 'note';
33a25f0a04SGreg Roach
34a25f0a04SGreg Roach    /**
35886b77daSGreg Roach     * A closure which will create a record from a database row.
36886b77daSGreg Roach     *
37886b77daSGreg Roach     * @return Closure
38886b77daSGreg Roach     */
39c0804649SGreg Roach    public static function rowMapper(): Closure
40886b77daSGreg Roach    {
41c0804649SGreg Roach        return function (stdClass $row): Note {
42a7a24840SGreg Roach            return Note::getInstance($row->o_id, Tree::findById((int) $row->o_file), $row->o_gedcom);
43886b77daSGreg Roach        };
44886b77daSGreg Roach    }
45886b77daSGreg Roach
46886b77daSGreg Roach    /**
47e71ef9d2SGreg Roach     * Get an instance of a note object. For single records,
48e71ef9d2SGreg Roach     * we just receive the XREF. For bulk records (such as lists
49e71ef9d2SGreg Roach     * and search results) we can receive the GEDCOM data as well.
50e71ef9d2SGreg Roach     *
51e71ef9d2SGreg Roach     * @param string      $xref
52e71ef9d2SGreg Roach     * @param Tree        $tree
53e71ef9d2SGreg Roach     * @param string|null $gedcom
54e71ef9d2SGreg Roach     *
55e71ef9d2SGreg Roach     * @throws \Exception
56e71ef9d2SGreg Roach     *
57e71ef9d2SGreg Roach     * @return Note|null
58e71ef9d2SGreg Roach     */
59*e364afe4SGreg Roach    public static function getInstance(string $xref, Tree $tree, string $gedcom = null): ?self
60c1010edaSGreg Roach    {
61e71ef9d2SGreg Roach        $record = parent::getInstance($xref, $tree, $gedcom);
62e71ef9d2SGreg Roach
63*e364afe4SGreg Roach        if ($record instanceof self) {
64e71ef9d2SGreg Roach            return $record;
65e71ef9d2SGreg Roach        }
66b2ce94c6SRico Sonntag
67b2ce94c6SRico Sonntag        return null;
68e71ef9d2SGreg Roach    }
69e71ef9d2SGreg Roach
70e71ef9d2SGreg Roach    /**
71a25f0a04SGreg Roach     * Get the text contents of the note
72a25f0a04SGreg Roach     *
73ff166e64SGreg Roach     * @return string
74a25f0a04SGreg Roach     */
75*e364afe4SGreg Roach    public function getNote(): string
76c1010edaSGreg Roach    {
778d0ebef0SGreg Roach        if (preg_match('/^0 @' . Gedcom::REGEX_XREF . '@ NOTE ?(.*(?:\n1 CONT ?.*)*)/', $this->gedcom . $this->pending, $match)) {
78a25f0a04SGreg Roach            return preg_replace("/\n1 CONT ?/", "\n", $match[1]);
79a25f0a04SGreg Roach        }
80b2ce94c6SRico Sonntag
81ff166e64SGreg Roach        return '';
82a25f0a04SGreg Roach    }
83a25f0a04SGreg Roach
8476692c8bSGreg Roach    /**
8576692c8bSGreg Roach     * Each object type may have its own special rules, and re-implement this function.
8676692c8bSGreg Roach     *
8776692c8bSGreg Roach     * @param int $access_level
8876692c8bSGreg Roach     *
8976692c8bSGreg Roach     * @return bool
9076692c8bSGreg Roach     */
9135584196SGreg Roach    protected function canShowByType(int $access_level): bool
92c1010edaSGreg Roach    {
93a25f0a04SGreg Roach        // Hide notes if they are attached to private records
9425366223SGreg Roach        $linked_ids = DB::table('link')
9525366223SGreg Roach            ->where('l_file', '=', $this->tree->id())
9625366223SGreg Roach            ->where('l_to', '=', $this->xref)
9725366223SGreg Roach            ->pluck('l_from');
9825366223SGreg Roach
99a25f0a04SGreg Roach        foreach ($linked_ids as $linked_id) {
10024ec66ceSGreg Roach            $linked_record = GedcomRecord::getInstance($linked_id, $this->tree);
101a25f0a04SGreg Roach            if ($linked_record && !$linked_record->canShow($access_level)) {
102a25f0a04SGreg Roach                return false;
103a25f0a04SGreg Roach            }
104a25f0a04SGreg Roach        }
105a25f0a04SGreg Roach
106a25f0a04SGreg Roach        // Apply default behaviour
107a25f0a04SGreg Roach        return parent::canShowByType($access_level);
108a25f0a04SGreg Roach    }
109a25f0a04SGreg Roach
11076692c8bSGreg Roach    /**
11176692c8bSGreg Roach     * Generate a private version of this record
11276692c8bSGreg Roach     *
11376692c8bSGreg Roach     * @param int $access_level
11476692c8bSGreg Roach     *
11576692c8bSGreg Roach     * @return string
11676692c8bSGreg Roach     */
1173c90ed31SGreg Roach    protected function createPrivateGedcomRecord(int $access_level): string
118c1010edaSGreg Roach    {
119a25f0a04SGreg Roach        return '0 @' . $this->xref . '@ NOTE ' . I18N::translate('Private');
120a25f0a04SGreg Roach    }
121a25f0a04SGreg Roach
12276692c8bSGreg Roach    /**
12376692c8bSGreg Roach     * Fetch data from the database
12476692c8bSGreg Roach     *
12576692c8bSGreg Roach     * @param string $xref
12676692c8bSGreg Roach     * @param int    $tree_id
12776692c8bSGreg Roach     *
128*e364afe4SGreg Roach     * @return string|null
12976692c8bSGreg Roach     */
130*e364afe4SGreg Roach    protected static function fetchGedcomRecord(string $xref, int $tree_id): ?string
131c1010edaSGreg Roach    {
1322e5b4452SGreg Roach        return DB::table('other')
1332e5b4452SGreg Roach            ->where('o_id', '=', $xref)
1342e5b4452SGreg Roach            ->where('o_file', '=', $tree_id)
1352e5b4452SGreg Roach            ->where('o_type', '=', self::RECORD_TYPE)
1362e5b4452SGreg Roach            ->value('o_gedcom');
137a25f0a04SGreg Roach    }
138a25f0a04SGreg Roach
139a25f0a04SGreg Roach    /**
140a25f0a04SGreg Roach     * Create a name for this note - apply (and remove) markup, then take
141a25f0a04SGreg Roach     * a maximum of 100 characters from the first line.
142c7ff4153SGreg Roach     *
143c7ff4153SGreg Roach     * @return void
144a25f0a04SGreg Roach     */
145*e364afe4SGreg Roach    public function extractNames(): void
146c1010edaSGreg Roach    {
147a25f0a04SGreg Roach        $text = $this->getNote();
148a25f0a04SGreg Roach
149a25f0a04SGreg Roach        if ($text) {
150f4afa648SGreg Roach            switch ($this->tree()->getPreference('FORMAT_TEXT')) {
151a25f0a04SGreg Roach                case 'markdown':
152f4afa648SGreg Roach                    $text = Filter::markdown($text, $this->tree());
1539524b7b5SGreg Roach                    $text = strip_tags($text);
1549524b7b5SGreg Roach                    $text = html_entity_decode($text, ENT_QUOTES, 'UTF-8');
155a25f0a04SGreg Roach                    break;
156a25f0a04SGreg Roach            }
157a25f0a04SGreg Roach
15865e02381SGreg Roach            [$text] = explode("\n", $text);
1599a9e551aSGreg Roach            $this->addName('NOTE', Str::limit($text, 100, I18N::translate('…')), $this->gedcom());
160a25f0a04SGreg Roach        }
161a25f0a04SGreg Roach    }
162a25f0a04SGreg Roach}
163