xref: /webtrees/app/Note.php (revision d7daee59eca171e896682012e6fe10d1f901501f)
1a25f0a04SGreg Roach<?php
23976b470SGreg Roach
3a25f0a04SGreg Roach/**
4a25f0a04SGreg Roach * webtrees: online genealogy
58fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
6a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify
7a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by
8a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9a25f0a04SGreg Roach * (at your option) any later version.
10a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful,
11a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13a25f0a04SGreg Roach * GNU General Public License for more details.
14a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License
15a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
16a25f0a04SGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees;
21a25f0a04SGreg Roach
22886b77daSGreg Roachuse Closure;
236ccdf4f0SGreg Roachuse Exception;
24*d7daee59SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\NotePage;
252e5b4452SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
269a9e551aSGreg Roachuse Illuminate\Support\Str;
27886b77daSGreg Roachuse stdClass;
289a9e551aSGreg Roach
29a25f0a04SGreg Roach/**
3076692c8bSGreg Roach * A GEDCOM note (NOTE) object.
31a25f0a04SGreg Roach */
32c1010edaSGreg Roachclass Note extends GedcomRecord
33c1010edaSGreg Roach{
3416d6367aSGreg Roach    public const RECORD_TYPE = 'NOTE';
3516d6367aSGreg Roach
36*d7daee59SGreg Roach    protected const ROUTE_NAME = NotePage::class;
37a25f0a04SGreg Roach
38a25f0a04SGreg Roach    /**
39886b77daSGreg Roach     * A closure which will create a record from a database row.
40886b77daSGreg Roach     *
41886b77daSGreg Roach     * @return Closure
42886b77daSGreg Roach     */
43c0804649SGreg Roach    public static function rowMapper(): Closure
44886b77daSGreg Roach    {
456c2179e2SGreg Roach        return static function (stdClass $row): Note {
46a7a24840SGreg Roach            return Note::getInstance($row->o_id, Tree::findById((int) $row->o_file), $row->o_gedcom);
47886b77daSGreg Roach        };
48886b77daSGreg Roach    }
49886b77daSGreg Roach
50886b77daSGreg Roach    /**
51e71ef9d2SGreg Roach     * Get an instance of a note object. For single records,
52e71ef9d2SGreg Roach     * we just receive the XREF. For bulk records (such as lists
53e71ef9d2SGreg Roach     * and search results) we can receive the GEDCOM data as well.
54e71ef9d2SGreg Roach     *
55e71ef9d2SGreg Roach     * @param string      $xref
56e71ef9d2SGreg Roach     * @param Tree        $tree
57e71ef9d2SGreg Roach     * @param string|null $gedcom
58e71ef9d2SGreg Roach     *
596ccdf4f0SGreg Roach     * @throws Exception
60e71ef9d2SGreg Roach     *
61e71ef9d2SGreg Roach     * @return Note|null
62e71ef9d2SGreg Roach     */
63e364afe4SGreg Roach    public static function getInstance(string $xref, Tree $tree, string $gedcom = null): ?self
64c1010edaSGreg Roach    {
65e71ef9d2SGreg Roach        $record = parent::getInstance($xref, $tree, $gedcom);
66e71ef9d2SGreg Roach
67e364afe4SGreg Roach        if ($record instanceof self) {
68e71ef9d2SGreg Roach            return $record;
69e71ef9d2SGreg Roach        }
70b2ce94c6SRico Sonntag
71b2ce94c6SRico Sonntag        return null;
72e71ef9d2SGreg Roach    }
73e71ef9d2SGreg Roach
74e71ef9d2SGreg Roach    /**
75a25f0a04SGreg Roach     * Get the text contents of the note
76a25f0a04SGreg Roach     *
77ff166e64SGreg Roach     * @return string
78a25f0a04SGreg Roach     */
79e364afe4SGreg Roach    public function getNote(): string
80c1010edaSGreg Roach    {
818d0ebef0SGreg Roach        if (preg_match('/^0 @' . Gedcom::REGEX_XREF . '@ NOTE ?(.*(?:\n1 CONT ?.*)*)/', $this->gedcom . $this->pending, $match)) {
82a25f0a04SGreg Roach            return preg_replace("/\n1 CONT ?/", "\n", $match[1]);
83a25f0a04SGreg Roach        }
84b2ce94c6SRico Sonntag
85ff166e64SGreg Roach        return '';
86a25f0a04SGreg Roach    }
87a25f0a04SGreg Roach
8876692c8bSGreg Roach    /**
8976692c8bSGreg Roach     * Each object type may have its own special rules, and re-implement this function.
9076692c8bSGreg Roach     *
9176692c8bSGreg Roach     * @param int $access_level
9276692c8bSGreg Roach     *
9376692c8bSGreg Roach     * @return bool
9476692c8bSGreg Roach     */
9535584196SGreg Roach    protected function canShowByType(int $access_level): bool
96c1010edaSGreg Roach    {
97a25f0a04SGreg Roach        // Hide notes if they are attached to private records
9825366223SGreg Roach        $linked_ids = DB::table('link')
9925366223SGreg Roach            ->where('l_file', '=', $this->tree->id())
10025366223SGreg Roach            ->where('l_to', '=', $this->xref)
10125366223SGreg Roach            ->pluck('l_from');
10225366223SGreg Roach
103a25f0a04SGreg Roach        foreach ($linked_ids as $linked_id) {
10424ec66ceSGreg Roach            $linked_record = GedcomRecord::getInstance($linked_id, $this->tree);
105a25f0a04SGreg Roach            if ($linked_record && !$linked_record->canShow($access_level)) {
106a25f0a04SGreg Roach                return false;
107a25f0a04SGreg Roach            }
108a25f0a04SGreg Roach        }
109a25f0a04SGreg Roach
110a25f0a04SGreg Roach        // Apply default behaviour
111a25f0a04SGreg Roach        return parent::canShowByType($access_level);
112a25f0a04SGreg Roach    }
113a25f0a04SGreg Roach
11476692c8bSGreg Roach    /**
11576692c8bSGreg Roach     * Generate a private version of this record
11676692c8bSGreg Roach     *
11776692c8bSGreg Roach     * @param int $access_level
11876692c8bSGreg Roach     *
11976692c8bSGreg Roach     * @return string
12076692c8bSGreg Roach     */
1213c90ed31SGreg Roach    protected function createPrivateGedcomRecord(int $access_level): string
122c1010edaSGreg Roach    {
123a25f0a04SGreg Roach        return '0 @' . $this->xref . '@ NOTE ' . I18N::translate('Private');
124a25f0a04SGreg Roach    }
125a25f0a04SGreg Roach
12676692c8bSGreg Roach    /**
12776692c8bSGreg Roach     * Fetch data from the database
12876692c8bSGreg Roach     *
12976692c8bSGreg Roach     * @param string $xref
13076692c8bSGreg Roach     * @param int    $tree_id
13176692c8bSGreg Roach     *
132e364afe4SGreg Roach     * @return string|null
13376692c8bSGreg Roach     */
134e364afe4SGreg Roach    protected static function fetchGedcomRecord(string $xref, int $tree_id): ?string
135c1010edaSGreg Roach    {
1362e5b4452SGreg Roach        return DB::table('other')
1372e5b4452SGreg Roach            ->where('o_id', '=', $xref)
1382e5b4452SGreg Roach            ->where('o_file', '=', $tree_id)
1392e5b4452SGreg Roach            ->where('o_type', '=', self::RECORD_TYPE)
1402e5b4452SGreg Roach            ->value('o_gedcom');
141a25f0a04SGreg Roach    }
142a25f0a04SGreg Roach
143a25f0a04SGreg Roach    /**
144a25f0a04SGreg Roach     * Create a name for this note - apply (and remove) markup, then take
145a25f0a04SGreg Roach     * a maximum of 100 characters from the first line.
146c7ff4153SGreg Roach     *
147c7ff4153SGreg Roach     * @return void
148a25f0a04SGreg Roach     */
149e364afe4SGreg Roach    public function extractNames(): void
150c1010edaSGreg Roach    {
151a25f0a04SGreg Roach        $text = $this->getNote();
152a25f0a04SGreg Roach
153a25f0a04SGreg Roach        if ($text) {
15465e02381SGreg Roach            [$text] = explode("\n", $text);
1559a9e551aSGreg Roach            $this->addName('NOTE', Str::limit($text, 100, I18N::translate('…')), $this->gedcom());
156a25f0a04SGreg Roach        }
157a25f0a04SGreg Roach    }
158a25f0a04SGreg Roach}
159