xref: /webtrees/app/Elements/NoteStructure.php (revision 06c8779186e72591468a2ed38ad37fb573924968)
1c2ed51d1SGreg Roach<?php
2c2ed51d1SGreg Roach
3c2ed51d1SGreg Roach/**
4c2ed51d1SGreg Roach * webtrees: online genealogy
55bfc6897SGreg Roach * Copyright (C) 2022 webtrees development team
6c2ed51d1SGreg Roach * This program is free software: you can redistribute it and/or modify
7c2ed51d1SGreg Roach * it under the terms of the GNU General Public License as published by
8c2ed51d1SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9c2ed51d1SGreg Roach * (at your option) any later version.
10c2ed51d1SGreg Roach * This program is distributed in the hope that it will be useful,
11c2ed51d1SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12c2ed51d1SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13c2ed51d1SGreg Roach * GNU General Public License for more details.
14c2ed51d1SGreg Roach * You should have received a copy of the GNU General Public License
15c2ed51d1SGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16c2ed51d1SGreg Roach */
17c2ed51d1SGreg Roach
18c2ed51d1SGreg Roachdeclare(strict_types=1);
19c2ed51d1SGreg Roach
20c2ed51d1SGreg Roachnamespace Fisharebest\Webtrees\Elements;
21c2ed51d1SGreg Roach
22e8ded2caSGreg Roachuse Fisharebest\Webtrees\Gedcom;
23e8ded2caSGreg Roachuse Fisharebest\Webtrees\I18N;
24b315f3e1SGreg Roachuse Fisharebest\Webtrees\Registry;
25e8ded2caSGreg Roachuse Fisharebest\Webtrees\Tree;
26b315f3e1SGreg Roachuse Illuminate\Support\Str;
27b315f3e1SGreg Roachuse Ramsey\Uuid\Uuid;
28e8ded2caSGreg Roach
29e8ded2caSGreg Roachuse function e;
30b315f3e1SGreg Roachuse function explode;
31e8ded2caSGreg Roachuse function preg_match;
32b315f3e1SGreg Roachuse function strip_tags;
33b315f3e1SGreg Roachuse function view;
34e8ded2caSGreg Roach
35c2ed51d1SGreg Roach/**
36c2ed51d1SGreg Roach * NOTE can be text or an XREF.
37c2ed51d1SGreg Roach */
38b315f3e1SGreg Roachclass NoteStructure extends SubmitterText
39c2ed51d1SGreg Roach{
40c2ed51d1SGreg Roach    /**
41e8ded2caSGreg Roach     * An edit control for this data.
42e8ded2caSGreg Roach     *
43e8ded2caSGreg Roach     * @param string $id
44e8ded2caSGreg Roach     * @param string $name
45e8ded2caSGreg Roach     * @param string $value
46e8ded2caSGreg Roach     * @param Tree   $tree
47e8ded2caSGreg Roach     *
48e8ded2caSGreg Roach     * @return string
49e8ded2caSGreg Roach     */
50e8ded2caSGreg Roach    public function edit(string $id, string $name, string $value, Tree $tree): string
51e8ded2caSGreg Roach    {
52e8ded2caSGreg Roach        $submitter_text = new SubmitterText('');
53e8ded2caSGreg Roach        $xref_note      = new XrefNote('');
54e8ded2caSGreg Roach
55e8ded2caSGreg Roach        // Existing shared note.
5673e16a1dSGreg Roach        if (preg_match('/^@' . Gedcom::REGEX_XREF . '@$/', $value)) {
57e8ded2caSGreg Roach            return $xref_note->edit($id, $name, $value, $tree);
58e8ded2caSGreg Roach        }
59e8ded2caSGreg Roach
60e8ded2caSGreg Roach        // Existing inline note.
61e8ded2caSGreg Roach        if ($value !== '') {
62e8ded2caSGreg Roach            return $submitter_text->edit($id, $name, $value, $tree);
63e8ded2caSGreg Roach        }
64e8ded2caSGreg Roach
65e8ded2caSGreg Roach        $options = [
667f85bc37SGreg Roach            'inline' => I18N::translate('inline note'),
677f85bc37SGreg Roach            'shared' => I18N::translate('shared note'),
68e8ded2caSGreg Roach        ];
69e8ded2caSGreg Roach
70e8ded2caSGreg Roach        // New note - either inline or shared
71e8ded2caSGreg Roach        return
72e8ded2caSGreg Roach            '<div id="' . e($id) . '-note-structure">' .
73e8ded2caSGreg Roach            '<div id="' . e($id) . '-options">' .
74e8ded2caSGreg Roach            view('components/radios-inline', ['name' => $id . '-options', 'options' => $options, 'selected' => 'inline']) .
75e8ded2caSGreg Roach            '</div>' .
76e8ded2caSGreg Roach            '<div id="' . e($id) . '-inline">' .
77e8ded2caSGreg Roach            $submitter_text->edit($id, $name, $value, $tree) .
78e8ded2caSGreg Roach            '</div>' .
79e8ded2caSGreg Roach            '<div id="' . e($id) . '-shared" class="d-none">' .
80c1f104abSGreg Roach            $xref_note->edit($id . '-select', $name, $value, $tree) .
81e8ded2caSGreg Roach            '</div>' .
82e8ded2caSGreg Roach            '</div>' .
83e8ded2caSGreg Roach            '<script>' .
84e8ded2caSGreg Roach            'document.getElementById("' . e($id) . '-shared").querySelector("select").disabled=true;' .
85e8ded2caSGreg Roach            'document.getElementById("' . e($id) . '-options").addEventListener("change", function(){' .
86e8ded2caSGreg Roach            ' document.getElementById("' . e($id) . '-inline").classList.toggle("d-none");' .
87e8ded2caSGreg Roach            ' document.getElementById("' . e($id) . '-shared").classList.toggle("d-none");' .
88e8ded2caSGreg Roach            ' const inline = document.getElementById("' . e($id) . '-inline").querySelector("textarea");' .
89e8ded2caSGreg Roach            ' const shared = document.getElementById("' . e($id) . '-shared").querySelector("select");' .
90e8ded2caSGreg Roach            ' inline.disabled = !inline.disabled;' .
91e8ded2caSGreg Roach            ' shared.disabled = !shared.disabled;' .
9243b88982SGreg Roach            ' if (shared.disabled) { shared.tomselect.disable(); } else { shared.tomselect.enable(); }' .
93e8ded2caSGreg Roach            '})' .
94e8ded2caSGreg Roach            '</script>';
95e8ded2caSGreg Roach    }
96b315f3e1SGreg Roach
97b315f3e1SGreg Roach    /**
98b315f3e1SGreg Roach     * Create a label/value pair for this element.
99b315f3e1SGreg Roach     *
100b315f3e1SGreg Roach     * @param string $value
101b315f3e1SGreg Roach     * @param Tree   $tree
102b315f3e1SGreg Roach     *
103b315f3e1SGreg Roach     * @return string
104b315f3e1SGreg Roach     */
105b315f3e1SGreg Roach    public function labelValue(string $value, Tree $tree): string
106b315f3e1SGreg Roach    {
107b315f3e1SGreg Roach        // A note structure can contain an inline note or a linked to a shared note.
108b315f3e1SGreg Roach        if (preg_match('/^@(' . Gedcom::REGEX_XREF . ')@$/', $value, $match) === 1) {
109b315f3e1SGreg Roach            $note = Registry::noteFactory()->make($match[1], $tree);
110b315f3e1SGreg Roach
111b315f3e1SGreg Roach            if ($note === null) {
112b315f3e1SGreg Roach                return parent::labelValue($value, $tree);
113b315f3e1SGreg Roach            }
114b315f3e1SGreg Roach
115b315f3e1SGreg Roach            $value         = $note->getNote();
116b315f3e1SGreg Roach            $element       = Registry::elementFactory()->make('NOTE');
117b315f3e1SGreg Roach            $label         = $element->label();
118b315f3e1SGreg Roach            $html          = $this->valueFormatted($value, $tree);
119b315f3e1SGreg Roach            $first_line    = '<a href="' . e($note->url()) . '">' . $note->fullName() . '</a>';
120b315f3e1SGreg Roach            $one_line_only = strip_tags($note->fullName()) === strip_tags($value);
121b315f3e1SGreg Roach        } else {
122b315f3e1SGreg Roach            $label         = I18N::translate('Note');
123b315f3e1SGreg Roach            $html          = $this->valueFormatted($value, $tree);
124db3f0368SGreg Roach            [$first_line]  = explode('<br>', strip_tags($html, ['<br>']));
125b315f3e1SGreg Roach            $first_line    = Str::limit($first_line, 100, I18N::translate('…'));
126db3f0368SGreg Roach            $one_line_only = !str_contains($html, '<br>') && mb_strlen($value) <= 100;
127b315f3e1SGreg Roach        }
128b315f3e1SGreg Roach
129b315f3e1SGreg Roach        $id       = 'collapse-' . Uuid::uuid4()->toString();
130b315f3e1SGreg Roach        $expanded = $tree->getPreference('EXPAND_NOTES') === '1';
131b315f3e1SGreg Roach
132b315f3e1SGreg Roach        if ($one_line_only) {
133*06c87791SGreg Roach            $label = '<span class="label">' . $label . '</span>';
134*06c87791SGreg Roach            $value = '<span class="field" dir="auto">' . $html . '</span>';
135*06c87791SGreg Roach
136*06c87791SGreg Roach            return '<div class="fact_NOTE">' . I18N::translate('%1$s: %2$s', $label, $value) . '</div>';
137b315f3e1SGreg Roach        }
138b315f3e1SGreg Roach
139b315f3e1SGreg Roach        return
140b315f3e1SGreg Roach            '<div class="fact_NOTE">' .
141b315f3e1SGreg Roach            '<a href="#' . e($id) . '" role="button" data-bs-toggle="collapse" aria-controls="' . e($id) . '" aria-expanded="' . ($expanded ? 'true' : 'false') . '">' .
142b315f3e1SGreg Roach            view('icons/expand') .
143b315f3e1SGreg Roach            view('icons/collapse') .
144b315f3e1SGreg Roach            '</a>' .
145b315f3e1SGreg Roach            '<span class="label">' . $label . ':</span> ' . $first_line .
146b315f3e1SGreg Roach            '</div>' .
147b315f3e1SGreg Roach            '<div id="' . e($id) . '" class="ps-4 collapse ' . ($expanded ? 'show' : '') . '">' .
148b315f3e1SGreg Roach            $html .
149b315f3e1SGreg Roach            '</div>';
150b315f3e1SGreg Roach    }
151c2ed51d1SGreg Roach}
152