xref: /webtrees/resources/views/edit/edit-gedcom-fields.phtml (revision a9866bf217fcbf79695b119a127478d64260caff)
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$collapse = [];
22
23foreach ($keys as $key) {
24    $hierarchy[$levels[$key]] = $tags[$key];
25    $full_tag                 = implode(':', array_slice($hierarchy, 0, 1 + $levels[$key]));
26    $elements[$key]           = Registry::elementFactory()->make($full_tag);
27    $ids[$key]                = Uuid::uuid4()->toString() . '-' . $full_tag;
28
29    // Does this element have any children?
30    $has_subtags = ($levels[$key + 1] ?? 0) > $levels[$key];
31
32    // Do these children have values?
33    $has_subtags_with_values = false;
34    for ($n = $key + 1; $n < count($keys) && $levels[$n] > $levels[$key]; ++$n) {
35        if ($values[$n] !== '') {
36            $has_subtags_with_values = true;
37            break;
38        }
39    }
40
41    $collapse[$key] = $elements[$key]->collapseChildren() && $has_subtags && !$has_subtags_with_values;
42}
43
44?>
45<?php foreach ($keys as $key) : ?>
46    <?php if ($collapse[$key - 1] ?? false) : ?>
47        <div id="collapse-<?= $key ?>" class="collapse ms-4 ps-4 border-start border-4">
48    <?php endif ?>
49
50    <?php if ($elements[$key] instanceof EmptyElement && $values[$key] === '') : ?>
51        <input type="hidden" name="<?= e($prefix) ?>levels[]" value="<?= $levels[$key] ?>">
52        <input type="hidden" name="<?= e($prefix) ?>tags[]" value="<?= e($tags[$key]) ?>">
53        <input type="hidden" name="<?= e($prefix) ?>values[]" value="">
54    <?php else : ?>
55        <div class="row form-group mb-3">
56            <label class="col-sm-3 col-form-label" for="<?= e($ids[$key]) ?>">
57                <?= $elements[$key]->label() ?>
58                <?php if ($collapse[$key]) : ?>
59                    <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">
60                        <?= view('icons/expand') ?>
61                        <?= view('icons/collapse') ?>
62                    </button>
63                <?php endif ?>
64            </label>
65
66            <div class="col-sm">
67                <input type="hidden" name="<?= e($prefix) ?>levels[]" value="<?= $levels[$key] ?>">
68                <input type="hidden" name="<?= e($prefix) ?>tags[]" value="<?= e($tags[$key]) ?>">
69                <?= $elements[$key]->edit($ids[$key], $prefix . 'values[]', strtr($values[$key], ["\r" => "\n"]), $tree) ?>
70            </div>
71        </div>
72    <?php endif ?>
73
74    <?php for ($n = $key; $levels[$n] >= ($levels[$key + 1] ?? PHP_INT_MAX); $n--) : ?>
75        <?php if ( $collapse[$n] ) : ?>
76            </div>
77        <?php endif ?>
78        <?php if ( $levels[$n] === ($levels[$key + 1] ?? PHP_INT_MAX)) : ?>
79            <?php break; ?>
80        <?php endif ?>
81    <?php endfor ?>
82<?php endforeach ?>
83