xref: /webtrees/app/Note.php (revision c2ed51d13a57743094c11c8fe84befd9d4f158cd)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees;
21
22use Closure;
23use Fisharebest\Webtrees\Http\RequestHandlers\NotePage;
24use Illuminate\Database\Capsule\Manager as DB;
25use Illuminate\Support\Str;
26
27/**
28 * A GEDCOM note (NOTE) object.
29 */
30class Note extends GedcomRecord
31{
32    public const RECORD_TYPE = 'NOTE';
33
34    protected const ROUTE_NAME = NotePage::class;
35
36    /**
37     * A closure which will create a record from a database row.
38     *
39     * @deprecated since 2.0.4.  Will be removed in 2.1.0 - Use Registry::noteFactory()
40     *
41     * @param Tree $tree
42     *
43     * @return Closure
44     */
45    public static function rowMapper(Tree $tree): Closure
46    {
47        return Registry::noteFactory()->mapper($tree);
48    }
49
50    /**
51     * Get an instance of a note object. For single records,
52     * we just receive the XREF. For bulk records (such as lists
53     * and search results) we can receive the GEDCOM data as well.
54     *
55     * @deprecated since 2.0.4.  Will be removed in 2.1.0 - Use Registry::noteFactory()
56     *
57     * @param string      $xref
58     * @param Tree        $tree
59     * @param string|null $gedcom
60     *
61     * @return Note|null
62     */
63    public static function getInstance(string $xref, Tree $tree, string $gedcom = null): ?Note
64    {
65        return Registry::noteFactory()->make($xref, $tree, $gedcom);
66    }
67
68    /**
69     * Get the text contents of the note
70     *
71     * @return string
72     */
73    public function getNote(): string
74    {
75        if (preg_match('/^0 @' . Gedcom::REGEX_XREF . '@ NOTE ?(.*(?:\n1 CONT ?.*)*)/', $this->gedcom . $this->pending, $match)) {
76            return preg_replace("/\n1 CONT ?/", "\n", $match[1]);
77        }
78
79        return '';
80    }
81
82    /**
83     * Each object type may have its own special rules, and re-implement this function.
84     *
85     * @param int $access_level
86     *
87     * @return bool
88     */
89    protected function canShowByType(int $access_level): bool
90    {
91        // Hide notes if they are attached to private records
92        $linked_ids = DB::table('link')
93            ->where('l_file', '=', $this->tree->id())
94            ->where('l_to', '=', $this->xref)
95            ->pluck('l_from');
96
97        foreach ($linked_ids as $linked_id) {
98            $linked_record = Registry::gedcomRecordFactory()->make($linked_id, $this->tree);
99            if ($linked_record instanceof GedcomRecord && !$linked_record->canShow($access_level)) {
100                return false;
101            }
102        }
103
104        // Apply default behavior
105        return parent::canShowByType($access_level);
106    }
107
108    /**
109     * Create a name for this note - apply (and remove) markup, then take
110     * a maximum of 100 characters from the first non-empty line.
111     *
112     * @return void
113     */
114    public function extractNames(): void
115    {
116        $text = trim($this->getNote());
117
118        [$text] = explode("\n", $text);
119
120        if ($text !== '') {
121            $this->addName('NOTE', Str::limit($text, 100, I18N::translate('…')), $this->gedcom());
122        }
123    }
124}
125