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 1
/
branchgenerator.php
391 lines (313 loc) · 10.7 KB
/
branchgenerator.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
<?php
namespace BlasiusSecundus\WebtreesModules\BranchExport;
use Fisharebest\Webtrees\Individual;
use Fisharebest\Webtrees\GedcomRecord;
use Fisharebest\Webtrees\Tree;
use Fisharebest\Webtrees\Database;
use Fisharebest\Webtrees\Family;
/**
* Branch generator class.
*/
class BranchGenerator
{
/**
*
* @var string[] An array of indi and family XREFs, used as cutoff points.
*/
protected $InputCutoffPoints = array();
/**
*
* @var string[] An array containing only indi XREFs - all user-provided cutoff individuals + members of cutoff families.
*/
protected $CutoffPoints = array();
/**
*
* @var Individual The pivot individual.
*/
protected $PivotIndi = NULL;
/**
*
* @var Tree The current family tree.
*/
protected $Tree = NULL;
/**
*
* @var Individual[] Associative array of individuals included in the branch (key = XREF, value = Individual object).
*/
protected $BranchIndies = array();
/**
*
* @var string[] XREF of individuals who failed to load while traversing the tree.
*/
protected $NullIndies = array();
/**
*
* @var string[] XREF of individuals who are already processed.
*/
protected $ProcessedIndies = array();
/**
*
* @var type string[] XREFs of records in the branch.
*/
protected $BranchContentXREFs = array();
/**
*
* @var GedcomRecord[] The array of loaded gedcom records (in branch).
*/
protected $BranchRecords = array();
/**
*
* @var GedcomRecord[] The array of loaded cutoff point gedcom records.
*/
protected $CutoffRecords = array();
/**
* Adds all members of the family as cutoff point.
* @param string $cutoff_point The family XREF.
*/
protected function addFamilyCutoffPoint($cutoff_point){
$tree_id = $this->Tree->getTreeId();
$member_rows = Database::prepare("SELECT DISTINCT l_to FROM ##link WHERE l_from = :family AND l_file = :tree_id")->execute(["tree_id"=>$tree_id,"family"=>$cutoff_point])->fetchAll();
foreach($member_rows as $row)
{
if($row->l_to != $this->PivotIndi->getXref())//we don't add the pivot point as cutoff
{
$this->CutoffPoints[] = $row->l_to;
}
}
}
/**
* Preprocesses cutoff points. Family cutoff points will be resolved to individuals.
*/
protected function preprocessCutoffPoints()
{
$this->CutoffPoints = [];
foreach($this->InputCutoffPoints as $cutoff_point)
{
//if individual, we add the indi xref
if(strpos($cutoff_point,"I")!==FALSE)
{
$this->CutoffPoints[] = $cutoff_point;
}
//if family: add family members
else if(strpos($cutoff_point,"F") !== FALSE)
{
$this->addFamilyCutoffPoint($cutoff_point);
}
}
}
/**
* Gets the immediate relatives of an individual.
* @param Individual $indi The individual.
* @return string[] A list of relatives (individual XREFs).
*/
protected function getImmediateRelatives($indi)
{
$related_persons = [];
$tree_id = $this->Tree->getTreeId();
$families = Database::prepare("SELECT DISTINCT l_to FROM `##link` WHERE l_from = :my_xref AND l_file = :tree_id AND (l_type='FAMC' OR l_type='FAMS')")->execute(["my_xref" => $indi->getXref(), "tree_id" => $tree_id])->fetchAll();
foreach($families as $family_row)
{
$relatives_for_current_fam = Database::prepare("SELECT DISTINCT l_from FROM `##link` WHERE l_to = :family_xref AND l_file = :tree_id")->execute(["family_xref" => $family_row->l_to, "tree_id" => $tree_id])->fetchAll();
foreach($relatives_for_current_fam as $relative)
{
$related_persons[] = $relative->l_from;
}
}
return array_unique($related_persons);
}
/**
* Gets the XREFs for all Gedcom records linked to the specified individual.
* @param Individual $indi The individual.
* @return string[] Array containg the XREFs. Returns empty array, if no families found.
*/
protected function getRelatedRecordXrefs($indi)
{
$record_xrefs = array();
$record_rows = Database::prepare("SELECT DISTINCT l_to FROM ##link WHERE l_from = :indi AND l_file = :tree")->execute(["indi"=>$indi->getXref(), "tree" => $this->Tree->getTreeId()])->fetchAll();
foreach($record_rows as $row)
{
$record_obj = \Fisharebest\Webtrees\GedcomRecord::getInstance($row->l_to, $this->Tree);
if($record_obj->canShow()) {
$record_xrefs[] = $row->l_to;
}
}
return $record_xrefs;
}
protected function addNULLIndiToBranch($indi_xref)
{
$this->NullIndies[] = $indi_xref;
if(!in_array($indi_xref, $this->ProcessedIndies))
{
$this->ProcessedIndies[] = $indi_xref;
}
if(!in_array($indi_xref,$this->BranchContentXREFs))
{
$this->BranchContentXREFs[]= $indi_xref;
}
}
protected function addIndiToBranch($indi)
{
if(!$indi->canShow())
return false;
$this->BranchIndies[$indi->getXref()] = $indi;
if(!in_array($indi->getXref(), $this->ProcessedIndies))
{
$this->ProcessedIndies[] = $indi->getXref();
}
$this->BranchContentXREFs[] = $indi->getXref();
$this->BranchContentXREFs = array_unique(array_merge($this->BranchContentXREFs, $this->getRelatedRecordXrefs($indi)));
return true;
}
protected function generateBranchStep($pivot_indi)
{
//pivot indi is always in the branch
if(!$this->addIndiToBranch($pivot_indi))
return;
if(in_array($pivot_indi->getXref(),$this->CutoffPoints))
{
return;
}
$immediate_relatives = $this->getImmediateRelatives($pivot_indi);
foreach($immediate_relatives as $irel_xref)
{
$irel = Individual::getInstance($irel_xref, $this->Tree);
//null indi
if(!$irel)
{
$this->addNULLIndiToBranch($irel_xref);
continue;
}
if(!$irel->canShow())
continue;
if(in_array($irel_xref, $this->CutoffPoints))
{
$this->addIndiToBranch($irel);
}
else if(!in_array($irel_xref, $this->ProcessedIndies))
{
$this->addIndiToBranch($irel);
$this->generateBranchStep($irel);
}
}
}
/**
* Constructor.
* @param Individual $pivot_indi
* @param array $cutoff_points
*/
public function __construct($pivot_indi, $cutoff_points){
global $WT_TREE;
$this->Tree = $WT_TREE;
$this->PivotIndi = is_a($pivot_indi,"Fisharebest\Webtrees\Individual") ? $pivot_indi : Individual::getInstance($pivot_indi, $this->Tree);
$this->InputCutoffPoints = $cutoff_points;
$this->preprocessCutoffPoints();
}
/**
*
* @return Individual
*/
public function getPivotIndi()
{
return $this->PivotIndi;
}
/**
*
* @return string[] Generates the array of XREF in the branch.
*/
public function generateBranch()
{
$this->BranchIndies = array();
$this->NullIndies = array();
$this->ProcessedIndies = array();
$this->BranchContentXREFs = array();
$this->generateBranchStep($this->PivotIndi);
return $this->BranchContentXREFs;
}
/**
* Loads the Gedcom records (Individuals and Families) in the branch. Used to display preview.
* @global \BlasiusSecundus\WebtreesModules\BranchExport\type $WT_TREE
*/
protected function loadBranchRecords()
{
global $WT_TREE;
$this->BranchRecords = [];
if(!$this->BranchContentXREFs)
{
$this->generateBranch();
}
foreach($this->BranchContentXREFs as $xref)
{
$record = GedcomRecord::getInstance($xref, $WT_TREE);
if($record) {$this->BranchRecords[$xref] = $record;}
}
uasort($this->BranchRecords,'\Fisharebest\Webtrees\GedcomRecord::compare');
}
/**
* Loads the cutoff point getcom records.
*/
protected function loadCutoffPointRecords()
{
global $WT_TREE;
$this->CutoffRecords = [];
foreach($this->InputCutoffPoints as $xref)
{
$record = null;
if(strpos($xref,"I") !== FALSE)
{
$record = Individual::getInstance($xref, $WT_TREE);
}
else if(strpos($xref,"F") !== FALSE)
{
$record = Family::getInstance($xref, $WT_TREE);
}
if($record !== NULL){
$this->CutoffRecords[] = $record;
}
}
}
/**
*
* @return GedcomRecord[]
*/
public function getBranchRecords()
{
if(!$this->BranchRecords)
{
$this->loadBranchRecords();
}
return $this->BranchRecords;
}
/**
*
* @return GedcomRecord[]
*/
public function getCutoffpointRecords()
{
if(!$this->CutoffRecords)
{
$this->loadCutoffPointRecords();
}
return $this->CutoffRecords;
}
/**
* Returns the number of instances a particular record type in the branch.
* @param string $type
* @return int
*/
public function numRecordInBranch($type)
{
$num_records = 0;
if(!$this->BranchRecords)
{
$this->loadBranchRecords();
}
foreach($this->BranchRecords as $record)
{
if($record::RECORD_TYPE === $type)
{
$num_records++;
}
}
return $num_records;
}
}