xref: /webtrees/app/Elements/NoteStructure.php (revision 18f53ad36051375155568c08b5e93a2a2d040a03)
1c2ed51d1SGreg Roach<?php
2c2ed51d1SGreg Roach
3c2ed51d1SGreg Roach/**
4c2ed51d1SGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 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;
242de327daSGreg Roachuse Fisharebest\Webtrees\Note;
25b315f3e1SGreg Roachuse Fisharebest\Webtrees\Registry;
26e8ded2caSGreg Roachuse Fisharebest\Webtrees\Tree;
27e8ded2caSGreg Roach
28e8ded2caSGreg Roachuse function e;
29e8ded2caSGreg Roachuse function preg_match;
30b315f3e1SGreg Roachuse function strip_tags;
312de327daSGreg Roachuse function substr_count;
32b315f3e1SGreg Roachuse function view;
33e8ded2caSGreg Roach
34c2ed51d1SGreg Roach/**
35c2ed51d1SGreg Roach * NOTE can be text or an XREF.
36c2ed51d1SGreg Roach */
37b315f3e1SGreg Roachclass NoteStructure extends SubmitterText
38c2ed51d1SGreg Roach{
39c2ed51d1SGreg Roach    /**
40e8ded2caSGreg Roach     * An edit control for this data.
41e8ded2caSGreg Roach     *
42e8ded2caSGreg Roach     * @param string $id
43e8ded2caSGreg Roach     * @param string $name
44e8ded2caSGreg Roach     * @param string $value
45e8ded2caSGreg Roach     * @param Tree   $tree
46e8ded2caSGreg Roach     *
47e8ded2caSGreg Roach     * @return string
48e8ded2caSGreg Roach     */
49e8ded2caSGreg Roach    public function edit(string $id, string $name, string $value, Tree $tree): string
50e8ded2caSGreg Roach    {
51e8ded2caSGreg Roach        $submitter_text = new SubmitterText('');
52e8ded2caSGreg Roach        $xref_note      = new XrefNote('');
53e8ded2caSGreg Roach
54e8ded2caSGreg Roach        // Existing shared note.
5573e16a1dSGreg Roach        if (preg_match('/^@' . Gedcom::REGEX_XREF . '@$/', $value)) {
56e8ded2caSGreg Roach            return $xref_note->edit($id, $name, $value, $tree);
57e8ded2caSGreg Roach        }
58e8ded2caSGreg Roach
59e8ded2caSGreg Roach        // Existing inline note.
60e8ded2caSGreg Roach        if ($value !== '') {
61e8ded2caSGreg Roach            return $submitter_text->edit($id, $name, $value, $tree);
62e8ded2caSGreg Roach        }
63e8ded2caSGreg Roach
64e8ded2caSGreg Roach        $options = [
657f85bc37SGreg Roach            'inline' => I18N::translate('inline note'),
667f85bc37SGreg Roach            'shared' => I18N::translate('shared note'),
67e8ded2caSGreg Roach        ];
68e8ded2caSGreg Roach
69e8ded2caSGreg Roach        // New note - either inline or shared
70e8ded2caSGreg Roach        return
71e8ded2caSGreg Roach            '<div id="' . e($id) . '-note-structure">' .
72e8ded2caSGreg Roach            '<div id="' . e($id) . '-options">' .
73e8ded2caSGreg Roach            view('components/radios-inline', ['name' => $id . '-options', 'options' => $options, 'selected' => 'inline']) .
74e8ded2caSGreg Roach            '</div>' .
75e8ded2caSGreg Roach            '<div id="' . e($id) . '-inline">' .
76e8ded2caSGreg Roach            $submitter_text->edit($id, $name, $value, $tree) .
77e8ded2caSGreg Roach            '</div>' .
78e8ded2caSGreg Roach            '<div id="' . e($id) . '-shared" class="d-none">' .
79c1f104abSGreg Roach            $xref_note->edit($id . '-select', $name, $value, $tree) .
80e8ded2caSGreg Roach            '</div>' .
81e8ded2caSGreg Roach            '</div>' .
82e8ded2caSGreg Roach            '<script>' .
83e8ded2caSGreg Roach            'document.getElementById("' . e($id) . '-shared").querySelector("select").disabled=true;' .
84e8ded2caSGreg Roach            'document.getElementById("' . e($id) . '-options").addEventListener("change", function(){' .
85e8ded2caSGreg Roach            ' document.getElementById("' . e($id) . '-inline").classList.toggle("d-none");' .
86e8ded2caSGreg Roach            ' document.getElementById("' . e($id) . '-shared").classList.toggle("d-none");' .
87e8ded2caSGreg Roach            ' const inline = document.getElementById("' . e($id) . '-inline").querySelector("textarea");' .
88e8ded2caSGreg Roach            ' const shared = document.getElementById("' . e($id) . '-shared").querySelector("select");' .
89e8ded2caSGreg Roach            ' inline.disabled = !inline.disabled;' .
90e8ded2caSGreg Roach            ' shared.disabled = !shared.disabled;' .
9143b88982SGreg Roach            ' if (shared.disabled) { shared.tomselect.disable(); } else { shared.tomselect.enable(); }' .
92e8ded2caSGreg Roach            '})' .
93e8ded2caSGreg Roach            '</script>';
94e8ded2caSGreg Roach    }
95b315f3e1SGreg Roach
96b315f3e1SGreg Roach    /**
97b315f3e1SGreg Roach     * Create a label/value pair for this element.
98b315f3e1SGreg Roach     *
99b315f3e1SGreg Roach     * @param string $value
100b315f3e1SGreg Roach     * @param Tree   $tree
101b315f3e1SGreg Roach     *
102b315f3e1SGreg Roach     * @return string
103b315f3e1SGreg Roach     */
104b315f3e1SGreg Roach    public function labelValue(string $value, Tree $tree): string
105b315f3e1SGreg Roach    {
1062e464181SGreg Roach        $id       = Registry::idFactory()->id();
1072de327daSGreg Roach        $expanded = $tree->getPreference('EXPAND_NOTES') === '1';
1082de327daSGreg Roach
109b315f3e1SGreg Roach        // A note structure can contain an inline note or a linked to a shared note.
110b315f3e1SGreg Roach        if (preg_match('/^@(' . Gedcom::REGEX_XREF . ')@$/', $value, $match) === 1) {
111b315f3e1SGreg Roach            $note = Registry::noteFactory()->make($match[1], $tree);
112b315f3e1SGreg Roach
113b315f3e1SGreg Roach            if ($note === null) {
114b315f3e1SGreg Roach                return parent::labelValue($value, $tree);
115b315f3e1SGreg Roach            }
116b315f3e1SGreg Roach
117c7d242d1SGreg Roach            if (!$note->canShow()) {
118c7d242d1SGreg Roach                return '';
119c7d242d1SGreg Roach            }
120c7d242d1SGreg Roach
1212de327daSGreg Roach            $label         = '<span class="label">' . I18N::translate('Shared note') . '</span>';
122b315f3e1SGreg Roach            $value         = $note->getNote();
123b315f3e1SGreg Roach            $html          = $this->valueFormatted($value, $tree);
124b315f3e1SGreg Roach            $first_line    = '<a href="' . e($note->url()) . '">' . $note->fullName() . '</a>';
125b315f3e1SGreg Roach
1262de327daSGreg Roach            // Shared note where the title is the same as the text
1272de327daSGreg Roach            if ($html === '<p>' . strip_tags($note->fullName()) . '</p>') {
1282de327daSGreg Roach                $value = '<a href="' . e($note->url()) . '">' . strip_tags($html) . '</a>';
129b315f3e1SGreg Roach
13045a1e407SGreg Roach                return '<div>' . I18N::translate('%1$s: %2$s', $label, $value) . '</div>';
131b315f3e1SGreg Roach            }
132b315f3e1SGreg Roach
133b315f3e1SGreg Roach            return
1342de327daSGreg Roach                '<div class="wt-text-overflow-elipsis">' .
135ce30781dSGreg Roach                '<button type="button" class="btn btn-text p-0" href="#' . e($id) . '" data-bs-toggle="collapse" aria-controls="' . e($id) . '" aria-expanded="' . ($expanded ? 'true' : 'false') . '">' .
136b315f3e1SGreg Roach                view('icons/expand') .
137b315f3e1SGreg Roach                view('icons/collapse') .
138ce30781dSGreg Roach                '</button> ' .
139b315f3e1SGreg Roach                '<span class="label">' . $label . ':</span> ' . $first_line .
140b315f3e1SGreg Roach                '</div>' .
141*18f53ad3SPawel                '<div id="' . e($id) . '" class="ps-4 collapse ut' . ($expanded ? 'show' : '') . '">' .
142b315f3e1SGreg Roach                $html .
143b315f3e1SGreg Roach                '</div>';
144b315f3e1SGreg Roach        }
1452de327daSGreg Roach
1462de327daSGreg Roach        $label = '<span class="label">' . I18N::translate('Note') . '</span>';
1472de327daSGreg Roach        $html  = $this->valueFormatted($value, $tree);
1482de327daSGreg Roach
1492de327daSGreg Roach        // Inline note with only one paragraph and inline markup?
1502de327daSGreg Roach        if ($html === strip_tags($html, ['a', 'em', 'p', 'strong']) && substr_count($html, '<p>') === 1) {
1512de327daSGreg Roach            $html  = strip_tags($html, ['a', 'em', 'strong']);
1522de327daSGreg Roach            $value = '<span class="ut">' . $html . '</span>';
1532de327daSGreg Roach
15445a1e407SGreg Roach            return '<div>' . I18N::translate('%1$s: %2$s', $label, $value) . '</div>';
1552de327daSGreg Roach        }
1562de327daSGreg Roach
1572de327daSGreg Roach        $value = e(Note::firstLineOfTextFromHtml($html));
1582de327daSGreg Roach        $value = '<span class="ut collapse ' . ($expanded ? '' : 'show') . ' ' . e($id) . '">' . $value . '</span>';
1592de327daSGreg Roach
1602de327daSGreg Roach        return
1612de327daSGreg Roach            '<div class="wt-text-overflow-elipsis">' .
162ce30781dSGreg Roach            '<button type="button" class="btn btn-text p-0" href="#" data-bs-target=".' . e($id) . '" data-bs-toggle="collapse" aria-controls="' . e($id) . '" aria-expanded="' . ($expanded ? 'true' : 'false') . '">' .
1632de327daSGreg Roach            view('icons/expand') .
1642de327daSGreg Roach            view('icons/collapse') .
165ce30781dSGreg Roach            '</button> ' .
1662de327daSGreg Roach            I18N::translate('%1$s: %2$s', $label, $value) .
1672de327daSGreg Roach            '</div>' .
168*18f53ad3SPawel            '<div class="ps-4 collapse ut ' . ($expanded ? 'show' : '') . ' ' . e($id) . '">' .
1692de327daSGreg Roach            $html .
1702de327daSGreg Roach            '</div>';
1712de327daSGreg Roach    }
172ae1057f3SGreg Roach
173ae1057f3SGreg Roach    /**
174ae1057f3SGreg Roach     * Display the value of this type of element.
175ae1057f3SGreg Roach     *
176ae1057f3SGreg Roach     * @param string $value
177ae1057f3SGreg Roach     * @param Tree   $tree
178ae1057f3SGreg Roach     *
179ae1057f3SGreg Roach     * @return string
180ae1057f3SGreg Roach     */
181ae1057f3SGreg Roach    public function value(string $value, Tree $tree): string
182ae1057f3SGreg Roach    {
183ae1057f3SGreg Roach        if (preg_match('/^@(' . Gedcom::REGEX_XREF . ')@$/', $value, $match) === 1) {
184ae1057f3SGreg Roach            $note = Registry::noteFactory()->make($match[1], $tree);
185ae1057f3SGreg Roach
186ae1057f3SGreg Roach            if ($note instanceof Note) {
187ae1057f3SGreg Roach                $value = $note->getNote();
188ae1057f3SGreg Roach            }
189ae1057f3SGreg Roach        }
190ae1057f3SGreg Roach
191ae1057f3SGreg Roach        return parent::value($value, $tree);
192ae1057f3SGreg Roach    }
193c2ed51d1SGreg Roach}
194