xref: /webtrees/app/Elements/NoteStructure.php (revision 63a2c22b53010174339c850ccde339d45092dcf2)
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
22*63a2c22bSGreg Roachuse Fisharebest\Webtrees\Factories\MarkdownFactory;
23e8ded2caSGreg Roachuse Fisharebest\Webtrees\Gedcom;
24e8ded2caSGreg Roachuse Fisharebest\Webtrees\I18N;
25b315f3e1SGreg Roachuse Fisharebest\Webtrees\Registry;
26e8ded2caSGreg Roachuse Fisharebest\Webtrees\Tree;
27b315f3e1SGreg Roachuse Illuminate\Support\Str;
28b315f3e1SGreg Roachuse Ramsey\Uuid\Uuid;
29e8ded2caSGreg Roach
30e8ded2caSGreg Roachuse function e;
31b315f3e1SGreg Roachuse function explode;
32e8ded2caSGreg Roachuse function preg_match;
33b315f3e1SGreg Roachuse function strip_tags;
34b315f3e1SGreg Roachuse function view;
35e8ded2caSGreg Roach
36c2ed51d1SGreg Roach/**
37c2ed51d1SGreg Roach * NOTE can be text or an XREF.
38c2ed51d1SGreg Roach */
39b315f3e1SGreg Roachclass NoteStructure extends SubmitterText
40c2ed51d1SGreg Roach{
41c2ed51d1SGreg Roach    /**
42e8ded2caSGreg Roach     * An edit control for this data.
43e8ded2caSGreg Roach     *
44e8ded2caSGreg Roach     * @param string $id
45e8ded2caSGreg Roach     * @param string $name
46e8ded2caSGreg Roach     * @param string $value
47e8ded2caSGreg Roach     * @param Tree   $tree
48e8ded2caSGreg Roach     *
49e8ded2caSGreg Roach     * @return string
50e8ded2caSGreg Roach     */
51e8ded2caSGreg Roach    public function edit(string $id, string $name, string $value, Tree $tree): string
52e8ded2caSGreg Roach    {
53e8ded2caSGreg Roach        $submitter_text = new SubmitterText('');
54e8ded2caSGreg Roach        $xref_note      = new XrefNote('');
55e8ded2caSGreg Roach
56e8ded2caSGreg Roach        // Existing shared note.
5773e16a1dSGreg Roach        if (preg_match('/^@' . Gedcom::REGEX_XREF . '@$/', $value)) {
58e8ded2caSGreg Roach            return $xref_note->edit($id, $name, $value, $tree);
59e8ded2caSGreg Roach        }
60e8ded2caSGreg Roach
61e8ded2caSGreg Roach        // Existing inline note.
62e8ded2caSGreg Roach        if ($value !== '') {
63e8ded2caSGreg Roach            return $submitter_text->edit($id, $name, $value, $tree);
64e8ded2caSGreg Roach        }
65e8ded2caSGreg Roach
66e8ded2caSGreg Roach        $options = [
677f85bc37SGreg Roach            'inline' => I18N::translate('inline note'),
687f85bc37SGreg Roach            'shared' => I18N::translate('shared note'),
69e8ded2caSGreg Roach        ];
70e8ded2caSGreg Roach
71e8ded2caSGreg Roach        // New note - either inline or shared
72e8ded2caSGreg Roach        return
73e8ded2caSGreg Roach            '<div id="' . e($id) . '-note-structure">' .
74e8ded2caSGreg Roach            '<div id="' . e($id) . '-options">' .
75e8ded2caSGreg Roach            view('components/radios-inline', ['name' => $id . '-options', 'options' => $options, 'selected' => 'inline']) .
76e8ded2caSGreg Roach            '</div>' .
77e8ded2caSGreg Roach            '<div id="' . e($id) . '-inline">' .
78e8ded2caSGreg Roach            $submitter_text->edit($id, $name, $value, $tree) .
79e8ded2caSGreg Roach            '</div>' .
80e8ded2caSGreg Roach            '<div id="' . e($id) . '-shared" class="d-none">' .
81c1f104abSGreg Roach            $xref_note->edit($id . '-select', $name, $value, $tree) .
82e8ded2caSGreg Roach            '</div>' .
83e8ded2caSGreg Roach            '</div>' .
84e8ded2caSGreg Roach            '<script>' .
85e8ded2caSGreg Roach            'document.getElementById("' . e($id) . '-shared").querySelector("select").disabled=true;' .
86e8ded2caSGreg Roach            'document.getElementById("' . e($id) . '-options").addEventListener("change", function(){' .
87e8ded2caSGreg Roach            ' document.getElementById("' . e($id) . '-inline").classList.toggle("d-none");' .
88e8ded2caSGreg Roach            ' document.getElementById("' . e($id) . '-shared").classList.toggle("d-none");' .
89e8ded2caSGreg Roach            ' const inline = document.getElementById("' . e($id) . '-inline").querySelector("textarea");' .
90e8ded2caSGreg Roach            ' const shared = document.getElementById("' . e($id) . '-shared").querySelector("select");' .
91e8ded2caSGreg Roach            ' inline.disabled = !inline.disabled;' .
92e8ded2caSGreg Roach            ' shared.disabled = !shared.disabled;' .
9343b88982SGreg Roach            ' if (shared.disabled) { shared.tomselect.disable(); } else { shared.tomselect.enable(); }' .
94e8ded2caSGreg Roach            '})' .
95e8ded2caSGreg Roach            '</script>';
96e8ded2caSGreg Roach    }
97b315f3e1SGreg Roach
98b315f3e1SGreg Roach    /**
99b315f3e1SGreg Roach     * Create a label/value pair for this element.
100b315f3e1SGreg Roach     *
101b315f3e1SGreg Roach     * @param string $value
102b315f3e1SGreg Roach     * @param Tree   $tree
103b315f3e1SGreg Roach     *
104b315f3e1SGreg Roach     * @return string
105b315f3e1SGreg Roach     */
106b315f3e1SGreg Roach    public function labelValue(string $value, Tree $tree): string
107b315f3e1SGreg Roach    {
108b315f3e1SGreg Roach        // A note structure can contain an inline note or a linked to a shared note.
109b315f3e1SGreg Roach        if (preg_match('/^@(' . Gedcom::REGEX_XREF . ')@$/', $value, $match) === 1) {
110b315f3e1SGreg Roach            $note = Registry::noteFactory()->make($match[1], $tree);
111b315f3e1SGreg Roach
112b315f3e1SGreg Roach            if ($note === null) {
113b315f3e1SGreg Roach                return parent::labelValue($value, $tree);
114b315f3e1SGreg Roach            }
115b315f3e1SGreg Roach
116b315f3e1SGreg Roach            $value         = $note->getNote();
117*63a2c22bSGreg Roach            $label         = I18N::translate('Shared note');
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);
124*63a2c22bSGreg Roach            [$first_line]  = explode(MarkdownFactory::BREAK, strip_tags($html, ['br']));
125b315f3e1SGreg Roach            $first_line    = Str::limit($first_line, 100, I18N::translate('…'));
126*63a2c22bSGreg Roach            $one_line_only = !str_contains($html, MarkdownFactory::BREAK);
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) {
13306c87791SGreg Roach            $label = '<span class="label">' . $label . '</span>';
13406c87791SGreg Roach            $value = '<span class="field" dir="auto">' . $html . '</span>';
13506c87791SGreg Roach
13606c87791SGreg 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