This repository has been archived by the owner on Jan 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
descendantnumbergenerator.php
244 lines (197 loc) · 8.27 KB
/
descendantnumbergenerator.php
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<?php
require_once 'idescendantnumberprovider.php';
require_once 'descendantnumberprovidermanager.php';
use Fisharebest\Webtrees\Individual;
/**
* Generates descendant numbering for an ancestor individual.
*/
class DescendantNumberGenerator
{
/**
*
* @var Individual The ancestor.
*/
protected $Ancestor = null;
/**
*
* @var integer The current level. Level 1 = the ancestor, 2 = their children, 3 = grandchildren and so on.
*/
protected $CurrentLevel = 1;
/**
* @var DescendantNumberProviderBase The descendant number provider.
*/
protected $NumberProvider = null;
/**
* Descendant numbering. Key = descendant XREF (string value), value = descendant numbering.
* @var string[]
*/
protected $DescendantNumbering = [];
/**
*
* @param array $numbering
* @param Individual $indi
* @param string $number
*/
protected static function storeNumber(&$numbering, $indi, $number)
{
if($indi->canShow()){
$numbering[$indi->getXref()] = array("name"=>$indi ? $indi->getAllNames()[$indi->getPrimaryName()]["fullNN"]: NULL, "number"=>$number);
}
}
/**
* Generates descendant numbering for the specified ancestor.
* @param Individual $ancestor The ancestor.
* @param DescendantNumberProviderBase $numbering_class The numbering style.
* @param Individual[] $numbering The array of numbered individuals. Used internally for recursion. Must be an empty array when initially called with the ancestor.
* @param integer level The current level of recursion (i. e. the depth of the descendant tree). 1 = the children of the root ancestor, 2 = grandchildren and so on. Used internally.
* @return string[]
*/
protected static function getDescendantNumberingFor($ancestor, $numbering_class, &$numbering = array(), $level = 1)
{
$children = [];
$spouse_families = $ancestor->getSpouseFamilies();
usort($spouse_families,array("Fisharebest\Webtrees\Family","compareMarrDate"));
$cumulative_child_idx = 0;
if(!$numbering)//if the numbering array is empty, the current $ancestor is the "root"
{
self::storeNumber($numbering, $ancestor, $numbering_class->getDescendantNumber(NULL));
}
//spouse numbering - if applicable
for($f = 0; $f <count($spouse_families); $f++)
{
if(!array_key_exists($ancestor->getXref(), $numbering))
continue;
//get spouse number
$spouse_number = $numbering_class->getSpouseNumber($numbering[$ancestor->getXref()]["number"], $f+1);
if($spouse_number === NULL) {continue;}//do not add if numbering not provided
//get the spouse of the ancestor
$fam = $spouse_families[$f];
$wife = $fam->getWife();
$husband = $fam->getHusband();
if($wife == $ancestor) //ancestor is the wife => her spouse is the husband
{
$spouse = $husband;
}
else if($husband == $ancestor) //ancestor is the husband => his spouse is the wife
{
$spouse = $wife;
}
else
{
$spouse = NULL;
}
//add spouse number
if($spouse === NULL){
$numbering["SPOUSE-NULL-{$ancestor->getXref()}-{$fam->getXref()}-{$ancestor->getTree()->getName()}-{$fam->getTree()->getName()}"] = array("number"=>$spouse_number, "name"=>NULL);
//throw new Exception("Failed to retrieve the spouse of {$ancestor} (family: $fam).");
}
else{
self::storeNumber($numbering, $spouse, $spouse_number);
}
}
//descendant numbering
for($f = 0; $f<count($spouse_families); $f++) {
$fam = $spouse_families[$f];
$children = $fam->getChildren();
usort($children,array("Fisharebest\Webtrees\Individual","compareBirthDate"));
for($i = 0; $i<count($children); $i++)
{
if(!array_key_exists($ancestor->getXref(), $numbering))
continue;
$params = new \DescendantNumberParameters();
$params->NthChild = $cumulative_child_idx+$i+1;
$params->NthMarr = $f+1;
$params->NumTotalMarr = count($spouse_families);
$params->ParentNumber = $numbering[$ancestor->getXref()]["number"];
$params->Level = $level;
if(!$children[$i])
{
$numbering["child-$i-of-$ancestor"] = array("number"=>$numbering_class->getDescendantNumber($params), "name"=>NULL);
continue;
}
else {
self::storeNumber($numbering, $children[$i], $numbering_class->getDescendantNumber($params));
}
self::getDescendantNumberingFor($children[$i], $numbering_class,$numbering,$level+1);
}
$cumulative_child_idx+=count($children);
}
return $numbering;
}
/**
* Constructor.
* @param Individual $ancestor
* @param DescendantNumberProviderBase $number_provider
* @param array $parameters Custom parameters for the number provider.
*/
public function __construct($ancestor, $number_provider,$parameters) {
$this->setAncestor($ancestor);
$this->setNumberProvider($number_provider,$parameters);
}
/**
* Sets the descendant number provider.
* @param DescendantNumberProviderBase $number_provider
* @param array $parameters Custom parameters for the number provider.
*/
public function setNumberProvider($number_provider,$parameters)
{
if(is_a($number_provider,"IDescendantNumberProvider"))
{
$this->NumberProvider = $number_provider;
}
else {
$this->NumberProvider = DescendantNumberProviderManager::getProviderByClassName($number_provider);
}
$this->NumberProvider->setParameters($parameters);
}
/**
* Gets the current number provider.
* @return DescendantNumberProviderBase
*/
public function getNumberProvider()
{
return $this->NumberProvider;
}
/**
* Sets the ancestor.
* @global Tree $WT_TREE
* @param Individual $ancestor The ancestor.
*/
public function setAncestor($ancestor)
{
if(is_a($ancestor, "Fisharebest\Webtrees\Individual"))
{
$this->Ancestor = $ancestor;
}
else {
global $WT_TREE;
if(!preg_match("/^I[0-9]+$/", $ancestor))
{
throw new Exception("Invalid ancestor XREF: $ancestor");
}
$this->Ancestor = Individual::getInstance ($ancestor, $WT_TREE);
if(!$this->Ancestor)
{
throw new Exception("Could not load ancestor: $ancestor.");
}
if(!$this->Ancestor->canShow())
{
throw new Exception("The current user cannot access this individual.");
}
}
$this->CurrentLevel = 1;
$this->DescendantNumbering = [];
}
/**
* Gets the descendant numbering for the descendants of the specified ancestor, using the specified numbering style.
* @return string[]
*/
public function getDescendantNumbering()
{
if($this->DescendantNumbering)
{
return $this->DescendantNumbering;
}
return self::getDescendantNumberingFor($this->Ancestor, $this->NumberProvider, $this->DescendantNumbering);
}
}