xref: /webtrees/resources/views/fact-gedcom-fields.phtml (revision e8eafd07fe8e6b7ac971d4a996fad0c99990a4db)
1b315f3e1SGreg Roach<?php
2b315f3e1SGreg Roach
310e06497SGreg Roachdeclare(strict_types=1);
410e06497SGreg Roach
54c01bcf4SGreg Roachuse Fisharebest\Webtrees\Elements\AbstractXrefElement;
6b315f3e1SGreg Roachuse Fisharebest\Webtrees\Elements\XrefSource;
7b315f3e1SGreg Roachuse Fisharebest\Webtrees\Gedcom;
84c01bcf4SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
9b315f3e1SGreg Roachuse Fisharebest\Webtrees\I18N;
10b315f3e1SGreg Roachuse Fisharebest\Webtrees\Registry;
11b315f3e1SGreg Roachuse Fisharebest\Webtrees\Source;
12b315f3e1SGreg Roachuse Fisharebest\Webtrees\Tree;
13b315f3e1SGreg Roach
14b315f3e1SGreg Roach/**
15b315f3e1SGreg Roach * @var string $gedcom
16b315f3e1SGreg Roach * @var string $parent e.g. "INDI:BIRT:DATE"
17b315f3e1SGreg Roach * @var Tree   $tree
18b315f3e1SGreg Roach */
19b315f3e1SGreg Roach
20b315f3e1SGreg Roach$hierarchy = explode(':', $parent);
21b315f3e1SGreg Roach
22b315f3e1SGreg Roach// Merge CONT records onto their parent line.
23b315f3e1SGreg Roach$gedcom = preg_replace('/\n\d CONT ?/', "\r", $gedcom);
24b315f3e1SGreg Roach
25b315f3e1SGreg Roachpreg_match_all('/^(\d+) (\w+) ?(.*)/m', $gedcom, $matches);
26b315f3e1SGreg Roach[, $levels, $tags, $values] = $matches;
27b315f3e1SGreg Roach$levels   = array_map(static fn (string $x): int => (int) $x, $levels);
28b315f3e1SGreg Roach$keys     = array_keys($levels);
29b315f3e1SGreg Roach$elements = [];
30b315f3e1SGreg Roach
314c01bcf4SGreg Roach$private_level = PHP_INT_MAX;
324c01bcf4SGreg Roach
33b315f3e1SGreg Roachforeach ($keys as $key) {
34b315f3e1SGreg Roach    $hierarchy[$levels[$key]] = $tags[$key];
35b315f3e1SGreg Roach    $full_tag                 = implode(':', array_slice($hierarchy, 0, 1 + $levels[$key]));
36b315f3e1SGreg Roach    $elements[$key]           = Registry::elementFactory()->make($full_tag);
374c01bcf4SGreg Roach
384c01bcf4SGreg Roach    if ($elements[$key] instanceof AbstractXrefElement && preg_match('/^@(' . Gedcom::REGEX_XREF . ')@$/', $values[$key], $match) === 1) {
394c01bcf4SGreg Roach        $record = Registry::gedcomRecordFactory()->make($match[1], $tree);
404c01bcf4SGreg Roach
414c01bcf4SGreg Roach        if ($record instanceof GedcomRecord && !$record->canShow()) {
429df321e0SGreg Roach            $private_level = min($private_level, $levels[$key]);
434c01bcf4SGreg Roach        }
44b315f3e1SGreg Roach    }
45b315f3e1SGreg Roach
464c01bcf4SGreg Roach    if ($levels[$key] >= $private_level) {
474c01bcf4SGreg Roach        $values[$key] = '';
484c01bcf4SGreg Roach    } else {
494c01bcf4SGreg Roach        $private_level = PHP_INT_MAX;
504c01bcf4SGreg Roach    }
514c01bcf4SGreg Roach}
52b315f3e1SGreg Roach
53b315f3e1SGreg Roach?>
54b315f3e1SGreg Roach
55b315f3e1SGreg Roach<?php if ($elements[0] instanceof XrefSource && preg_match('/^@(' . Gedcom::REGEX_XREF . ')@$/', $values[0], $match) === 1) : ?>
56b315f3e1SGreg Roach    <?php $source = Registry::sourceFactory()->make($match[1], $tree) ?>
57b315f3e1SGreg Roach    <?php if ($source instanceof Source) : ?>
582e464181SGreg Roach        <?php $id = Registry::idFactory()->id() ?>
59b315f3e1SGreg Roach        <?php $expanded = $tree->getPreference('EXPAND_SOURCES') === '1' ?>
6061486981SGreg Roach        <div>
61*e8eafd07SGreg Roach            <?php if (isset($elements[1])) : ?>
62ce30781dSGreg Roach                <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' ?>">
63b315f3e1SGreg Roach                        <?= view('icons/expand') ?>
64b315f3e1SGreg Roach                        <?= view('icons/collapse') ?>
65ce30781dSGreg Roach                </button>
66*e8eafd07SGreg Roach            <?php else : ?>
67*e8eafd07SGreg Roach                <span class="btn btn-text p-0">
68*e8eafd07SGreg Roach                    <?= view('icons/spacer') ?>
69*e8eafd07SGreg Roach                </span>
70*e8eafd07SGreg Roach            <?php endif ?>
71b315f3e1SGreg Roach
7206c87791SGreg Roach            <?php $label = '<span class="label">' . I18N::translate('Source') . '</span>' ?>
7306c87791SGreg Roach            <?php $value = '<span class="field" dir="auto"><a href="' . e($source->url()) . '">' . $source->fullName() . '</a></span>' ?>
7406c87791SGreg Roach            <?= I18N::translate('%1$s: %2$s', $label, $value) ?>
75b315f3e1SGreg Roach        </div>
76b315f3e1SGreg Roach
77b315f3e1SGreg Roach        <div id="<?= e($id) ?>" class="ps-4 collapse <?= $expanded ? 'show' : '' ?>">
78b315f3e1SGreg Roach            <?php array_shift($keys) ?>
79b315f3e1SGreg Roach            <?php foreach ($keys as $key) : ?>
80b315f3e1SGreg Roach                <?php if ($values[$key] !== '') : ?>
81b315f3e1SGreg Roach                    <?= $elements[$key]->labelValue(strtr($values[$key], ["\r" => "\n"]), $tree) ?>
82b315f3e1SGreg Roach                <?php endif ?>
83b315f3e1SGreg Roach            <?php endforeach ?>
84b315f3e1SGreg Roach        </div>
85b315f3e1SGreg Roach    <?php endif ?>
86b315f3e1SGreg Roach<?php else : ?>
87b315f3e1SGreg Roach    <?php foreach ($keys as $key) : ?>
88b315f3e1SGreg Roach        <?php if ($values[$key] !== '') : ?>
89b315f3e1SGreg Roach            <?= $elements[$key]->labelValue(strtr($values[$key], ["\r" => "\n"]), $tree) ?>
90b315f3e1SGreg Roach        <?php endif ?>
91b315f3e1SGreg Roach    <?php endforeach ?>
92b315f3e1SGreg Roach<?php endif ?>
93