xref: /webtrees/resources/views/edit/edit-gedcom-fields.phtml (revision 2e464181f49de4b07e9c43f180e054c9d1ee7e4a)
1c2ed51d1SGreg Roach<?php
2c2ed51d1SGreg Roach
301df94bfSGreg Roachuse Fisharebest\Webtrees\Elements\EmptyElement;
4c2ed51d1SGreg Roachuse Fisharebest\Webtrees\Registry;
5c2ed51d1SGreg Roachuse Fisharebest\Webtrees\Tree;
6c2ed51d1SGreg Roach
7c2ed51d1SGreg Roach/**
8c2ed51d1SGreg Roach * @var string        $gedcom
9c2ed51d1SGreg Roach * @var array<string> $hierarchy
10c2ed51d1SGreg Roach * @var string        $prefix
11c2ed51d1SGreg Roach * @var Tree          $tree
12c2ed51d1SGreg Roach */
13c2ed51d1SGreg Roach
1401df94bfSGreg Roachpreg_match_all('/^(\d+) (\w+) ?(.*)/m', $gedcom, $matches);
1501df94bfSGreg Roach[, $levels, $tags, $values] = $matches;
1601df94bfSGreg Roach$levels   = array_map(static fn (string $x): int => (int) $x, $levels);
1701df94bfSGreg Roach$keys     = array_keys($levels);
1801df94bfSGreg Roach$elements = [];
1901df94bfSGreg Roach$ids      = [];
20aa936b04SGreg Roach$indent   = [];
214e09581bSGreg Roach$collapse = [];
22c2ed51d1SGreg Roach
23b315f3e1SGreg Roachforeach ($keys as $num => $key) {
2401df94bfSGreg Roach    $hierarchy[$levels[$key]] = $tags[$key];
2501df94bfSGreg Roach    $full_tag                 = implode(':', array_slice($hierarchy, 0, 1 + $levels[$key]));
2601df94bfSGreg Roach    $elements[$key]           = Registry::elementFactory()->make($full_tag);
27*2e464181SGreg Roach    $ids[$key]                = Registry::idFactory()->id() . '-' . $full_tag;
284e09581bSGreg Roach
294e09581bSGreg Roach    // Does this element have any children?
304e09581bSGreg Roach    $has_subtags = ($levels[$key + 1] ?? 0) > $levels[$key];
314e09581bSGreg Roach
324e09581bSGreg Roach    // Do these children have values?
334e09581bSGreg Roach    $has_subtags_with_values = false;
344e09581bSGreg Roach    for ($n = $key + 1; $n < count($keys) && $levels[$n] > $levels[$key]; ++$n) {
354e09581bSGreg Roach        if ($values[$n] !== '') {
364e09581bSGreg Roach            $has_subtags_with_values = true;
374e09581bSGreg Roach            break;
384e09581bSGreg Roach        }
394e09581bSGreg Roach    }
404e09581bSGreg Roach
41aa936b04SGreg Roach    $indent[$key] = $elements[$key]->collapseChildren() && $has_subtags;
42aa936b04SGreg Roach
43b315f3e1SGreg Roach    $collapse[$key] = $num > 0 && $indent[$key] && !$has_subtags_with_values;
4401df94bfSGreg Roach}
45c2ed51d1SGreg Roach
4601df94bfSGreg Roach?>
4701df94bfSGreg Roach<?php foreach ($keys as $key) : ?>
48aa936b04SGreg Roach    <?php if ($indent[$key - 1] ?? false) : ?>
494e09581bSGreg Roach        <?php if ($collapse[$key - 1] ?? false) : ?>
50561ec906SGreg Roach            <div id="collapse-<?= $key ?>" class="collapse wt-nested-edit-fields">
51aa936b04SGreg Roach        <?php else : ?>
52561ec906SGreg Roach                <div class="wt-nested-edit-fields">
53aa936b04SGreg Roach        <?php endif ?>
542ebcf907SGreg Roach    <?php endif ?>
552ebcf907SGreg Roach
5601df94bfSGreg Roach    <?php if ($elements[$key] instanceof EmptyElement && $values[$key] === '') : ?>
5701df94bfSGreg Roach        <input type="hidden" name="<?= e($prefix) ?>levels[]" value="<?= $levels[$key] ?>">
5801df94bfSGreg Roach        <input type="hidden" name="<?= e($prefix) ?>tags[]" value="<?= e($tags[$key]) ?>">
5901df94bfSGreg Roach        <input type="hidden" name="<?= e($prefix) ?>values[]" value="">
6001df94bfSGreg Roach    <?php else : ?>
619e3c2cf9SGreg Roach        <div class="row mb-3">
6201df94bfSGreg Roach            <label class="col-sm-3 col-form-label" for="<?= e($ids[$key]) ?>">
6301df94bfSGreg Roach                <?= $elements[$key]->label() ?>
644e09581bSGreg Roach                <?php if ($collapse[$key]) : ?>
654e09581bSGreg Roach                    <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">
664e09581bSGreg Roach                        <?= view('icons/expand') ?>
674e09581bSGreg Roach                        <?= view('icons/collapse') ?>
684e09581bSGreg Roach                    </button>
694e09581bSGreg Roach                <?php endif ?>
70c2ed51d1SGreg Roach            </label>
71c2ed51d1SGreg Roach
724e09581bSGreg Roach            <div class="col-sm">
7301df94bfSGreg Roach                <input type="hidden" name="<?= e($prefix) ?>levels[]" value="<?= $levels[$key] ?>">
7401df94bfSGreg Roach                <input type="hidden" name="<?= e($prefix) ?>tags[]" value="<?= e($tags[$key]) ?>">
7501df94bfSGreg Roach                <?= $elements[$key]->edit($ids[$key], $prefix . 'values[]', strtr($values[$key], ["\r" => "\n"]), $tree) ?>
76c2ed51d1SGreg Roach            </div>
77c2ed51d1SGreg Roach        </div>
7801df94bfSGreg Roach    <?php endif ?>
794e09581bSGreg Roach
80a08c49f7SGreg Roach    <?php for ($n = $key; $levels[$n] >= ($levels[$key + 1] ?? $levels[0]); $n--) : ?>
81aa936b04SGreg Roach        <?php if ($indent[$n]) : ?>
829cfc37d2SGreg Roach            <?php $indent[$n] = false ?>
834e09581bSGreg Roach            </div>
844e09581bSGreg Roach        <?php endif ?>
85a08c49f7SGreg Roach        <?php if ($levels[$n] === ($levels[$key + 1] ?? $levels[0])) : ?>
864e09581bSGreg Roach            <?php break; ?>
874e09581bSGreg Roach        <?php endif ?>
884e09581bSGreg Roach    <?php endfor ?>
89c2ed51d1SGreg Roach<?php endforeach ?>
90