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