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