xref: /webtrees/app/Note.php (revision 25366223eb30b309b137279bf74b779083e0a64e)
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
202e5b4452SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
219a9e551aSGreg Roachuse Illuminate\Support\Str;
229a9e551aSGreg Roach
23a25f0a04SGreg Roach/**
2476692c8bSGreg Roach * A GEDCOM note (NOTE) object.
25a25f0a04SGreg Roach */
26c1010edaSGreg Roachclass Note extends GedcomRecord
27c1010edaSGreg Roach{
2816d6367aSGreg Roach    public const RECORD_TYPE = 'NOTE';
2916d6367aSGreg Roach
3016d6367aSGreg Roach    protected const ROUTE_NAME = 'note';
31a25f0a04SGreg Roach
32a25f0a04SGreg Roach    /**
33e71ef9d2SGreg Roach     * Get an instance of a note object. For single records,
34e71ef9d2SGreg Roach     * we just receive the XREF. For bulk records (such as lists
35e71ef9d2SGreg Roach     * and search results) we can receive the GEDCOM data as well.
36e71ef9d2SGreg Roach     *
37e71ef9d2SGreg Roach     * @param string      $xref
38e71ef9d2SGreg Roach     * @param Tree        $tree
39e71ef9d2SGreg Roach     * @param string|null $gedcom
40e71ef9d2SGreg Roach     *
41e71ef9d2SGreg Roach     * @throws \Exception
42e71ef9d2SGreg Roach     *
43e71ef9d2SGreg Roach     * @return Note|null
44e71ef9d2SGreg Roach     */
4576f666f4SGreg Roach    public static function getInstance(string $xref, Tree $tree, string $gedcom = null)
46c1010edaSGreg Roach    {
47e71ef9d2SGreg Roach        $record = parent::getInstance($xref, $tree, $gedcom);
48e71ef9d2SGreg Roach
49e71ef9d2SGreg Roach        if ($record instanceof Note) {
50e71ef9d2SGreg Roach            return $record;
51e71ef9d2SGreg Roach        }
52b2ce94c6SRico Sonntag
53b2ce94c6SRico Sonntag        return null;
54e71ef9d2SGreg Roach    }
55e71ef9d2SGreg Roach
56e71ef9d2SGreg Roach    /**
57a25f0a04SGreg Roach     * Get the text contents of the note
58a25f0a04SGreg Roach     *
59ff166e64SGreg Roach     * @return string
60a25f0a04SGreg Roach     */
61c1010edaSGreg Roach    public function getNote()
62c1010edaSGreg Roach    {
638d0ebef0SGreg Roach        if (preg_match('/^0 @' . Gedcom::REGEX_XREF . '@ NOTE ?(.*(?:\n1 CONT ?.*)*)/', $this->gedcom . $this->pending, $match)) {
64a25f0a04SGreg Roach            return preg_replace("/\n1 CONT ?/", "\n", $match[1]);
65a25f0a04SGreg Roach        }
66b2ce94c6SRico Sonntag
67ff166e64SGreg Roach        return '';
68a25f0a04SGreg Roach    }
69a25f0a04SGreg Roach
7076692c8bSGreg Roach    /**
7176692c8bSGreg Roach     * Each object type may have its own special rules, and re-implement this function.
7276692c8bSGreg Roach     *
7376692c8bSGreg Roach     * @param int $access_level
7476692c8bSGreg Roach     *
7576692c8bSGreg Roach     * @return bool
7676692c8bSGreg Roach     */
7735584196SGreg Roach    protected function canShowByType(int $access_level): bool
78c1010edaSGreg Roach    {
79a25f0a04SGreg Roach        // Hide notes if they are attached to private records
80*25366223SGreg Roach        $linked_ids = DB::table('link')
81*25366223SGreg Roach            ->where('l_file', '=', $this->tree->id())
82*25366223SGreg Roach            ->where('l_to', '=', $this->xref)
83*25366223SGreg Roach            ->pluck('l_from');
84*25366223SGreg Roach
85a25f0a04SGreg Roach        foreach ($linked_ids as $linked_id) {
8624ec66ceSGreg Roach            $linked_record = GedcomRecord::getInstance($linked_id, $this->tree);
87a25f0a04SGreg Roach            if ($linked_record && !$linked_record->canShow($access_level)) {
88a25f0a04SGreg Roach                return false;
89a25f0a04SGreg Roach            }
90a25f0a04SGreg Roach        }
91a25f0a04SGreg Roach
92a25f0a04SGreg Roach        // Apply default behaviour
93a25f0a04SGreg Roach        return parent::canShowByType($access_level);
94a25f0a04SGreg Roach    }
95a25f0a04SGreg Roach
9676692c8bSGreg Roach    /**
9776692c8bSGreg Roach     * Generate a private version of this record
9876692c8bSGreg Roach     *
9976692c8bSGreg Roach     * @param int $access_level
10076692c8bSGreg Roach     *
10176692c8bSGreg Roach     * @return string
10276692c8bSGreg Roach     */
1033c90ed31SGreg Roach    protected function createPrivateGedcomRecord(int $access_level): string
104c1010edaSGreg Roach    {
105a25f0a04SGreg Roach        return '0 @' . $this->xref . '@ NOTE ' . I18N::translate('Private');
106a25f0a04SGreg Roach    }
107a25f0a04SGreg Roach
10876692c8bSGreg Roach    /**
10976692c8bSGreg Roach     * Fetch data from the database
11076692c8bSGreg Roach     *
11176692c8bSGreg Roach     * @param string $xref
11276692c8bSGreg Roach     * @param int    $tree_id
11376692c8bSGreg Roach     *
11476692c8bSGreg Roach     * @return null|string
11576692c8bSGreg Roach     */
11676f666f4SGreg Roach    protected static function fetchGedcomRecord(string $xref, int $tree_id)
117c1010edaSGreg Roach    {
1182e5b4452SGreg Roach        return DB::table('other')
1192e5b4452SGreg Roach            ->where('o_id', '=', $xref)
1202e5b4452SGreg Roach            ->where('o_file', '=', $tree_id)
1212e5b4452SGreg Roach            ->where('o_type', '=', self::RECORD_TYPE)
1222e5b4452SGreg Roach            ->value('o_gedcom');
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);
1459a9e551aSGreg Roach            $this->addName('NOTE', Str::limit($text, 100, I18N::translate('…')), $this->gedcom());
146a25f0a04SGreg Roach        }
147a25f0a04SGreg Roach    }
148a25f0a04SGreg Roach}
149