1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* webtrees-lib: MyArtJaub library for webtrees |
5
|
|
|
* |
6
|
|
|
* @package MyArtJaub\Webtrees |
7
|
|
|
* @subpackage PatronymicLineage |
8
|
|
|
* @author Jonathan Jaubart <[email protected]> |
9
|
|
|
* @copyright Copyright (c) 2009-2022, Jonathan Jaubart |
10
|
|
|
* @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 3 |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
declare(strict_types=1); |
14
|
|
|
|
15
|
|
|
namespace MyArtJaub\Webtrees\Module\PatronymicLineage\Model; |
16
|
|
|
|
17
|
|
|
use Fisharebest\Webtrees\Auth; |
18
|
|
|
use Fisharebest\Webtrees\Family; |
19
|
|
|
use Fisharebest\Webtrees\I18N; |
20
|
|
|
use Fisharebest\Webtrees\Individual; |
21
|
|
|
use Fisharebest\Webtrees\Registry; |
22
|
|
|
use Fisharebest\Webtrees\Tree; |
23
|
|
|
use Illuminate\Support\Collection; |
24
|
|
|
use MyArtJaub\Webtrees\Module\PatronymicLineage\PatronymicLineageModule; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Build the patronymic lineage for a surname |
28
|
|
|
*/ |
29
|
|
|
class LineageBuilder |
30
|
|
|
{ |
31
|
|
|
private string $surname; |
32
|
|
|
private Tree $tree; |
33
|
|
|
private PatronymicLineageModule $patrolineage_module; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var Collection<string, bool> $used_indis Individuals already processed |
37
|
|
|
*/ |
38
|
|
|
private Collection $used_indis; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Constructor for Lineage Builder |
42
|
|
|
* |
43
|
|
|
* @param string $surname Reference surname |
44
|
|
|
* @param Tree $tree Gedcom tree |
45
|
|
|
*/ |
46
|
|
|
public function __construct(string $surname, Tree $tree, PatronymicLineageModule $patrolineage_module) |
47
|
|
|
{ |
48
|
|
|
$this->surname = $surname; |
49
|
|
|
$this->tree = $tree; |
50
|
|
|
$this->patrolineage_module = $patrolineage_module; |
51
|
|
|
$this->used_indis = new Collection(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Build all patronymic lineages for the reference surname. |
56
|
|
|
* |
57
|
|
|
* @return Collection<LineageRootNode>|NULL List of root patronymic lineages |
58
|
|
|
*/ |
59
|
|
|
public function buildLineages(): ?Collection |
60
|
|
|
{ |
61
|
|
|
$indis = $this->patrolineage_module->individuals( |
62
|
|
|
$this->tree, |
63
|
|
|
$this->surname, |
64
|
|
|
'', |
65
|
|
|
'', |
66
|
|
|
false, |
67
|
|
|
false, |
68
|
|
|
I18N::locale() |
69
|
|
|
); |
70
|
|
|
//Warning - the individuals method returns a clone of individuals objects. Cannot be used for object equality |
71
|
|
|
if (count($indis) === 0) { |
72
|
|
|
return null; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$root_lineages = new Collection(); |
76
|
|
|
|
77
|
|
|
foreach ($indis as $indi) { |
78
|
|
|
/** @var Individual $indi */ |
79
|
|
|
if ($this->used_indis->get($indi->xref(), false) === false) { |
80
|
|
|
$indi_first = $this->getLineageRootIndividual($indi); |
81
|
|
|
if ($indi_first !== null) { |
82
|
|
|
// The root lineage needs to be recreated from the Factory, to retrieve the proper object |
83
|
|
|
$indi_first = Registry::individualFactory()->make($indi_first->xref(), $this->tree); |
84
|
|
|
} |
85
|
|
|
if ($indi_first === null) { |
86
|
|
|
continue; |
87
|
|
|
} |
88
|
|
|
$this->used_indis->put($indi_first->xref(), true); |
89
|
|
|
if ($indi_first->canShow()) { |
90
|
|
|
//Check if the root individual has brothers and sisters, without parents |
91
|
|
|
$indi_first_child_family = $indi_first->childFamilies()->first(); |
92
|
|
|
if ($indi_first_child_family !== null) { |
93
|
|
|
$root_node = new LineageRootNode(null); |
94
|
|
|
$root_node->addFamily($indi_first_child_family); |
95
|
|
|
} else { |
96
|
|
|
$root_node = new LineageRootNode($indi_first); |
97
|
|
|
} |
98
|
|
|
$root_node = $this->buildLineage($root_node); |
99
|
|
|
$root_lineages->add($root_node); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $root_lineages->sort(function (LineageRootNode $a, LineageRootNode $b) { |
105
|
|
|
if ($a->numberChildNodes() === $b->numberChildNodes()) { |
106
|
|
|
return 0; |
107
|
|
|
} |
108
|
|
|
return ($a->numberChildNodes() > $b->numberChildNodes()) ? -1 : 1; |
109
|
|
|
})->values(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Retrieve the root individual, from any individual, by recursion. |
114
|
|
|
* The Root individual is the individual without a father, or without a mother holding the same name. |
115
|
|
|
* |
116
|
|
|
* @param Individual $indi |
117
|
|
|
* @return Individual|NULL Root individual |
118
|
|
|
*/ |
119
|
|
|
private function getLineageRootIndividual(Individual $indi): ?Individual |
120
|
|
|
{ |
121
|
|
|
$child_families = $indi->childFamilies(); |
122
|
|
|
if ($this->used_indis->get($indi->xref(), false) !== false) { |
123
|
|
|
return null; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
foreach ($child_families as $child_family) { |
127
|
|
|
/** @var Family $child_family */ |
128
|
|
|
$child_family->husband(); |
129
|
|
|
if (($husb = $child_family->husband()) !== null) { |
130
|
|
|
if ($husb->isPendingAddition() && $husb->privatizeGedcom(Auth::PRIV_HIDE) === '') { |
131
|
|
|
return $indi; |
132
|
|
|
} |
133
|
|
|
return $this->getLineageRootIndividual($husb); |
134
|
|
|
} elseif (($wife = $child_family->wife()) !== null) { |
135
|
|
|
if (!($wife->isPendingAddition() && $wife->privatizeGedcom(Auth::PRIV_HIDE) === '')) { |
136
|
|
|
$indi_surname = $indi->getAllNames()[$indi->getPrimaryName()]['surname']; |
137
|
|
|
$wife_surname = $wife->getAllNames()[$wife->getPrimaryName()]['surname']; |
138
|
|
|
if ( |
139
|
|
|
$indi->canShowName() |
140
|
|
|
&& $wife->canShowName() |
141
|
|
|
&& I18N::comparator()($indi_surname, $wife_surname) === 0 |
142
|
|
|
) { |
143
|
|
|
return $this->getLineageRootIndividual($wife); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
return $indi; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
return $indi; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Computes descendent Lineage from a node. |
154
|
|
|
* Uses recursion to build the lineage tree |
155
|
|
|
* |
156
|
|
|
* @param LineageNode $node |
157
|
|
|
* @return LineageNode Computed lineage |
158
|
|
|
*/ |
159
|
|
|
private function buildLineage(LineageNode $node): LineageNode |
160
|
|
|
{ |
161
|
|
|
$indi_surname = ''; |
162
|
|
|
|
163
|
|
|
$indi_node = $node->individual(); |
164
|
|
|
if ($indi_node !== null) { |
165
|
|
|
if ($node->families()->count() === 0) { |
166
|
|
|
foreach ($indi_node->spouseFamilies() as $spouse_family) { |
167
|
|
|
$node->addFamily($spouse_family); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
$indi_surname = $indi_node->getAllNames()[$indi_node->getPrimaryName()]['surname'] ?? ''; |
172
|
|
|
$node->rootNode()->addPlace($indi_node->getBirthPlace()); |
173
|
|
|
|
174
|
|
|
//Tag the individual as used |
175
|
|
|
$this->used_indis->put($indi_node->xref(), true); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
foreach ($node->families() as $family_node) { |
179
|
|
|
/** @var Family $spouse_family */ |
180
|
|
|
$spouse_family = $family_node->family; |
181
|
|
|
$spouse_surname = ''; |
182
|
|
|
$spouse = null; |
183
|
|
|
if ( |
184
|
|
|
$indi_node !== null && |
185
|
|
|
($spouse = $spouse_family->spouse($indi_node)) !== null && $spouse->canShowName() |
186
|
|
|
) { |
187
|
|
|
$spouse_surname = $spouse->getAllNames()[$spouse->getPrimaryName()]['surname'] ?? ''; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
$nb_children = $nb_natural = 0; |
191
|
|
|
|
192
|
|
|
foreach ($spouse_family->children() as $child) { |
193
|
|
|
if (!($child->isPendingAddition() && $child->privatizeGedcom(Auth::PRIV_HIDE) === '')) { |
194
|
|
|
$child_surname = $child->getAllNames()[$child->getPrimaryName()]['surname'] ?? ''; |
195
|
|
|
|
196
|
|
|
$nb_children++; |
197
|
|
|
if ($indi_node !== null && $indi_node->sex() === 'F') { //If the root individual is the mother |
198
|
|
|
//Print only lineages of children with the same surname as their mother |
199
|
|
|
//(supposing they are natural children) |
200
|
|
|
/** @psalm-suppress RedundantCondition */ |
201
|
|
|
if ( |
202
|
|
|
$spouse === null || |
203
|
|
|
($spouse_surname !== '' && I18N::comparator()($child_surname, $spouse_surname) != 0) |
204
|
|
|
) { |
205
|
|
|
if (I18N::comparator()($child_surname, $indi_surname) === 0) { |
206
|
|
|
$nb_natural++; |
207
|
|
|
$node_child = new LineageNode($child, $node->rootNode()); |
208
|
|
|
$node_child = $this->buildLineage($node_child); |
209
|
|
|
$node->addChild($spouse_family, $node_child); |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
} else { //If the root individual is the father |
213
|
|
|
$nb_natural++; |
214
|
|
|
//Print if the children does not bear the same name as his mother |
215
|
|
|
//(and different from his father) |
216
|
|
|
if ( |
217
|
|
|
mb_strlen($child_surname) === 0 || |
218
|
|
|
mb_strlen($indi_surname) === 0 || mb_strlen($spouse_surname) === 0 || |
219
|
|
|
I18N::comparator()($child_surname, $indi_surname) === 0 || |
220
|
|
|
I18N::comparator()($child_surname, $spouse_surname) != 0 |
221
|
|
|
) { |
222
|
|
|
$node_child = new LineageNode($child, $node->rootNode()); |
223
|
|
|
$node_child = $this->buildLineage($node_child); |
224
|
|
|
} else { |
225
|
|
|
$node_child = new LineageNode($child, $node->rootNode(), $child_surname); |
226
|
|
|
} |
227
|
|
|
$node->addChild($spouse_family, $node_child); |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
//Do not print other children |
233
|
|
|
if (($nb_children - $nb_natural) > 0) { |
234
|
|
|
$node->addChild($spouse_family, null); |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
return $node; |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
|