Passed
Push — dev ( f79383...5f589d )
by Greg
06:20
created

NoteStructure::labelValue()   B

Complexity

Conditions 8
Paths 17

Size

Total Lines 50
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 37
nc 17
nop 2
dl 0
loc 50
rs 8.0835
c 0
b 0
f 0
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
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Elements;
21
22
use Fisharebest\Webtrees\Gedcom;
23
use Fisharebest\Webtrees\I18N;
24
use Fisharebest\Webtrees\Registry;
25
use Fisharebest\Webtrees\Tree;
26
use Illuminate\Support\Str;
27
use Ramsey\Uuid\Uuid;
28
29
use function e;
30
use function explode;
31
use function preg_match;
32
use function strip_tags;
33
use function view;
34
35
/**
36
 * NOTE can be text or an XREF.
37
 */
38
class NoteStructure extends AbstractElement
39
{
40
    /**
41
     * Convert a value to a canonical form.
42
     *
43
     * @param string $value
44
     *
45
     * @return string
46
     */
47
    public function canonical(string $value): string
48
    {
49
        return $this->canonicalText($value);
50
    }
51
52
    /**
53
     * An edit control for this data.
54
     *
55
     * @param string $id
56
     * @param string $name
57
     * @param string $value
58
     * @param Tree   $tree
59
     *
60
     * @return string
61
     */
62
    public function edit(string $id, string $name, string $value, Tree $tree): string
63
    {
64
        $submitter_text = new SubmitterText('');
65
        $xref_note      = new XrefNote('');
66
67
        // Existing shared note.
68
        if (preg_match('/^@' . Gedcom::REGEX_XREF . '@$/', $value)) {
69
            return $xref_note->edit($id, $name, $value, $tree);
70
        }
71
72
        // Existing inline note.
73
        if ($value !== '') {
74
            return $submitter_text->edit($id, $name, $value, $tree);
75
        }
76
77
        $options = [
78
            'inline' => I18N::translate('inline note'),
79
            'shared' => I18N::translate('shared note'),
80
        ];
81
82
        // New note - either inline or shared
83
        return
84
            '<div id="' . e($id) . '-note-structure">' .
85
            '<div id="' . e($id) . '-options">' .
86
            view('components/radios-inline', ['name' => $id . '-options', 'options' => $options, 'selected' => 'inline']) .
87
            '</div>' .
88
            '<div id="' . e($id) . '-inline">' .
89
            $submitter_text->edit($id, $name, $value, $tree) .
90
            '</div>' .
91
            '<div id="' . e($id) . '-shared" class="d-none">' .
92
            $xref_note->edit($id . '-select', $name, $value, $tree) .
93
            '</div>' .
94
            '</div>' .
95
            '<script>' .
96
            'document.getElementById("' . e($id) . '-shared").querySelector("select").disabled=true;' .
97
            'document.getElementById("' . e($id) . '-options").addEventListener("change", function(){' .
98
            ' document.getElementById("' . e($id) . '-inline").classList.toggle("d-none");' .
99
            ' document.getElementById("' . e($id) . '-shared").classList.toggle("d-none");' .
100
            ' const inline = document.getElementById("' . e($id) . '-inline").querySelector("textarea");' .
101
            ' const shared = document.getElementById("' . e($id) . '-shared").querySelector("select");' .
102
            ' inline.disabled = !inline.disabled;' .
103
            ' shared.disabled = !shared.disabled;' .
104
            '})' .
105
            '</script>';
106
    }
107
108
    /**
109
     * Create a label/value pair for this element.
110
     *
111
     * @param string $value
112
     * @param Tree   $tree
113
     *
114
     * @return string
115
     */
116
    public function labelValue(string $value, Tree $tree): string
117
    {
118
        // A note structure can contain an inline note or a linked to a shared note.
119
        if (preg_match('/^@(' . Gedcom::REGEX_XREF . ')@$/', $value, $match) === 1) {
120
            $note    = Registry::noteFactory()->make($match[1], $tree);
121
122
            if ($note === null) {
123
                return parent::labelValue($value, $tree);
124
            }
125
126
            if (!$note->canShow()) {
127
                return '';
128
            }
129
130
            $value         = $note->getNote();
131
            $element       = Registry::elementFactory()->make('NOTE');
132
            $label         = $element->label();
133
            $html          = $element->value($value, $tree);
134
            $first_line    = '<a href="' . e($note->url()) . '">' . $note->fullName() . '</a>';
135
            $one_line_only = strip_tags($note->fullName()) === strip_tags($value);
136
        } else {
137
            $element       = $this;
138
            $label         = I18N::translate('Note');
139
            $html          = $element->value($value, $tree);
140
            [$first_line]  = explode("\n", strip_tags($html));
141
            $first_line    = Str::limit($first_line, 100, I18N::translate('…'));
142
            $one_line_only = !str_contains($value, "\n") && mb_strlen($value) <= 100;
143
        }
144
145
        $id       = 'collapse-' . Uuid::uuid4()->toString();
146
        $expanded = $tree->getPreference('EXPAND_NOTES') === '1';
147
148
         if ($one_line_only) {
149
             return
150
                 '<div class="fact_NOTE">' .
151
                 I18N::translate('<span class="label">%1$s:</span> <span class="field" dir="auto">%2$s</span>', $label, $html) .
152
                 '</div>';
153
         }
154
155
        return
156
            '<div class="fact_NOTE">' .
157
            '<a href="#' . e($id) . '" role="button" data-bs-toggle="collapse" aria-controls="' . e($id) . '" aria-expanded="' . ($expanded ? 'true' : 'false') . '">' .
158
            view('icons/expand') .
159
            view('icons/collapse') .
160
            '</a>' .
161
            '<span class="label">' . $label . ':</span> ' . $first_line .
162
            '</div>' .
163
            '<div id="' . e($id) . '" class="collapse ' . ($expanded ? 'show' : '') . '">' .
164
            $html .
165
            '</div>';
166
    }
167
}
168