1c2ed51d1SGreg Roach<?php 2c2ed51d1SGreg Roach 3*10e06497SGreg Roachdeclare(strict_types=1); 4*10e06497SGreg 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]; 2701df94bfSGreg Roach $full_tag = implode(':', array_slice($hierarchy, 0, 1 + $levels[$key])); 2801df94bfSGreg Roach $elements[$key] = Registry::elementFactory()->make($full_tag); 292e464181SGreg Roach $ids[$key] = Registry::idFactory()->id() . '-' . $full_tag; 304e09581bSGreg Roach 314e09581bSGreg Roach // Does this element have any children? 324e09581bSGreg Roach $has_subtags = ($levels[$key + 1] ?? 0) > $levels[$key]; 334e09581bSGreg Roach 344e09581bSGreg Roach // Do these children have values? 354e09581bSGreg Roach $has_subtags_with_values = false; 364e09581bSGreg Roach for ($n = $key + 1; $n < count($keys) && $levels[$n] > $levels[$key]; ++$n) { 374e09581bSGreg Roach if ($values[$n] !== '') { 384e09581bSGreg Roach $has_subtags_with_values = true; 394e09581bSGreg Roach break; 404e09581bSGreg Roach } 414e09581bSGreg Roach } 424e09581bSGreg Roach 43aa936b04SGreg Roach $indent[$key] = $elements[$key]->collapseChildren() && $has_subtags; 44aa936b04SGreg Roach 45b315f3e1SGreg Roach $collapse[$key] = $num > 0 && $indent[$key] && !$has_subtags_with_values; 4601df94bfSGreg Roach} 47c2ed51d1SGreg Roach 4801df94bfSGreg Roach?> 4901df94bfSGreg Roach<?php foreach ($keys as $key) : ?> 50aa936b04SGreg Roach <?php if ($indent[$key - 1] ?? false) : ?> 514e09581bSGreg Roach <?php if ($collapse[$key - 1] ?? false) : ?> 52561ec906SGreg Roach <div id="collapse-<?= $key ?>" class="collapse wt-nested-edit-fields"> 53aa936b04SGreg Roach <?php else : ?> 54561ec906SGreg Roach <div class="wt-nested-edit-fields"> 55aa936b04SGreg Roach <?php endif ?> 562ebcf907SGreg Roach <?php endif ?> 572ebcf907SGreg Roach 5801df94bfSGreg Roach <?php if ($elements[$key] instanceof EmptyElement && $values[$key] === '') : ?> 5901df94bfSGreg Roach <input type="hidden" name="<?= e($prefix) ?>levels[]" value="<?= $levels[$key] ?>"> 6001df94bfSGreg Roach <input type="hidden" name="<?= e($prefix) ?>tags[]" value="<?= e($tags[$key]) ?>"> 6101df94bfSGreg Roach <input type="hidden" name="<?= e($prefix) ?>values[]" value=""> 6201df94bfSGreg Roach <?php else : ?> 639e3c2cf9SGreg Roach <div class="row mb-3"> 6401df94bfSGreg Roach <label class="col-sm-3 col-form-label" for="<?= e($ids[$key]) ?>"> 6501df94bfSGreg Roach <?= $elements[$key]->label() ?> 664e09581bSGreg Roach <?php if ($collapse[$key]) : ?> 674e09581bSGreg 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"> 684e09581bSGreg Roach <?= view('icons/expand') ?> 694e09581bSGreg Roach <?= view('icons/collapse') ?> 704e09581bSGreg Roach </button> 714e09581bSGreg Roach <?php endif ?> 72c2ed51d1SGreg Roach </label> 73c2ed51d1SGreg Roach 744e09581bSGreg Roach <div class="col-sm"> 7501df94bfSGreg Roach <input type="hidden" name="<?= e($prefix) ?>levels[]" value="<?= $levels[$key] ?>"> 7601df94bfSGreg Roach <input type="hidden" name="<?= e($prefix) ?>tags[]" value="<?= e($tags[$key]) ?>"> 7701df94bfSGreg Roach <?= $elements[$key]->edit($ids[$key], $prefix . 'values[]', strtr($values[$key], ["\r" => "\n"]), $tree) ?> 78c2ed51d1SGreg Roach </div> 79c2ed51d1SGreg Roach </div> 8001df94bfSGreg Roach <?php endif ?> 814e09581bSGreg Roach 82a08c49f7SGreg Roach <?php for ($n = $key; $levels[$n] >= ($levels[$key + 1] ?? $levels[0]); $n--) : ?> 83aa936b04SGreg Roach <?php if ($indent[$n]) : ?> 849cfc37d2SGreg Roach <?php $indent[$n] = false ?> 854e09581bSGreg Roach </div> 864e09581bSGreg Roach <?php endif ?> 87a08c49f7SGreg Roach <?php if ($levels[$n] === ($levels[$key + 1] ?? $levels[0])) : ?> 884e09581bSGreg Roach <?php break; ?> 894e09581bSGreg Roach <?php endif ?> 904e09581bSGreg Roach <?php endfor ?> 91c2ed51d1SGreg Roach<?php endforeach ?> 92