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