xref: /webtrees/resources/views/edit/edit-gedcom-fields.phtml (revision 45ebc6c6221f90468111ed2015b8449dca331d93)
1<?php
2
3declare(strict_types=1);
4
5use Fisharebest\Webtrees\Elements\EmptyElement;
6use Fisharebest\Webtrees\Registry;
7use Fisharebest\Webtrees\Tree;
8
9/**
10 * @var string        $gedcom
11 * @var array<string> $hierarchy
12 * @var string        $prefix
13 * @var Tree          $tree
14 */
15
16preg_match_all('/^(\d+) (\w+) ?(.*)/m', $gedcom, $matches);
17[, $levels, $tags, $values] = $matches;
18$levels   = array_map(static fn (string $x): int => (int) $x, $levels);
19$keys     = array_keys($levels);
20$elements = [];
21$ids      = [];
22$indent   = [];
23$collapse = [];
24
25foreach ($keys as $num => $key) {
26    $hierarchy[$levels[$key]] = $tags[$key];
27    $full_tag                 = implode(':', array_slice($hierarchy, 0, 1 + $levels[$key]));
28    $elements[$key]           = Registry::elementFactory()->make($full_tag);
29    $ids[$key]                = Registry::idFactory()->id() . '-' . $full_tag;
30
31    // Does this element have any children?
32    $has_subtags = ($levels[$key + 1] ?? 0) > $levels[$key];
33
34    // Do these children have values?
35    $has_subtags_with_values = false;
36    for ($n = $key + 1; $n < count($keys) && $levels[$n] > $levels[$key]; ++$n) {
37        if ($values[$n] !== '') {
38            $has_subtags_with_values = true;
39            break;
40        }
41    }
42
43    $indent[$key] = $elements[$key]->collapseChildren() && $has_subtags;
44
45    $collapse[$key] = $num > 0 && $indent[$key] && !$has_subtags_with_values;
46}
47
48?>
49<?php foreach ($keys as $key) : ?>
50    <?php if ($indent[$key - 1] ?? false) : ?>
51        <?php if ($collapse[$key - 1] ?? false) : ?>
52            <div id="collapse-<?= $key ?>" class="collapse wt-nested-edit-fields">
53        <?php else : ?>
54                <div class="wt-nested-edit-fields">
55        <?php endif ?>
56    <?php endif ?>
57
58    <?php if ($elements[$key] instanceof EmptyElement && $values[$key] === '') : ?>
59        <input type="hidden" name="<?= e($prefix) ?>levels[]" value="<?= $levels[$key] ?>">
60        <input type="hidden" name="<?= e($prefix) ?>tags[]" value="<?= e($tags[$key]) ?>">
61        <input type="hidden" name="<?= e($prefix) ?>values[]" value="">
62    <?php else : ?>
63        <div class="row mb-3">
64            <label class="col-sm-3 col-form-label" for="<?= e($ids[$key]) ?>">
65                <?= $elements[$key]->label() ?>
66                <?php if ($collapse[$key]) : ?>
67                    <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">
68                        <?= view('icons/expand') ?>
69                        <?= view('icons/collapse') ?>
70                    </button>
71                <?php endif ?>
72            </label>
73
74            <div class="col-sm">
75                <input type="hidden" name="<?= e($prefix) ?>levels[]" value="<?= $levels[$key] ?>">
76                <input type="hidden" name="<?= e($prefix) ?>tags[]" value="<?= e($tags[$key]) ?>">
77                <?= $elements[$key]->edit($ids[$key], $prefix . 'values[]', strtr($values[$key], ["\r" => "\n"]), $tree) ?>
78            </div>
79        </div>
80    <?php endif ?>
81
82    <?php for ($n = $key; $levels[$n] >= ($levels[$key + 1] ?? $levels[0]); $n--) : ?>
83        <?php if ($indent[$n]) : ?>
84            <?php $indent[$n] = false ?>
85            </div>
86        <?php endif ?>
87        <?php if ($levels[$n] === ($levels[$key + 1] ?? $levels[0])) : ?>
88            <?php break; ?>
89        <?php endif ?>
90    <?php endfor ?>
91<?php endforeach ?>
92