xref: /webtrees/app/Elements/NoteStructure.php (revision 94db7d2d27c3ad6a04fceebbdcb98e28e97649b3)
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
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            '})' .
93            '</script>';
94    }
95
96    /**
97     * Create a label/value pair for this element.
98     *
99     * @param string $value
100     * @param Tree   $tree
101     *
102     * @return string
103     */
104    public function labelValue(string $value, Tree $tree): string
105    {
106        // A note structure can contain an inline note or a linked to a shared note.
107        if (preg_match('/^@(' . Gedcom::REGEX_XREF . ')@$/', $value, $match) === 1) {
108            $note = Registry::noteFactory()->make($match[1], $tree);
109
110            if ($note === null) {
111                return parent::labelValue($value, $tree);
112            }
113
114            $value         = $note->getNote();
115            $element       = Registry::elementFactory()->make('NOTE');
116            $label         = $element->label();
117            $html          = $this->valueFormatted($value, $tree);
118            $first_line    = '<a href="' . e($note->url()) . '">' . $note->fullName() . '</a>';
119            $one_line_only = strip_tags($note->fullName()) === strip_tags($value);
120        } else {
121            $label         = I18N::translate('Note');
122            $html          = $this->valueFormatted($value, $tree);
123            [$first_line]  = explode("\n", strip_tags($html));
124            $first_line    = Str::limit($first_line, 100, I18N::translate('…'));
125            $one_line_only = !str_contains($value, "\n") && mb_strlen($value) <= 100;
126        }
127
128        $id       = 'collapse-' . Uuid::uuid4()->toString();
129        $expanded = $tree->getPreference('EXPAND_NOTES') === '1';
130
131        if ($one_line_only) {
132            return
133                 '<div class="fact_NOTE">' .
134                 I18N::translate('<span class="label">%1$s:</span> <span class="field" dir="auto">%2$s</span>', $label, $html) .
135                 '</div>';
136        }
137
138        return
139            '<div class="fact_NOTE">' .
140            '<a href="#' . e($id) . '" role="button" data-bs-toggle="collapse" aria-controls="' . e($id) . '" aria-expanded="' . ($expanded ? 'true' : 'false') . '">' .
141            view('icons/expand') .
142            view('icons/collapse') .
143            '</a>' .
144            '<span class="label">' . $label . ':</span> ' . $first_line .
145            '</div>' .
146            '<div id="' . e($id) . '" class="ps-4 collapse ' . ($expanded ? 'show' : '') . '">' .
147            $html .
148            '</div>';
149    }
150}
151