xref: /webtrees/app/Elements/NoteStructure.php (revision 98ebe5e457a6f4c1618b893965b4a0f135dc8b4b)
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       = 'collapse-' . Uuid::uuid4()->toString();
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            $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 I18N::translate('%1$s: %2$s', $label, $value);
131            }
132
133            return
134                '<div class="wt-text-overflow-elipsis">' .
135                '<a href="#' . e($id) . '" role="button" data-bs-toggle="collapse" aria-controls="' . e($id) . '" aria-expanded="' . ($expanded ? 'true' : 'false') . '">' .
136                view('icons/expand') .
137                view('icons/collapse') .
138                '</a>' .
139                '<span class="label">' . $label . ':</span> ' . $first_line .
140                '</div>' .
141                '<div id="' . e($id) . '" class="ps-4 collapse ' . ($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 I18N::translate('%1$s: %2$s', $label, $value);
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            '<a href="#" data-bs-target=".' . e($id) . '" role="button" data-bs-toggle="collapse" aria-controls="' . e($id) . '" aria-expanded="' . ($expanded ? 'true' : 'false') . '">' .
163            view('icons/expand') .
164            view('icons/collapse') .
165            '</a>' .
166            I18N::translate('%1$s: %2$s', $label, $value) .
167            '</div>' .
168            '<div class="ps-4 collapse ' . ($expanded ? 'show' : '') . ' ' . e($id) . '">' .
169            $html .
170            '</div>';
171    }
172}
173