xref: /webtrees/resources/views/fact-association-structure.phtml (revision d35568b467207589ea9059739da0bf1f7e785a0d)
1b315f3e1SGreg Roach<?php
2b315f3e1SGreg Roach
310e06497SGreg Roachdeclare(strict_types=1);
410e06497SGreg Roach
5b315f3e1SGreg Roachuse Fisharebest\Webtrees\Age;
6b315f3e1SGreg Roachuse Fisharebest\Webtrees\Auth;
7b315f3e1SGreg Roachuse Fisharebest\Webtrees\Fact;
8b315f3e1SGreg Roachuse Fisharebest\Webtrees\Family;
9b315f3e1SGreg Roachuse Fisharebest\Webtrees\Gedcom;
10b315f3e1SGreg Roachuse Fisharebest\Webtrees\I18N;
11b315f3e1SGreg Roachuse Fisharebest\Webtrees\Individual;
12b315f3e1SGreg Roachuse Fisharebest\Webtrees\Module\ModuleChartInterface;
13b315f3e1SGreg Roachuse Fisharebest\Webtrees\Module\ModuleInterface;
14b315f3e1SGreg Roachuse Fisharebest\Webtrees\Module\RelationshipsChartModule;
15b315f3e1SGreg Roachuse Fisharebest\Webtrees\Registry;
16b315f3e1SGreg Roachuse Fisharebest\Webtrees\Services\ModuleService;
17b315f3e1SGreg Roachuse Fisharebest\Webtrees\Services\RelationshipService;
18b315f3e1SGreg Roach
19b315f3e1SGreg Roach/**
20b315f3e1SGreg Roach * @var Fact $fact
21b315f3e1SGreg Roach */
22b315f3e1SGreg Roach
23b315f3e1SGreg Roach$parent = $fact->record();
24b315f3e1SGreg Roach// To whom is this record an associate?
25b315f3e1SGreg Roachif ($parent instanceof Individual) {
26b315f3e1SGreg Roach    // On an individual page, we just show links to the person
27b315f3e1SGreg Roach    $associates = [$parent];
28b315f3e1SGreg Roach} elseif ($parent instanceof Family) {
29b315f3e1SGreg Roach    // On a family page, we show links to both spouses
30b315f3e1SGreg Roach    $associates = $parent->spouses();
31b315f3e1SGreg Roach} else {
32b315f3e1SGreg Roach    // On other pages, it does not make sense to show associates
33b315f3e1SGreg Roach    return '';
34b315f3e1SGreg Roach}
35b315f3e1SGreg Roach
36b315f3e1SGreg Roachpreg_match_all('/\n2 (_?ASSO) @(' . Gedcom::REGEX_XREF . ')@((\n[3-9].*)*)/', $fact->gedcom(), $amatches, PREG_SET_ORDER);
37b315f3e1SGreg Roach
38b315f3e1SGreg Roach$html = '';
39b315f3e1SGreg Roach// For each ASSO record
40b315f3e1SGreg Roachforeach ($amatches as $amatch) {
41b315f3e1SGreg Roach    $person = Registry::individualFactory()->make($amatch[2], $fact->record()->tree());
42bd29d468SGreg Roach    if ($person instanceof Individual && $person->canShowName()) {
43b315f3e1SGreg Roach        // Is there a "RELA" tag
44bd29d468SGreg Roach        if (preg_match('/\n([23]) RELA (.+)/', $amatch[3], $rmatch) === 1) {
45b315f3e1SGreg Roach            if ($rmatch[1] === '2') {
46b315f3e1SGreg Roach                $rela_tag = $fact->record()->tag() . ':' . $amatch[1] . ':RELA';
47b315f3e1SGreg Roach            } else {
48b315f3e1SGreg Roach                $rela_tag = $fact->tag() . ':' . $amatch[1] . ':RELA';
49b315f3e1SGreg Roach            }
50b315f3e1SGreg Roach            // Use the supplied relationship as a label
51b315f3e1SGreg Roach            $label = Registry::elementFactory()->make($rela_tag)->value($rmatch[2], $parent->tree());
52bd29d468SGreg Roach        } elseif (preg_match('/^1 _?ASSO/', $fact->gedcom()) === 1) {
53b315f3e1SGreg Roach            // Use a default label
54b315f3e1SGreg Roach            $label = Registry::elementFactory()->make($fact->tag())->label();
55b315f3e1SGreg Roach        } else {
56b315f3e1SGreg Roach            // Use a default label
57b315f3e1SGreg Roach            $label = Registry::elementFactory()->make($fact->tag() . ':_ASSO')->label();
58b315f3e1SGreg Roach        }
59b315f3e1SGreg Roach
60f6ffd5b7SGreg Roach        if ($person !== $parent && $person->getBirthDate()->isOK() && $fact->date()->isOK()) {
61b315f3e1SGreg Roach            $age = new Age($person->getBirthDate(), $fact->date());
62b315f3e1SGreg Roach            switch ($person->sex()) {
63b315f3e1SGreg Roach                case 'M':
64b315f3e1SGreg Roach                    $age_text = ' ' . I18N::translateContext('Male', '(aged %s)', (string) $age);
65b315f3e1SGreg Roach                    break;
66b315f3e1SGreg Roach                case 'F':
67b315f3e1SGreg Roach                    $age_text = ' ' . I18N::translateContext('Female', '(aged %s)', (string) $age);
68b315f3e1SGreg Roach                    break;
69b315f3e1SGreg Roach                default:
70b315f3e1SGreg Roach                    $age_text = ' ' . I18N::translate('(aged %s)', (string) $age);
71b315f3e1SGreg Roach                    break;
72b315f3e1SGreg Roach            }
73b315f3e1SGreg Roach        } else {
74b315f3e1SGreg Roach            $age_text = '';
75b315f3e1SGreg Roach        }
76b315f3e1SGreg Roach
77b315f3e1SGreg Roach        $values = ['<a href="' . e($person->url()) . '">' . $person->fullName() . '</a>' . $age_text];
78b315f3e1SGreg Roach
79*d35568b4SGreg Roach        $module = Registry::container()->get(ModuleService::class)
80*d35568b4SGreg Roach            ->findByComponent(ModuleChartInterface::class, $person->tree(), Auth::user())
81*d35568b4SGreg Roach            ->first(static function (ModuleInterface $module) {
82b315f3e1SGreg Roach                return $module instanceof RelationshipsChartModule;
83b315f3e1SGreg Roach            });
84b315f3e1SGreg Roach
85b315f3e1SGreg Roach        if ($module instanceof RelationshipsChartModule) {
86b315f3e1SGreg Roach            foreach ($associates as $associate) {
87*d35568b4SGreg Roach                $relationship_name = Registry::container()->get(RelationshipService::class)->getCloseRelationshipName($associate, $person);
88b315f3e1SGreg Roach                if ($relationship_name === '') {
89b315f3e1SGreg Roach                    $relationship_name = I18N::translate('Relationship');
90b315f3e1SGreg Roach                }
91b315f3e1SGreg Roach
92b315f3e1SGreg Roach                if ($parent instanceof Family) {
93b315f3e1SGreg Roach                    // For family ASSO records (e.g. MARR), identify the spouse with a sex icon
94b315f3e1SGreg Roach                    $relationship_name .= '<small>' . view('icons/sex', ['sex' => $associate->sex()]) . '</small>';
95b315f3e1SGreg Roach                }
96b315f3e1SGreg Roach
97b315f3e1SGreg Roach                $values[] = '<a href="' . $module->chartUrl($associate, ['xref2' => $person->xref()]) . '" rel="nofollow">' . $relationship_name . '</a>';
98b315f3e1SGreg Roach            }
99b315f3e1SGreg Roach        }
10006c87791SGreg Roach        $label = '<span class="label">' . $label . '</span>';
10106c87791SGreg Roach        $value = '<span class="field" dir="auto">' . implode(' — ', $values) . '</span>';
10206c87791SGreg Roach
10306c87791SGreg Roach        $asso  = I18N::translate('%1$s: %2$s', $label, $value);
104701f5d18SGreg Roach    } elseif ($amatch[2] === 'VOID') {
10506c87791SGreg Roach        $label = '<span class="label">' . I18N::translate('Associate') . '</span>';
106701f5d18SGreg Roach        $value = I18N::translate('Not recorded');
107701f5d18SGreg Roach        $asso  = I18N::translate('%1$s: %2$s', $label, $value);
108701f5d18SGreg Roach    } elseif (Auth::isEditor($fact->record()->tree())) {
109701f5d18SGreg Roach        $label = '<span class="label">' . I18N::translate('Associate') . '</span>';
110701f5d18SGreg Roach        $value = '<span class="error">@' . e($amatch[2]) . '@</span>';
11164654c87SGreg Roach        $asso  = I18N::translate('%1$s: %2$s', $label, $value);
112b315f3e1SGreg Roach    } else {
113b315f3e1SGreg Roach        $asso = '';
114b315f3e1SGreg Roach    }
115b315f3e1SGreg Roach    $html .= '<div class="fact_ASSO">' . $asso . '</div>';
116b315f3e1SGreg Roach}
117b315f3e1SGreg Roach
118b315f3e1SGreg Roachecho $html;
119