1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
|
<?php
/**
* webtrees: online genealogy
* Copyright (C) 2022 webtrees development team
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace Fisharebest\Webtrees\Elements;
use Fisharebest\Webtrees\Contracts\ElementInterface;
use Fisharebest\Webtrees\Html;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Registry;
use Fisharebest\Webtrees\Tree;
use function array_key_exists;
use function array_map;
use function e;
use function is_numeric;
use function nl2br;
use function preg_replace;
use function str_contains;
use function str_starts_with;
use function strip_tags;
use function trim;
use function view;
/**
* A GEDCOM element is a tag/primitive in a GEDCOM file.
*/
abstract class AbstractElement implements ElementInterface
{
// HTML attributes for an <input>
protected const MAXIMUM_LENGTH = false;
protected const PATTERN = false;
// Which child elements can appear under this element.
protected const SUBTAGS = [];
// A label to describe this element
private string $label;
/** @var array<string,string> Subtags of this element */
private array $subtags;
/**
* AbstractGedcomElement constructor.
*
* @param string $label
* @param array<string>|null $subtags
*/
public function __construct(string $label, array $subtags = null)
{
$this->label = $label;
$this->subtags = $subtags ?? static::SUBTAGS;
}
/**
* Convert a value to a canonical form.
*
* @param string $value
*
* @return string
*/
public function canonical(string $value): string
{
$value = strtr($value, ["\t" => ' ', "\r" => ' ', "\n" => ' ']);
while (str_contains($value, ' ')) {
$value = strtr($value, [' ' => ' ']);
}
return trim($value);
}
/**
* Convert a multi-line value to a canonical form.
*
* @param string $value
*
* @return string
*/
protected function canonicalText(string $value): string
{
// Browsers use MS-DOS line endings in multi-line data.
$value = strtr($value, ["\t" => ' ', "\r\n" => "\n", "\r" => "\n"]);
// Remove blank lines at start/end
$value = preg_replace('/^( *\n)+/', '', $value);
return preg_replace('/(\n *)+$/', '', $value);
}
/**
* Should we collapse the children of this element when editing?
*
* @return bool
*/
public function collapseChildren(): bool
{
return false;
}
/**
* Create a default value for this element.
*
* @param Tree $tree
*
* @return string
*/
public function default(Tree $tree): string
{
return '';
}
/**
* An edit control for this data.
*
* @param string $id
* @param string $name
* @param string $value
* @param Tree $tree
*
* @return string
*/
public function edit(string $id, string $name, string $value, Tree $tree): string
{
$values = $this->values();
if ($values !== []) {
$value = $this->canonical($value);
// Ensure the current data is in the list.
if (!array_key_exists($value, $values)) {
$values = [$value => $value] + $values;
}
// We may use markup to display values, but not when editing them.
$values = array_map(static fn (string $x): string => strip_tags($x), $values);
return view('components/select', [
'id' => $id,
'name' => $name,
'options' => $values,
'selected' => $value,
]);
}
$attributes = [
'class' => 'form-control',
'dir' => 'auto',
'type' => 'text',
'id' => $id,
'name' => $name,
'value' => $value,
'maxlength' => static::MAXIMUM_LENGTH,
'pattern' => static::PATTERN,
];
return '<input ' . Html::attributes($attributes) . ' />';
}
/**
* An edit control for this data.
*
* @param string $id
* @param string $name
* @param string $value
*
* @return string
*/
public function editHidden(string $id, string $name, string $value): string
{
return '<input class="form-control" type="hidden" id="' . e($id) . '" name="' . e($name) . '" value="' . e($value) . '" />';
}
/**
* An edit control for this data.
*
* @param string $id
* @param string $name
* @param string $value
*
* @return string
*/
public function editTextArea(string $id, string $name, string $value): string
{
return '<textarea class="form-control" id="' . e($id) . '" name="' . e($name) . '" rows="3" dir="auto">' . e($value) . '</textarea>';
}
/**
* Escape @ signs in a GEDCOM export.
*
* @param string $value
*
* @return string
*/
public function escape(string $value): string
{
return strtr($value, ['@' => '@@']);
}
/**
* Create a label for this element.
*
* @return string
*/
public function label(): string
{
return $this->label;
}
/**
* Create a label/value pair for this element.
*
* @param string $value
* @param Tree $tree
*
* @return string
*/
public function labelValue(string $value, Tree $tree): string
{
$label = '<span class="label">' . $this->label() . '</span>';
$value = '<span class="value align-top">' . $this->value($value, $tree) . '</span>';
$html = I18N::translate(/* I18N: e.g. "Occupation: farmer" */ '%1$s: %2$s', $label, $value);
return '<div>' . $html . '</div>';
}
/**
* Set, remove or replace a subtag.
*
* @param string $subtag
* @param string $repeat
* @param string $before
*
* @return void
*/
public function subtag(string $subtag, string $repeat, string $before = ''): void
{
if ($before === '' || ($this->subtags[$before] ?? null) === null) {
$this->subtags[$subtag] = $repeat;
} else {
$tmp = [];
foreach ($this->subtags as $key => $value) {
if ($key === $before) {
$tmp[$subtag] = $repeat;
}
$tmp[$key] = $value;
}
$this->subtags = $tmp;
}
}
/**
* @return array<string,string>
*/
public function subtags(): array
{
return $this->subtags;
}
/**
* Display the value of this type of element.
*
* @param string $value
* @param Tree $tree
*
* @return string
*/
public function value(string $value, Tree $tree): string
{
$values = $this->values();
if ($values === []) {
if (str_contains($value, "\n")) {
return '<span class="ut d-inline-block">' . nl2br(e($value, false)) . '</span>';
}
return '<span class="ut">' . e($value) . '</span>';
}
$canonical = $this->canonical($value);
return $values[$canonical] ?? '<bdi>' . e($value) . '</bdi>';
}
/**
* A list of controlled values for this element
*
* @return array<int|string,string>
*/
public function values(): array
{
return [];
}
/**
* Display the value of this type of element - convert URLs to links.
*
* @param string $value
*
* @return string
*/
protected function valueAutoLink(string $value): string
{
$canonical = $this->canonical($value);
if (str_contains($canonical, 'http://') || str_contains($canonical, 'https://')) {
$html = Registry::markdownFactory()->autolink($canonical);
return strip_tags($html, ['a']);
}
return e($canonical);
}
/**
* Display the value of this type of element - multi-line text with/without markdown.
*
* @param string $value
* @param Tree $tree
*
* @return string
*/
protected function valueFormatted(string $value, Tree $tree): string
{
$canonical = $this->canonical($value);
$format = $tree->getPreference('FORMAT_TEXT');
switch ($format) {
case 'markdown':
return Registry::markdownFactory()->markdown($canonical, $tree);
default:
return Registry::markdownFactory()->autolink($canonical, $tree);
}
}
/**
* Display the value of this type of element - convert to URL.
*
* @param string $value
*
* @return string
*/
protected function valueLink(string $value): string
{
$canonical = $this->canonical($value);
if (str_starts_with($canonical, 'https://') || str_starts_with($canonical, 'http://')) {
return '<a dir="auto" href="' . e($canonical) . '">' . e($value) . '</a>';
}
return e($value);
}
/**
* Display the value of this type of element.
*
* @param string $value
*
* @return string
*/
public function valueNumeric(string $value): string
{
$canonical = $this->canonical($value);
if (is_numeric($canonical)) {
return I18N::number((int) $canonical);
}
return e($value);
}
}
|