xref: /webtrees/app/Elements/NoteStructure.php (revision c7d242d15f4b9cc420d2406de4640d3ce5fafcfa)
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
2263a2c22bSGreg Roachuse Fisharebest\Webtrees\Factories\MarkdownFactory;
23e8ded2caSGreg Roachuse Fisharebest\Webtrees\Gedcom;
24e8ded2caSGreg Roachuse Fisharebest\Webtrees\I18N;
252de327daSGreg Roachuse Fisharebest\Webtrees\Note;
26b315f3e1SGreg Roachuse Fisharebest\Webtrees\Registry;
27e8ded2caSGreg Roachuse Fisharebest\Webtrees\Tree;
28b315f3e1SGreg Roachuse Illuminate\Support\Str;
29b315f3e1SGreg Roachuse Ramsey\Uuid\Uuid;
30e8ded2caSGreg Roach
31e8ded2caSGreg Roachuse function e;
32b315f3e1SGreg Roachuse function explode;
33e8ded2caSGreg Roachuse function preg_match;
34b315f3e1SGreg Roachuse function strip_tags;
352de327daSGreg Roachuse function substr_count;
36b315f3e1SGreg Roachuse function view;
37e8ded2caSGreg Roach
38c2ed51d1SGreg Roach/**
39c2ed51d1SGreg Roach * NOTE can be text or an XREF.
40c2ed51d1SGreg Roach */
41b315f3e1SGreg Roachclass NoteStructure extends SubmitterText
42c2ed51d1SGreg Roach{
43c2ed51d1SGreg Roach    /**
44e8ded2caSGreg Roach     * An edit control for this data.
45e8ded2caSGreg Roach     *
46e8ded2caSGreg Roach     * @param string $id
47e8ded2caSGreg Roach     * @param string $name
48e8ded2caSGreg Roach     * @param string $value
49e8ded2caSGreg Roach     * @param Tree   $tree
50e8ded2caSGreg Roach     *
51e8ded2caSGreg Roach     * @return string
52e8ded2caSGreg Roach     */
53e8ded2caSGreg Roach    public function edit(string $id, string $name, string $value, Tree $tree): string
54e8ded2caSGreg Roach    {
55e8ded2caSGreg Roach        $submitter_text = new SubmitterText('');
56e8ded2caSGreg Roach        $xref_note      = new XrefNote('');
57e8ded2caSGreg Roach
58e8ded2caSGreg Roach        // Existing shared note.
5973e16a1dSGreg Roach        if (preg_match('/^@' . Gedcom::REGEX_XREF . '@$/', $value)) {
60e8ded2caSGreg Roach            return $xref_note->edit($id, $name, $value, $tree);
61e8ded2caSGreg Roach        }
62e8ded2caSGreg Roach
63e8ded2caSGreg Roach        // Existing inline note.
64e8ded2caSGreg Roach        if ($value !== '') {
65e8ded2caSGreg Roach            return $submitter_text->edit($id, $name, $value, $tree);
66e8ded2caSGreg Roach        }
67e8ded2caSGreg Roach
68e8ded2caSGreg Roach        $options = [
697f85bc37SGreg Roach            'inline' => I18N::translate('inline note'),
707f85bc37SGreg Roach            'shared' => I18N::translate('shared note'),
71e8ded2caSGreg Roach        ];
72e8ded2caSGreg Roach
73e8ded2caSGreg Roach        // New note - either inline or shared
74e8ded2caSGreg Roach        return
75e8ded2caSGreg Roach            '<div id="' . e($id) . '-note-structure">' .
76e8ded2caSGreg Roach            '<div id="' . e($id) . '-options">' .
77e8ded2caSGreg Roach            view('components/radios-inline', ['name' => $id . '-options', 'options' => $options, 'selected' => 'inline']) .
78e8ded2caSGreg Roach            '</div>' .
79e8ded2caSGreg Roach            '<div id="' . e($id) . '-inline">' .
80e8ded2caSGreg Roach            $submitter_text->edit($id, $name, $value, $tree) .
81e8ded2caSGreg Roach            '</div>' .
82e8ded2caSGreg Roach            '<div id="' . e($id) . '-shared" class="d-none">' .
83c1f104abSGreg Roach            $xref_note->edit($id . '-select', $name, $value, $tree) .
84e8ded2caSGreg Roach            '</div>' .
85e8ded2caSGreg Roach            '</div>' .
86e8ded2caSGreg Roach            '<script>' .
87e8ded2caSGreg Roach            'document.getElementById("' . e($id) . '-shared").querySelector("select").disabled=true;' .
88e8ded2caSGreg Roach            'document.getElementById("' . e($id) . '-options").addEventListener("change", function(){' .
89e8ded2caSGreg Roach            ' document.getElementById("' . e($id) . '-inline").classList.toggle("d-none");' .
90e8ded2caSGreg Roach            ' document.getElementById("' . e($id) . '-shared").classList.toggle("d-none");' .
91e8ded2caSGreg Roach            ' const inline = document.getElementById("' . e($id) . '-inline").querySelector("textarea");' .
92e8ded2caSGreg Roach            ' const shared = document.getElementById("' . e($id) . '-shared").querySelector("select");' .
93e8ded2caSGreg Roach            ' inline.disabled = !inline.disabled;' .
94e8ded2caSGreg Roach            ' shared.disabled = !shared.disabled;' .
9543b88982SGreg Roach            ' if (shared.disabled) { shared.tomselect.disable(); } else { shared.tomselect.enable(); }' .
96e8ded2caSGreg Roach            '})' .
97e8ded2caSGreg Roach            '</script>';
98e8ded2caSGreg Roach    }
99b315f3e1SGreg Roach
100b315f3e1SGreg Roach    /**
101b315f3e1SGreg Roach     * Create a label/value pair for this element.
102b315f3e1SGreg Roach     *
103b315f3e1SGreg Roach     * @param string $value
104b315f3e1SGreg Roach     * @param Tree   $tree
105b315f3e1SGreg Roach     *
106b315f3e1SGreg Roach     * @return string
107b315f3e1SGreg Roach     */
108b315f3e1SGreg Roach    public function labelValue(string $value, Tree $tree): string
109b315f3e1SGreg Roach    {
1102e464181SGreg Roach        $id       = Registry::idFactory()->id();
1112de327daSGreg Roach        $expanded = $tree->getPreference('EXPAND_NOTES') === '1';
1122de327daSGreg Roach
113b315f3e1SGreg Roach        // A note structure can contain an inline note or a linked to a shared note.
114b315f3e1SGreg Roach        if (preg_match('/^@(' . Gedcom::REGEX_XREF . ')@$/', $value, $match) === 1) {
115b315f3e1SGreg Roach            $note = Registry::noteFactory()->make($match[1], $tree);
116b315f3e1SGreg Roach
117b315f3e1SGreg Roach            if ($note === null) {
118b315f3e1SGreg Roach                return parent::labelValue($value, $tree);
119b315f3e1SGreg Roach            }
120b315f3e1SGreg Roach
121*c7d242d1SGreg Roach            if (!$note->canShow()) {
122*c7d242d1SGreg Roach                return '';
123*c7d242d1SGreg Roach            }
124*c7d242d1SGreg Roach
1252de327daSGreg Roach            $label         = '<span class="label">' . I18N::translate('Shared note') . '</span>';
126b315f3e1SGreg Roach            $value         = $note->getNote();
127b315f3e1SGreg Roach            $html          = $this->valueFormatted($value, $tree);
128b315f3e1SGreg Roach            $first_line    = '<a href="' . e($note->url()) . '">' . $note->fullName() . '</a>';
129b315f3e1SGreg Roach
1302de327daSGreg Roach            // Shared note where the title is the same as the text
1312de327daSGreg Roach            if ($html === '<p>' . strip_tags($note->fullName()) . '</p>') {
1322de327daSGreg Roach                $value = '<a href="' . e($note->url()) . '">' . strip_tags($html) . '</a>';
133b315f3e1SGreg Roach
13445a1e407SGreg Roach                return '<div>' . I18N::translate('%1$s: %2$s', $label, $value) . '</div>';
135b315f3e1SGreg Roach            }
136b315f3e1SGreg Roach
137b315f3e1SGreg Roach            return
1382de327daSGreg Roach                '<div class="wt-text-overflow-elipsis">' .
139ce30781dSGreg 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') . '">' .
140b315f3e1SGreg Roach                view('icons/expand') .
141b315f3e1SGreg Roach                view('icons/collapse') .
142ce30781dSGreg Roach                '</button> ' .
143b315f3e1SGreg Roach                '<span class="label">' . $label . ':</span> ' . $first_line .
144b315f3e1SGreg Roach                '</div>' .
145b315f3e1SGreg Roach                '<div id="' . e($id) . '" class="ps-4 collapse ' . ($expanded ? 'show' : '') . '">' .
146b315f3e1SGreg Roach                $html .
147b315f3e1SGreg Roach                '</div>';
148b315f3e1SGreg Roach        }
1492de327daSGreg Roach
1502de327daSGreg Roach        $label = '<span class="label">' . I18N::translate('Note') . '</span>';
1512de327daSGreg Roach        $html  = $this->valueFormatted($value, $tree);
1522de327daSGreg Roach
1532de327daSGreg Roach        // Inline note with only one paragraph and inline markup?
1542de327daSGreg Roach        if ($html === strip_tags($html, ['a', 'em', 'p', 'strong']) && substr_count($html, '<p>') === 1) {
1552de327daSGreg Roach            $html  = strip_tags($html, ['a', 'em', 'strong']);
1562de327daSGreg Roach            $value = '<span class="ut">' . $html . '</span>';
1572de327daSGreg Roach
15845a1e407SGreg Roach            return '<div>' . I18N::translate('%1$s: %2$s', $label, $value) . '</div>';
1592de327daSGreg Roach        }
1602de327daSGreg Roach
1612de327daSGreg Roach        $value = e(Note::firstLineOfTextFromHtml($html));
1622de327daSGreg Roach        $value = '<span class="ut collapse ' . ($expanded ? '' : 'show') . ' ' . e($id) . '">' . $value . '</span>';
1632de327daSGreg Roach
1642de327daSGreg Roach        return
1652de327daSGreg Roach            '<div class="wt-text-overflow-elipsis">' .
166ce30781dSGreg 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') . '">' .
1672de327daSGreg Roach            view('icons/expand') .
1682de327daSGreg Roach            view('icons/collapse') .
169ce30781dSGreg Roach            '</button> ' .
1702de327daSGreg Roach            I18N::translate('%1$s: %2$s', $label, $value) .
1712de327daSGreg Roach            '</div>' .
1722de327daSGreg Roach            '<div class="ps-4 collapse ' . ($expanded ? 'show' : '') . ' ' . e($id) . '">' .
1732de327daSGreg Roach            $html .
1742de327daSGreg Roach            '</div>';
1752de327daSGreg Roach    }
176ae1057f3SGreg Roach
177ae1057f3SGreg Roach    /**
178ae1057f3SGreg Roach     * Display the value of this type of element.
179ae1057f3SGreg Roach     *
180ae1057f3SGreg Roach     * @param string $value
181ae1057f3SGreg Roach     * @param Tree   $tree
182ae1057f3SGreg Roach     *
183ae1057f3SGreg Roach     * @return string
184ae1057f3SGreg Roach     */
185ae1057f3SGreg Roach    public function value(string $value, Tree $tree): string
186ae1057f3SGreg Roach    {
187ae1057f3SGreg Roach        if (preg_match('/^@(' . Gedcom::REGEX_XREF . ')@$/', $value, $match) === 1) {
188ae1057f3SGreg Roach            $note = Registry::noteFactory()->make($match[1], $tree);
189ae1057f3SGreg Roach
190ae1057f3SGreg Roach            if ($note instanceof Note) {
191ae1057f3SGreg Roach                $value = $note->getNote();
192ae1057f3SGreg Roach            }
193ae1057f3SGreg Roach        }
194ae1057f3SGreg Roach
195ae1057f3SGreg Roach        return parent::value($value, $tree);
196ae1057f3SGreg Roach    }
197c2ed51d1SGreg Roach}
198