xref: /webtrees/resources/views/edit/edit-gedcom-fields.phtml (revision a171b6a5f1a6037fc702a697eb572b1932e4c7ef)
1<?php
2
3use Fisharebest\Webtrees\Elements\EmptyElement;
4use Fisharebest\Webtrees\Registry;
5use Fisharebest\Webtrees\Tree;
6use Ramsey\Uuid\Uuid;
7
8/**
9 * @var string        $gedcom
10 * @var array<string> $hierarchy
11 * @var string        $prefix
12 * @var Tree          $tree
13 */
14
15preg_match_all('/^(\d+) (\w+) ?(.*)/m', $gedcom, $matches);
16[, $levels, $tags, $values] = $matches;
17$levels   = array_map(static fn (string $x): int => (int) $x, $levels);
18$keys     = array_keys($levels);
19$elements = [];
20$ids      = [];
21$indent   = [];
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    $indent[$key] = $elements[$key]->collapseChildren() && $has_subtags;
43
44    $collapse[$key] = $indent[$key] && !$has_subtags_with_values;
45}
46
47?>
48<?php foreach ($keys as $key) : ?>
49    <?php if ($indent[$key - 1] ?? false) : ?>
50        <?php if ($collapse[$key - 1] ?? false) : ?>
51            <div id="collapse-<?= $key ?>" class="collapse ms-4 ps-4 border-start border-4">
52        <?php else : ?>
53                <div class="ms-4 ps-4 border-start border-4">
54        <?php endif ?>
55    <?php endif ?>
56
57    <?php if ($elements[$key] instanceof EmptyElement && $values[$key] === '') : ?>
58        <input type="hidden" name="<?= e($prefix) ?>levels[]" value="<?= $levels[$key] ?>">
59        <input type="hidden" name="<?= e($prefix) ?>tags[]" value="<?= e($tags[$key]) ?>">
60        <input type="hidden" name="<?= e($prefix) ?>values[]" value="">
61    <?php else : ?>
62        <div class="row form-group mb-3">
63            <label class="col-sm-3 col-form-label" for="<?= e($ids[$key]) ?>">
64                <?= $elements[$key]->label() ?>
65                <?php if ($collapse[$key]) : ?>
66                    <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">
67                        <?= view('icons/expand') ?>
68                        <?= view('icons/collapse') ?>
69                    </button>
70                <?php endif ?>
71            </label>
72
73            <div class="col-sm">
74                <input type="hidden" name="<?= e($prefix) ?>levels[]" value="<?= $levels[$key] ?>">
75                <input type="hidden" name="<?= e($prefix) ?>tags[]" value="<?= e($tags[$key]) ?>">
76                <?= $elements[$key]->edit($ids[$key], $prefix . 'values[]', strtr($values[$key], ["\r" => "\n"]), $tree) ?>
77            </div>
78        </div>
79    <?php endif ?>
80
81    <?php for ($n = $key; $levels[$n] >= ($levels[$key + 1] ?? PHP_INT_MAX); $n--) : ?>
82        <?php if ( $indent[$n] ) : ?>
83            <?php $indent[$n] = false ?>
84            </div>
85        <?php endif ?>
86        <?php if ( $levels[$n] === ($levels[$key + 1] ?? PHP_INT_MAX)) : ?>
87            <?php break; ?>
88        <?php endif ?>
89    <?php endfor ?>
90<?php endforeach ?>
91