1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
<?php
use Fisharebest\Webtrees\Age;
use Fisharebest\Webtrees\Date;
use Fisharebest\Webtrees\Fact;
use Fisharebest\Webtrees\Family;
use Fisharebest\Webtrees\Gedcom;
use Fisharebest\Webtrees\GedcomRecord;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Individual;
use Fisharebest\Webtrees\Registry;
/**
* @var bool $cal_link
* @var Fact $fact
* @var GedcomRecord $record
* @var bool $time
*/
$element_factory = Registry::elementFactory();
$factrec = $fact->gedcom();
$html = '';
// Recorded age
if (preg_match('/\n2 AGE (.+)/', $factrec, $match)) {
$fact_age = $element_factory->make($fact->tag() . ':AGE')->value($match[1], $record->tree());
} else {
$fact_age = '';
}
if (preg_match('/\n2 HUSB\n3 AGE (.+)/', $factrec, $match)) {
$husb_age = $element_factory->make($fact->tag() . ':HUSB:AGE')->value($match[1], $record->tree());
} else {
$husb_age = '';
}
if (preg_match('/\n2 WIFE\n3 AGE (.+)/', $factrec, $match)) {
$wife_age = $element_factory->make($fact->tag() . ':WIFE:AGE')->value($match[1], $record->tree());
} else {
$wife_age = '';
}
// Calculated age
[, $tag] = explode(':', $fact->tag());
if (preg_match('/\n2 DATE (.+)/', $factrec, $match)) {
$date = new Date($match[1]);
$html .= ' ' . $date->display($cal_link ? $record->tree() : null, null, true);
// Time isn't valid GEDCOM, but it is widely used.
if ($time && preg_match('/\n3 TIME (.+)/', $factrec, $match)) {
$html .= ' – <span class="date">' . $match[1] . '</span>';
}
if ($record instanceof Individual) {
if (
in_array($tag, Gedcom::BIRTH_EVENTS, true) &&
$record === $fact->record() &&
$record->tree()->getPreference('SHOW_PARENTS_AGE') === '1'
) {
// age of parents at child birth
$html .= view('fact-parents-age', ['individual' => $record, 'birth_date' => $date]);
}
if ($tag !== 'BIRT' && $tag !== 'CHAN' && $tag !== '_TODO') {
// age at event
$birth_date = $record->getBirthDate();
// Can't use getDeathDate(), as this also gives BURI/CREM events, which
// wouldn't give the correct "days after death" result for people with
// no DEAT.
$death_event = $record->facts(['DEAT'])->first();
if ($death_event instanceof Fact) {
$death_date = $death_event->date();
} else {
$death_date = new Date('');
}
$ageText = '';
if ($tag === 'DEAT' || Date::compare($date, $death_date) <= 0 || !$record->isDead()) {
// Before death, print age
$age = (string) new Age($birth_date, $date);
// Only show calculated age if it differs from recorded age
if ($age !== '') {
if (
$fact_age !== '' && !str_starts_with($fact_age, $age) ||
$fact_age === '' && $husb_age === '' && $wife_age === '' ||
$husb_age !== '' && !str_starts_with($husb_age, $age) && $record->sex() === 'M' ||
$wife_age !== '' && !str_starts_with($wife_age, $age) && $record->sex() === 'F'
) {
switch ($record->sex()) {
case 'M':
/* I18N: The age of an individual at a given date */
$ageText = I18N::translateContext('Male', '(aged %s)', $age);
break;
case 'F':
/* I18N: The age of an individual at a given date */
$ageText = I18N::translateContext('Female', '(aged %s)', $age);
break;
default:
/* I18N: The age of an individual at a given date */
$ageText = I18N::translate('(aged %s)', $age);
break;
}
}
}
}
if ($tag !== 'DEAT' && $death_date->isOK() && Date::compare($death_date, $date) <= 0) {
$death_day = $death_date->minimumDate()->day();
$event_day = $date->minimumDate()->day();
if ($death_day !== 0 && $event_day !== 0 && Date::compare($death_date, $date) === 0) {
// On the exact date of death?
// NOTE: this path is never reached. Keep the code (translation) in case
// we decide to re-introduce it.
$ageText = I18N::translate('(on the date of death)');
} else {
// After death
$age = (string) new Age($death_date, $date);
$ageText = I18N::translate('(%s after death)', $age);
}
// Family events which occur after death are probably errors
if ($fact->record() instanceof Family) {
$ageText .= view('icons/warning');
}
}
if ($ageText !== '') {
$html .= ' <span class="age">' . $ageText . '</span>';
}
}
}
}
// print gedcom ages
$age_labels = [
I18N::translate('Age') => $fact_age,
I18N::translate('Husband') => $husb_age,
I18N::translate('Wife') => $wife_age,
];
foreach (array_filter($age_labels) as $label => $age) {
$html .= ' <span class="label">' . $label . ':</span> <span class="age">' . $age . '</span>';
}
?>
<div class="wt-fact-date-age">
<?= $html ?>
</div>
|