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