xref: /webtrees/resources/views/edit/edit-gedcom-fields.phtml (revision 43f2f523bcb6d4090564d23802872c0679ede6bc)
1<?php
2
3use Fisharebest\Webtrees\Elements\AbstractXrefElement;
4use Fisharebest\Webtrees\Elements\EmptyElement;
5use Fisharebest\Webtrees\Registry;
6use Fisharebest\Webtrees\Tree;
7use Ramsey\Uuid\Uuid;
8
9/**
10 * @var string        $gedcom
11 * @var array<string> $hierarchy
12 * @var string        $prefix
13 * @var Tree          $tree
14 */
15
16preg_match_all('/^(\d+) (\w+) ?(.*)/m', $gedcom, $matches);
17[, $levels, $tags, $values] = $matches;
18$levels   = array_map(static fn(string $x): int => (int) $x, $levels);
19$keys     = array_keys($levels);
20$elements = [];
21$ids      = [];
22$collapse = [];
23
24foreach ($keys as $key) {
25    $hierarchy[$levels[$key]] = $tags[$key];
26    $full_tag                 = implode(':', array_slice($hierarchy, 0, 1 + $levels[$key]));
27    $elements[$key]           = Registry::elementFactory()->make($full_tag);
28    $ids[$key]                = Uuid::uuid4()->toString() . '-' . $full_tag;
29
30    // Does this element have any children?
31    $has_subtags = ($levels[$key + 1] ?? 0) > $levels[$key];
32
33    // Do these children have values?
34    $has_subtags_with_values = false;
35    for ($n = $key + 1; $n < count($keys) && $levels[$n] > $levels[$key]; ++$n) {
36        if ($values[$n] !== '') {
37            $has_subtags_with_values = true;
38            break;
39        }
40    }
41
42    $collapse[$key] = $elements[$key]->collapseChildren() && $has_subtags && !$has_subtags_with_values;
43}
44
45?>
46<?php foreach ($keys as $key) : ?>
47    <?php if ($collapse[$key - 1] ?? false) : ?>
48        <div id="collapse-<?= $key ?>" class="collapse ms-4 ps-4 border-start border-4">
49    <?php endif ?>
50
51    <?php if ($elements[$key] instanceof EmptyElement && $values[$key] === '') : ?>
52        <input type="hidden" name="<?= e($prefix) ?>levels[]" value="<?= $levels[$key] ?>">
53        <input type="hidden" name="<?= e($prefix) ?>tags[]" value="<?= e($tags[$key]) ?>">
54        <input type="hidden" name="<?= e($prefix) ?>values[]" value="">
55    <?php else : ?>
56        <div class="row form-group mb-3">
57            <label class="col-sm-3 col-form-label" for="<?= e($ids[$key]) ?>">
58                <?= $elements[$key]->label() ?>
59                <?php if ($collapse[$key]) : ?>
60                    <button class="btn btn-link" data-bs-target="#collapse-<?= $key + 1 ?>" data-bs-toggle="collapse" aria-expanded="false" aria-controls="collapse-<?= $key + 1 ?>" type="button">
61                        <?= view('icons/expand') ?>
62                        <?= view('icons/collapse') ?>
63                    </button>
64                <?php endif ?>
65            </label>
66
67            <div class="col-sm">
68                <input type="hidden" name="<?= e($prefix) ?>levels[]" value="<?= $levels[$key] ?>">
69                <input type="hidden" name="<?= e($prefix) ?>tags[]" value="<?= e($tags[$key]) ?>">
70                <?= $elements[$key]->edit($ids[$key], $prefix . 'values[]', strtr($values[$key], ["\r" => "\n"]), $tree) ?>
71            </div>
72        </div>
73    <?php endif ?>
74
75    <?php for ($n = $key; $levels[$n] >= ($levels[$key + 1] ?? PHP_INT_MAX); $n--) : ?>
76        <?php if ( $collapse[$n] ) : ?>
77            </div>
78        <?php endif ?>
79        <?php if ( $levels[$n] === ($levels[$key + 1] ?? PHP_INT_MAX)) : ?>
80            <?php break; ?>
81        <?php endif ?>
82    <?php endfor ?>
83<?php endforeach ?>
84