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