Passed
Push — main ( ec1094...ae1057 )
by Greg
06:27
created

NoteStructure::value()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 2
dl 0
loc 11
rs 10
c 0
b 0
f 0
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
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Elements;
21
22
use Fisharebest\Webtrees\Factories\MarkdownFactory;
23
use Fisharebest\Webtrees\Gedcom;
24
use Fisharebest\Webtrees\I18N;
25
use Fisharebest\Webtrees\Note;
26
use Fisharebest\Webtrees\Registry;
27
use Fisharebest\Webtrees\Tree;
28
use Illuminate\Support\Str;
29
use Ramsey\Uuid\Uuid;
30
31
use function e;
32
use function explode;
33
use function preg_match;
34
use function strip_tags;
35
use function substr_count;
36
use function view;
37
38
/**
39
 * NOTE can be text or an XREF.
40
 */
41
class 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
            $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 ' . ($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 ' . ($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