1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2021 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\Contracts\ElementInterface; 23use Fisharebest\Webtrees\Html; 24use Fisharebest\Webtrees\I18N; 25use Fisharebest\Webtrees\Tree; 26use League\CommonMark\Block\Element\Document; 27use League\CommonMark\Block\Element\Paragraph; 28use League\CommonMark\Block\Renderer\DocumentRenderer; 29use League\CommonMark\Block\Renderer\ParagraphRenderer; 30use League\CommonMark\CommonMarkConverter; 31use League\CommonMark\Environment; 32use League\CommonMark\Inline\Element\Link; 33use League\CommonMark\Inline\Element\Text; 34use League\CommonMark\Inline\Parser\AutolinkParser; 35use League\CommonMark\Inline\Renderer\LinkRenderer; 36use League\CommonMark\Inline\Renderer\TextRenderer; 37 38use function array_key_exists; 39use function array_map; 40use function e; 41use function is_numeric; 42use function preg_replace; 43use function strpos; 44use function trim; 45use function view; 46 47/** 48 * A GEDCOM element is a tag/primitive in a GEDCOM file. 49 */ 50abstract class AbstractElement implements ElementInterface 51{ 52 protected const REGEX_URL = '~((https?|ftp]):)(//([^\s/?#<>]*))?([^\s?#<>]*)(\?([^\s#<>]*))?(#[^\s?#<>]+)?~'; 53 54 // HTML attributes for an <input> 55 protected const MAXIMUM_LENGTH = false; 56 protected const PATTERN = false; 57 58 // Which child elements can appear under this element. 59 protected const SUBTAGS = []; 60 61 /** @var string A label to describe this element */ 62 private $label; 63 64 /** @var array<string,string> */ 65 private $subtags; 66 67 /** 68 * AbstractGedcomElement constructor. 69 * 70 * @param string $label 71 * @param array<string>|null $subtags 72 */ 73 public function __construct(string $label, array $subtags = null) 74 { 75 $this->label = $label; 76 $this->subtags = $subtags ?? static::SUBTAGS; 77 } 78 79 /** 80 * Convert a value to a canonical form. 81 * 82 * @param string $value 83 * 84 * @return string 85 */ 86 public function canonical(string $value): string 87 { 88 $value = strtr($value, ["\t" => ' ', "\r" => ' ', "\n" => ' ']); 89 90 while (strpos($value, ' ') !== false) { 91 $value = strtr($value, [' ' => ' ']); 92 } 93 94 return trim($value); 95 } 96 97 /** 98 * Create a default value for this element. 99 * 100 * @param Tree $tree 101 * 102 * @return string 103 */ 104 public function default(Tree $tree): string 105 { 106 return ''; 107 } 108 109 /** 110 * An edit control for this data. 111 * 112 * @param string $id 113 * @param string $name 114 * @param string $value 115 * @param Tree $tree 116 * 117 * @return string 118 */ 119 public function edit(string $id, string $name, string $value, Tree $tree): string 120 { 121 $values = $this->values(); 122 123 if ($values !== []) { 124 $value = $this->canonical($value); 125 126 // Ensure the current data is in the list. 127 if (!array_key_exists($value, $values)) { 128 $values = [$value => $value] + $values; 129 } 130 131 // We may use markup to display values, but not when editing them. 132 $values = array_map('strip_tags', $values); 133 134 return view('components/select', [ 135 'id' => $id, 136 'name' => $name, 137 'options' => $values, 138 'selected' => $value, 139 ]); 140 } 141 142 $attributes = [ 143 'class' => 'form-control', 144 'type' => 'text', 145 'id' => $id, 146 'name' => $name, 147 'value' => $value, 148 'maxvalue' => static::MAXIMUM_LENGTH, 149 'pattern' => static::PATTERN, 150 ]; 151 152 return '<input ' . Html::attributes($attributes) . '">'; 153 } 154 155 /** 156 * An edit control for this data. 157 * 158 * @param string $id 159 * @param string $name 160 * @param string $value 161 * 162 * @return string 163 */ 164 public function editHidden(string $id, string $name, string $value): string 165 { 166 return '<input class="form-control" type="hidden" id="' . e($id) . '" name="' . e($name) . '" value="' . e($value) . '">'; 167 } 168 169 /** 170 * An edit control for this data. 171 * 172 * @param string $id 173 * @param string $name 174 * @param string $value 175 * 176 * @return string 177 */ 178 public function editTextArea(string $id, string $name, string $value): string 179 { 180 return '<textarea class="form-control" id="' . e($id) . '" name="' . e($name) . '" rows="5" dir="auto">' . e($value) . '</textarea>'; 181 } 182 183 /** 184 * Escape @ signs in a GEDCOM export. 185 * 186 * @param string $value 187 * 188 * @return string 189 */ 190 public function escape(string $value): string 191 { 192 return strtr($value, ['@' => '@@']); 193 } 194 195 /** 196 * Create a label for this element. 197 * 198 * @return string 199 */ 200 public function label(): string 201 { 202 return $this->label; 203 } 204 205 /** 206 * Create a label/value pair for this element. 207 * 208 * @param string $value 209 * @param Tree $tree 210 * 211 * @return string 212 */ 213 public function labelValue(string $value, Tree $tree): string 214 { 215 $label = '<span class="label">' . $this->label() . '</span>'; 216 $value = '<span class="value">' . $this->value($value, $tree) . '</span>'; 217 $html = I18N::translate(/* I18N: e.g. "Occupation: farmer" */ '%1$s: %2$s', $label, $value); 218 219 return '<div>' . $html . '</div>'; 220 } 221 222 /** 223 * @return array<string,string> 224 */ 225 public function subtags(): array 226 { 227 return $this->subtags; 228 } 229 230 /** 231 * Display the value of this type of element. 232 * 233 * @param string $value 234 * @param Tree $tree 235 * 236 * @return string 237 */ 238 public function value(string $value, Tree $tree): string 239 { 240 $values = $this->values(); 241 242 if ($values === []) { 243 if (str_contains($value, "\n")) { 244 return '<span dir="auto" class="d-inline-block" style="white-space: pre-wrap;">' . e($value) . '</span>'; 245 } 246 247 return '<span dir="auto">' . e($value) . '</span>'; 248 } 249 250 $canonical = $this->canonical($value); 251 252 return $values[$canonical] ?? '<span dir="auto">' . e($value) . '</span>'; 253 } 254 255 /** 256 * A list of controlled values for this element 257 * 258 * @return array<int|string,string> 259 */ 260 public function values(): array 261 { 262 return []; 263 } 264 265 /** 266 * Display the value of this type of element - convert URLs to links 267 * 268 * @param string $value 269 * 270 * @return string 271 */ 272 protected function valueAutoLink(string $value): string 273 { 274 // Convert URLs into markdown auto-links. 275 $value = preg_replace(self::REGEX_URL, '<$0>', $value); 276 277 // Create a minimal commonmark processor - just add support for autolinks. 278 $environment = new Environment(); 279 $environment 280 ->addBlockRenderer(Document::class, new DocumentRenderer()) 281 ->addBlockRenderer(Paragraph::class, new ParagraphRenderer()) 282 ->addInlineRenderer(Text::class, new TextRenderer()) 283 ->addInlineRenderer(Link::class, new LinkRenderer()) 284 ->addInlineParser(new AutolinkParser()); 285 286 $converter = new CommonMarkConverter(['html_input' => Environment::HTML_INPUT_ESCAPE], $environment); 287 288 return $converter->convertToHtml($value); 289 } 290 291 /** 292 * Display the value of this type of element. 293 * 294 * @param string $value 295 * 296 * @return string 297 */ 298 public function valueNumeric(string $value): string 299 { 300 $canonical = $this->canonical($value); 301 302 if (is_numeric($canonical)) { 303 return I18N::number((int) $canonical); 304 } 305 306 return e($value); 307 } 308} 309