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