xref: /webtrees/resources/views/edit/edit-gedcom-fields.phtml (revision 8634febf6487fcb3ee7b59e88f828071a22cba22)
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    $elements[$key]           = Registry::elementFactory()->make(implode(':', array_slice($hierarchy, 0, 1 + $levels[$key])));
28    $ids[$key]                = Registry::idFactory()->id() . '-' . implode('-', array_slice($hierarchy, 0, 1 + $levels[$key]));
29
30    // Does this element have any children?
31    $has_subtags = ($levels[$key + 1] ?? 0) > $levels[$key];
32
33    // Do these children have values?
34    $has_subtags_with_values = false;
35    for ($n = $key + 1; $n < count($keys) && $levels[$n] > $levels[$key]; ++$n) {
36        if ($values[$n] !== '') {
37            $has_subtags_with_values = true;
38            break;
39        }
40    }
41
42    $indent[$key] = $elements[$key]->collapseChildren() && $has_subtags;
43
44    $collapse[$key] = $num > 0 && $indent[$key] && !$has_subtags_with_values;
45}
46
47?>
48<?php foreach ($keys as $key) : ?>
49    <?php if ($indent[$key - 1] ?? false) : ?>
50        <?php if ($collapse[$key - 1] ?? false) : ?>
51            <div id="collapse-<?= $ids[$key] ?>" class="collapse wt-nested-edit-fields">
52        <?php else : ?>
53                <div class="wt-nested-edit-fields">
54        <?php endif ?>
55    <?php endif ?>
56
57    <?php if ($elements[$key] instanceof EmptyElement && $values[$key] === '') : ?>
58        <input type="hidden" name="<?= e($prefix) ?>levels[]" value="<?= $levels[$key] ?>">
59        <input type="hidden" name="<?= e($prefix) ?>tags[]" value="<?= e($tags[$key]) ?>">
60        <input type="hidden" name="<?= e($prefix) ?>values[]" value="">
61    <?php else : ?>
62        <div class="row mb-3">
63            <label class="col-sm-3 col-form-label" for="<?= e($ids[$key]) ?>">
64                <?= $elements[$key]->label() ?>
65                <?php if ($collapse[$key]) : ?>
66                    <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">
67                        <?= view('icons/expand') ?>
68                        <?= view('icons/collapse') ?>
69                    </button>
70                <?php endif ?>
71            </label>
72
73            <div class="col-sm">
74                <input type="hidden" name="<?= e($prefix) ?>levels[]" value="<?= $levels[$key] ?>">
75                <input type="hidden" name="<?= e($prefix) ?>tags[]" value="<?= e($tags[$key]) ?>">
76                <?= $elements[$key]->edit($ids[$key], $prefix . 'values[]', strtr($values[$key], ["\r" => "\n"]), $tree) ?>
77            </div>
78        </div>
79    <?php endif ?>
80
81    <?php for ($n = $key; $levels[$n] >= ($levels[$key + 1] ?? $levels[0]); $n--) : ?>
82        <?php if ($indent[$n]) : ?>
83            <?php $indent[$n] = false ?>
84            </div>
85        <?php endif ?>
86        <?php if ($levels[$n] === ($levels[$key + 1] ?? $levels[0])) : ?>
87            <?php break; ?>
88        <?php endif ?>
89    <?php endfor ?>
90<?php endforeach ?>
91