xref: /webtrees/resources/views/fact-gedcom-fields.phtml (revision 58feaed63d47b8abced82391ad3679c63abf54d7)
1<?php
2
3declare(strict_types=1);
4
5use Fisharebest\Webtrees\Elements\AbstractXrefElement;
6use Fisharebest\Webtrees\Elements\XrefSource;
7use Fisharebest\Webtrees\Gedcom;
8use Fisharebest\Webtrees\GedcomRecord;
9use Fisharebest\Webtrees\I18N;
10use Fisharebest\Webtrees\Registry;
11use Fisharebest\Webtrees\Source;
12use Fisharebest\Webtrees\Tree;
13
14/**
15 * @var string $gedcom
16 * @var string $parent e.g. "INDI:BIRT:DATE"
17 * @var Tree   $tree
18 */
19
20$hierarchy = explode(':', $parent);
21
22// Merge CONT records onto their parent line.
23$gedcom = preg_replace('/\n\d CONT ?/', "\r", $gedcom);
24
25preg_match_all('/^(\d+) (\w+) ?(.*)/m', $gedcom, $matches);
26[, $levels, $tags, $values] = $matches;
27$levels   = array_map(static fn (string $x): int => (int) $x, $levels);
28$keys     = array_keys($levels);
29$elements = [];
30
31$private_level = PHP_INT_MAX;
32
33foreach ($keys as $key) {
34    $hierarchy[$levels[$key]] = $tags[$key];
35    $full_tag                 = implode(':', array_slice($hierarchy, 0, 1 + $levels[$key]));
36    $elements[$key]           = Registry::elementFactory()->make($full_tag);
37
38    if ($elements[$key] instanceof AbstractXrefElement && preg_match('/^@(' . Gedcom::REGEX_XREF . ')@$/', $values[$key], $match) === 1) {
39        $record = Registry::gedcomRecordFactory()->make($match[1], $tree);
40
41        if ($record instanceof GedcomRecord && !$record->canShow()) {
42            $private_level = min($private_level, $levels[$key]);
43        }
44    }
45
46    if ($levels[$key] >= $private_level) {
47        $values[$key] = '';
48    } else {
49        $private_level = PHP_INT_MAX;
50    }
51}
52
53?>
54
55<?php if ($elements[0] instanceof XrefSource && preg_match('/^@(' . Gedcom::REGEX_XREF . ')@$/', $values[0], $match) === 1) : ?>
56    <?php $source = Registry::sourceFactory()->make($match[1], $tree) ?>
57    <?php if ($source instanceof Source) : ?>
58        <?php $id = Registry::idFactory()->id() ?>
59        <?php $expanded = $tree->getPreference('EXPAND_SOURCES') === '1' ?>
60        <div>
61            <?php if (isset($elements[1])) : ?>
62                <button type="button" class="btn btn-text p-0" href="#<?= e($id) ?>" data-bs-toggle="collapse" aria-controls="<?= e($id) ?>" aria-expanded="<?= $expanded ? 'true' : 'false' ?>">
63                        <?= view('icons/expand') ?>
64                        <?= view('icons/collapse') ?>
65                </button>
66            <?php else : ?>
67                <span class="btn btn-text p-0">
68                    <?= view('icons/spacer') ?>
69                </span>
70            <?php endif ?>
71
72            <?php $label = '<span class="label">' . I18N::translate('Source') . '</span>' ?>
73            <?php $value = '<span class="field" dir="auto"><a href="' . e($source->url()) . '">' . $source->fullName() . '</a></span>' ?>
74            <?= I18N::translate('%1$s: %2$s', $label, $value) ?>
75        </div>
76
77        <div id="<?= e($id) ?>" class="ps-4 collapse <?= $expanded ? 'show' : '' ?>">
78            <?php array_shift($keys) ?>
79            <?php foreach ($keys as $key) : ?>
80                <?php if ($values[$key] !== '') : ?>
81                    <?= $elements[$key]->labelValue(strtr($values[$key], ["\r" => "\n"]), $tree) ?>
82                <?php endif ?>
83            <?php endforeach ?>
84        </div>
85    <?php endif ?>
86<?php else : ?>
87    <?php foreach ($keys as $key) : ?>
88        <?php if ($values[$key] !== '') : ?>
89            <?= $elements[$key]->labelValue(strtr($values[$key], ["\r" => "\n"]), $tree) ?>
90        <?php endif ?>
91    <?php endforeach ?>
92<?php endif ?>
93