1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2022 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\Factories\MarkdownFactory; 23use Fisharebest\Webtrees\Gedcom; 24use Fisharebest\Webtrees\I18N; 25use Fisharebest\Webtrees\Registry; 26use Fisharebest\Webtrees\Tree; 27use Illuminate\Support\Str; 28use Ramsey\Uuid\Uuid; 29 30use function e; 31use function explode; 32use function preg_match; 33use function strip_tags; 34use function view; 35 36/** 37 * NOTE can be text or an XREF. 38 */ 39class NoteStructure extends SubmitterText 40{ 41 /** 42 * An edit control for this data. 43 * 44 * @param string $id 45 * @param string $name 46 * @param string $value 47 * @param Tree $tree 48 * 49 * @return string 50 */ 51 public function edit(string $id, string $name, string $value, Tree $tree): string 52 { 53 $submitter_text = new SubmitterText(''); 54 $xref_note = new XrefNote(''); 55 56 // Existing shared note. 57 if (preg_match('/^@' . Gedcom::REGEX_XREF . '@$/', $value)) { 58 return $xref_note->edit($id, $name, $value, $tree); 59 } 60 61 // Existing inline note. 62 if ($value !== '') { 63 return $submitter_text->edit($id, $name, $value, $tree); 64 } 65 66 $options = [ 67 'inline' => I18N::translate('inline note'), 68 'shared' => I18N::translate('shared note'), 69 ]; 70 71 // New note - either inline or shared 72 return 73 '<div id="' . e($id) . '-note-structure">' . 74 '<div id="' . e($id) . '-options">' . 75 view('components/radios-inline', ['name' => $id . '-options', 'options' => $options, 'selected' => 'inline']) . 76 '</div>' . 77 '<div id="' . e($id) . '-inline">' . 78 $submitter_text->edit($id, $name, $value, $tree) . 79 '</div>' . 80 '<div id="' . e($id) . '-shared" class="d-none">' . 81 $xref_note->edit($id . '-select', $name, $value, $tree) . 82 '</div>' . 83 '</div>' . 84 '<script>' . 85 'document.getElementById("' . e($id) . '-shared").querySelector("select").disabled=true;' . 86 'document.getElementById("' . e($id) . '-options").addEventListener("change", function(){' . 87 ' document.getElementById("' . e($id) . '-inline").classList.toggle("d-none");' . 88 ' document.getElementById("' . e($id) . '-shared").classList.toggle("d-none");' . 89 ' const inline = document.getElementById("' . e($id) . '-inline").querySelector("textarea");' . 90 ' const shared = document.getElementById("' . e($id) . '-shared").querySelector("select");' . 91 ' inline.disabled = !inline.disabled;' . 92 ' shared.disabled = !shared.disabled;' . 93 ' if (shared.disabled) { shared.tomselect.disable(); } else { shared.tomselect.enable(); }' . 94 '})' . 95 '</script>'; 96 } 97 98 /** 99 * Create a label/value pair for this element. 100 * 101 * @param string $value 102 * @param Tree $tree 103 * 104 * @return string 105 */ 106 public function labelValue(string $value, Tree $tree): string 107 { 108 // A note structure can contain an inline note or a linked to a shared note. 109 if (preg_match('/^@(' . Gedcom::REGEX_XREF . ')@$/', $value, $match) === 1) { 110 $note = Registry::noteFactory()->make($match[1], $tree); 111 112 if ($note === null) { 113 return parent::labelValue($value, $tree); 114 } 115 116 $value = $note->getNote(); 117 $label = I18N::translate('Shared note'); 118 $html = $this->valueFormatted($value, $tree); 119 $first_line = '<a href="' . e($note->url()) . '">' . $note->fullName() . '</a>'; 120 $one_line_only = strip_tags($note->fullName()) === strip_tags($value); 121 } else { 122 $label = I18N::translate('Note'); 123 $html = $this->valueFormatted($value, $tree); 124 [$first_line] = explode(MarkdownFactory::BREAK, strip_tags($html, ['br'])); 125 $first_line = Str::limit($first_line, 100, I18N::translate('…')); 126 $one_line_only = !str_contains($html, MarkdownFactory::BREAK); 127 } 128 129 $id = 'collapse-' . Uuid::uuid4()->toString(); 130 $expanded = $tree->getPreference('EXPAND_NOTES') === '1'; 131 132 if ($one_line_only) { 133 $label = '<span class="label">' . $label . '</span>'; 134 $value = '<span class="field" dir="auto">' . $html . '</span>'; 135 136 return '<div class="fact_NOTE">' . I18N::translate('%1$s: %2$s', $label, $value) . '</div>'; 137 } 138 139 return 140 '<div class="fact_NOTE">' . 141 '<a href="#' . e($id) . '" role="button" data-bs-toggle="collapse" aria-controls="' . e($id) . '" aria-expanded="' . ($expanded ? 'true' : 'false') . '">' . 142 view('icons/expand') . 143 view('icons/collapse') . 144 '</a>' . 145 '<span class="label">' . $label . ':</span> ' . $first_line . 146 '</div>' . 147 '<div id="' . e($id) . '" class="ps-4 collapse ' . ($expanded ? 'show' : '') . '">' . 148 $html . 149 '</div>'; 150 } 151} 152