xref: /webtrees/resources/views/fact-gedcom-fields.phtml (revision 1da5dadd02c9f21e0a345e21a19b2c63ae792ed3)
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            <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' ?>">
62                <?= view('icons/expand') ?>
63                <?= view('icons/collapse') ?>
64             </button>
65
66            <?php $label = '<span class="label">' . I18N::translate('Source') . '</span>' ?>
67            <?php $value = '<span class="field" dir="auto"><a href="' . e($source->url()) . '">' . $source->fullName() . '</a></span>' ?>
68            <?= I18N::translate('%1$s: %2$s', $label, $value) ?>
69        </div>
70
71        <div id="<?= e($id) ?>" class="ps-4 collapse <?= $expanded ? 'show' : '' ?>">
72            <?php array_shift($keys) ?>
73            <?php foreach ($keys as $key) : ?>
74                <?php if ($values[$key] !== '') : ?>
75                    <?= $elements[$key]->labelValue(strtr($values[$key], ["\r" => "\n"]), $tree) ?>
76                <?php endif ?>
77            <?php endforeach ?>
78        </div>
79    <?php endif ?>
80<?php else : ?>
81    <?php foreach ($keys as $key) : ?>
82        <?php if ($values[$key] !== '') : ?>
83            <?= $elements[$key]->labelValue(strtr($values[$key], ["\r" => "\n"]), $tree) ?>
84        <?php endif ?>
85    <?php endforeach ?>
86<?php endif ?>
87