xref: /webtrees/app/Elements/NoteStructure.php (revision 1270d2767576ed4a83917769b0ee3613e3b010bf)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 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\Elements;
21
22use Fisharebest\Webtrees\Gedcom;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Note;
25use Fisharebest\Webtrees\Registry;
26use Fisharebest\Webtrees\Tree;
27
28use function e;
29use function preg_match;
30use function strip_tags;
31use function substr_count;
32use function view;
33
34/**
35 * NOTE can be text or an XREF.
36 */
37class NoteStructure extends SubmitterText
38{
39    /**
40     * An edit control for this data.
41     *
42     * @param string $id
43     * @param string $name
44     * @param string $value
45     * @param Tree   $tree
46     *
47     * @return string
48     */
49    public function edit(string $id, string $name, string $value, Tree $tree): string
50    {
51        $submitter_text = new SubmitterText('');
52        $xref_note      = new XrefNote('');
53
54        // Existing shared note.
55        if (preg_match('/^@' . Gedcom::REGEX_XREF . '@$/', $value)) {
56            return $xref_note->edit($id, $name, $value, $tree);
57        }
58
59        // Existing inline note.
60        if ($value !== '') {
61            return $submitter_text->edit($id, $name, $value, $tree);
62        }
63
64        $options = [
65            'inline' => I18N::translate('inline note'),
66            'shared' => I18N::translate('shared note'),
67        ];
68
69        // New note - either inline or shared
70        return
71            '<div id="' . e($id) . '-note-structure">' .
72            '<div id="' . e($id) . '-options">' .
73            view('components/radios-inline', ['name' => $id . '-options', 'options' => $options, 'selected' => 'inline']) .
74            '</div>' .
75            '<div id="' . e($id) . '-inline">' .
76            $submitter_text->edit($id, $name, $value, $tree) .
77            '</div>' .
78            '<div id="' . e($id) . '-shared" class="d-none">' .
79            $xref_note->edit($id . '-select', $name, $value, $tree) .
80            '</div>' .
81            '</div>' .
82            '<script>' .
83            'document.getElementById("' . e($id) . '-shared").querySelector("select").disabled=true;' .
84            'document.getElementById("' . e($id) . '-options").addEventListener("change", function(){' .
85            ' document.getElementById("' . e($id) . '-inline").classList.toggle("d-none");' .
86            ' document.getElementById("' . e($id) . '-shared").classList.toggle("d-none");' .
87            ' const inline = document.getElementById("' . e($id) . '-inline").querySelector("textarea");' .
88            ' const shared = document.getElementById("' . e($id) . '-shared").querySelector("select");' .
89            ' inline.disabled = !inline.disabled;' .
90            ' shared.disabled = !shared.disabled;' .
91            ' if (shared.disabled) { shared.tomselect.disable(); } else { shared.tomselect.enable(); }' .
92            '})' .
93            '</script>';
94    }
95
96    /**
97     * Create a label/value pair for this element.
98     *
99     * @param string $value
100     * @param Tree   $tree
101     *
102     * @return string
103     */
104    public function labelValue(string $value, Tree $tree): string
105    {
106        $id       = Registry::idFactory()->id();
107        $expanded = $tree->getPreference('EXPAND_NOTES') === '1';
108
109        // A note structure can contain an inline note or a linked to a shared note.
110        if (preg_match('/^@(' . Gedcom::REGEX_XREF . ')@$/', $value, $match) === 1) {
111            $note = Registry::noteFactory()->make($match[1], $tree);
112
113            if ($note === null) {
114                return parent::labelValue($value, $tree);
115            }
116
117            if (!$note->canShow()) {
118                return '';
119            }
120
121            $label         = '<span class="label">' . I18N::translate('Shared note') . '</span>';
122            $value         = $note->getNote();
123            $html          = $this->valueFormatted($value, $tree);
124            $first_line    = '<a href="' . e($note->url()) . '">' . $note->fullName() . '</a>';
125
126            // Shared note where the title is the same as the text
127            if ($html === '<p>' . strip_tags($note->fullName()) . '</p>') {
128                $value = '<a href="' . e($note->url()) . '">' . strip_tags($html) . '</a>';
129
130                return '<div>' . I18N::translate('%1$s: %2$s', $label, $value) . '</div>';
131            }
132
133            return
134                '<div class="wt-text-overflow-elipsis">' .
135                '<button type="button" class="btn btn-text p-0" href="#' . e($id) . '" data-bs-toggle="collapse" aria-controls="' . e($id) . '" aria-expanded="' . ($expanded ? 'true' : 'false') . '">' .
136                view('icons/expand') .
137                view('icons/collapse') .
138                '</button> ' .
139                '<span class="label">' . $label . ':</span> ' . $first_line .
140                '</div>' .
141                '<div id="' . e($id) . '" class="ps-4 collapse ut' . ($expanded ? 'show' : '') . '">' .
142                $html .
143                '</div>';
144        }
145
146        $label = '<span class="label">' . I18N::translate('Note') . '</span>';
147        $html  = $this->valueFormatted($value, $tree);
148
149        // Inline note with only one paragraph and inline markup?
150        if ($html === strip_tags($html, ['a', 'em', 'p', 'strong']) && substr_count($html, '<p>') === 1) {
151            $html  = strip_tags($html, ['a', 'em', 'strong']);
152            $value = '<span class="ut">' . $html . '</span>';
153
154            return '<div>' . I18N::translate('%1$s: %2$s', $label, $value) . '</div>';
155        }
156
157        $value = e(Note::firstLineOfTextFromHtml($html));
158        $value = '<span class="ut collapse ' . ($expanded ? '' : 'show') . ' ' . e($id) . '">' . $value . '</span>';
159
160        return
161            '<div class="wt-text-overflow-elipsis">' .
162            '<button type="button" class="btn btn-text p-0" href="#" data-bs-target=".' . e($id) . '" data-bs-toggle="collapse" aria-controls="' . e($id) . '" aria-expanded="' . ($expanded ? 'true' : 'false') . '">' .
163            view('icons/expand') .
164            view('icons/collapse') .
165            '</button> ' .
166            I18N::translate('%1$s: %2$s', $label, $value) .
167            '</div>' .
168            '<div class="ps-4 collapse ut ' . ($expanded ? 'show' : '') . ' ' . e($id) . '">' .
169            $html .
170            '</div>';
171    }
172
173    /**
174     * Display the value of this type of element.
175     *
176     * @param string $value
177     * @param Tree   $tree
178     *
179     * @return string
180     */
181    public function value(string $value, Tree $tree): string
182    {
183        if (preg_match('/^@(' . Gedcom::REGEX_XREF . ')@$/', $value, $match) === 1) {
184            $note = Registry::noteFactory()->make($match[1], $tree);
185
186            if ($note instanceof Note) {
187                $value = $note->getNote();
188            }
189        }
190
191        return parent::value($value, $tree);
192    }
193}
194