xref: /webtrees/resources/views/edit/edit-gedcom-fields.phtml (revision fca408422158df832c5a18485ea01e348aebd21a)
1c2ed51d1SGreg Roach<?php
2c2ed51d1SGreg Roach
310e06497SGreg Roachdeclare(strict_types=1);
410e06497SGreg Roach
501df94bfSGreg Roachuse Fisharebest\Webtrees\Elements\EmptyElement;
6c2ed51d1SGreg Roachuse Fisharebest\Webtrees\Registry;
7c2ed51d1SGreg Roachuse Fisharebest\Webtrees\Tree;
8c2ed51d1SGreg Roach
9c2ed51d1SGreg Roach/**
10c2ed51d1SGreg Roach * @var string        $gedcom
11c2ed51d1SGreg Roach * @var array<string> $hierarchy
12c2ed51d1SGreg Roach * @var string        $prefix
13c2ed51d1SGreg Roach * @var Tree          $tree
14c2ed51d1SGreg Roach */
15c2ed51d1SGreg Roach
1601df94bfSGreg Roachpreg_match_all('/^(\d+) (\w+) ?(.*)/m', $gedcom, $matches);
1701df94bfSGreg Roach[, $levels, $tags, $values] = $matches;
1801df94bfSGreg Roach$levels   = array_map(static fn (string $x): int => (int) $x, $levels);
1901df94bfSGreg Roach$keys     = array_keys($levels);
2001df94bfSGreg Roach$elements = [];
2101df94bfSGreg Roach$ids      = [];
22aa936b04SGreg Roach$indent   = [];
234e09581bSGreg Roach$collapse = [];
24c2ed51d1SGreg Roach
25b315f3e1SGreg Roachforeach ($keys as $num => $key) {
2601df94bfSGreg Roach    $hierarchy[$levels[$key]] = $tags[$key];
27*fca40842SGreg Roach    $elements[$key]           = Registry::elementFactory()->make(implode(':', array_slice($hierarchy, 0, 1 + $levels[$key])));
28*fca40842SGreg Roach    $ids[$key]                = Registry::idFactory()->id() . '-' . implode('-', array_slice($hierarchy, 0, 1 + $levels[$key]));
294e09581bSGreg Roach
304e09581bSGreg Roach    // Does this element have any children?
314e09581bSGreg Roach    $has_subtags = ($levels[$key + 1] ?? 0) > $levels[$key];
324e09581bSGreg Roach
334e09581bSGreg Roach    // Do these children have values?
344e09581bSGreg Roach    $has_subtags_with_values = false;
354e09581bSGreg Roach    for ($n = $key + 1; $n < count($keys) && $levels[$n] > $levels[$key]; ++$n) {
364e09581bSGreg Roach        if ($values[$n] !== '') {
374e09581bSGreg Roach            $has_subtags_with_values = true;
384e09581bSGreg Roach            break;
394e09581bSGreg Roach        }
404e09581bSGreg Roach    }
414e09581bSGreg Roach
42aa936b04SGreg Roach    $indent[$key] = $elements[$key]->collapseChildren() && $has_subtags;
43aa936b04SGreg Roach
44b315f3e1SGreg Roach    $collapse[$key] = $num > 0 && $indent[$key] && !$has_subtags_with_values;
4501df94bfSGreg Roach}
46c2ed51d1SGreg Roach
4701df94bfSGreg Roach?>
4801df94bfSGreg Roach<?php foreach ($keys as $key) : ?>
49aa936b04SGreg Roach    <?php if ($indent[$key - 1] ?? false) : ?>
504e09581bSGreg Roach        <?php if ($collapse[$key - 1] ?? false) : ?>
51*fca40842SGreg Roach            <div id="collapse-<?= $ids[$key] ?>" class="collapse wt-nested-edit-fields">
52aa936b04SGreg Roach        <?php else : ?>
53561ec906SGreg Roach                <div class="wt-nested-edit-fields">
54aa936b04SGreg Roach        <?php endif ?>
552ebcf907SGreg Roach    <?php endif ?>
562ebcf907SGreg Roach
5701df94bfSGreg Roach    <?php if ($elements[$key] instanceof EmptyElement && $values[$key] === '') : ?>
5801df94bfSGreg Roach        <input type="hidden" name="<?= e($prefix) ?>levels[]" value="<?= $levels[$key] ?>">
5901df94bfSGreg Roach        <input type="hidden" name="<?= e($prefix) ?>tags[]" value="<?= e($tags[$key]) ?>">
6001df94bfSGreg Roach        <input type="hidden" name="<?= e($prefix) ?>values[]" value="">
6101df94bfSGreg Roach    <?php else : ?>
629e3c2cf9SGreg Roach        <div class="row mb-3">
6301df94bfSGreg Roach            <label class="col-sm-3 col-form-label" for="<?= e($ids[$key]) ?>">
6401df94bfSGreg Roach                <?= $elements[$key]->label() ?>
654e09581bSGreg Roach                <?php if ($collapse[$key]) : ?>
66*fca40842SGreg Roach                    <button class="btn btn-link" data-bs-target="#collapse-<?= $ids[$key + 1] ?>" data-bs-toggle="collapse" aria-expanded="false" aria-controls="collapse-<?= $ids[$key + 1] ?>" type="button">
674e09581bSGreg Roach                        <?= view('icons/expand') ?>
684e09581bSGreg Roach                        <?= view('icons/collapse') ?>
694e09581bSGreg Roach                    </button>
704e09581bSGreg Roach                <?php endif ?>
71c2ed51d1SGreg Roach            </label>
72c2ed51d1SGreg Roach
734e09581bSGreg Roach            <div class="col-sm">
7401df94bfSGreg Roach                <input type="hidden" name="<?= e($prefix) ?>levels[]" value="<?= $levels[$key] ?>">
7501df94bfSGreg Roach                <input type="hidden" name="<?= e($prefix) ?>tags[]" value="<?= e($tags[$key]) ?>">
7601df94bfSGreg Roach                <?= $elements[$key]->edit($ids[$key], $prefix . 'values[]', strtr($values[$key], ["\r" => "\n"]), $tree) ?>
77c2ed51d1SGreg Roach            </div>
78c2ed51d1SGreg Roach        </div>
7901df94bfSGreg Roach    <?php endif ?>
804e09581bSGreg Roach
81a08c49f7SGreg Roach    <?php for ($n = $key; $levels[$n] >= ($levels[$key + 1] ?? $levels[0]); $n--) : ?>
82aa936b04SGreg Roach        <?php if ($indent[$n]) : ?>
839cfc37d2SGreg Roach            <?php $indent[$n] = false ?>
844e09581bSGreg Roach            </div>
854e09581bSGreg Roach        <?php endif ?>
86a08c49f7SGreg Roach        <?php if ($levels[$n] === ($levels[$key + 1] ?? $levels[0])) : ?>
874e09581bSGreg Roach            <?php break; ?>
884e09581bSGreg Roach        <?php endif ?>
894e09581bSGreg Roach    <?php endfor ?>
90c2ed51d1SGreg Roach<?php endforeach ?>
91