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