|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Trees; |
|
4
|
|
|
|
|
5
|
|
|
use App\Models\Family; |
|
6
|
|
|
use App\Models\Person; |
|
7
|
|
|
use Illuminate\Http\Request; |
|
8
|
|
|
use Illuminate\Routing\Controller; |
|
9
|
|
|
|
|
10
|
|
|
class Ancestors extends Controller |
|
11
|
|
|
{ |
|
12
|
|
|
private ?array $persons = null; |
|
13
|
|
|
private ?array $unions = null; |
|
14
|
|
|
private ?array $links = null; |
|
15
|
|
|
private $nest; |
|
16
|
|
|
|
|
17
|
|
|
public function __invoke(Request $request) |
|
18
|
|
|
{ |
|
19
|
|
|
$start_id = $request->get('start_id'); |
|
20
|
|
|
$nest = $request->get('generation'); |
|
21
|
|
|
$ret = []; |
|
22
|
|
|
$ret['start'] = (int) $start_id; |
|
23
|
|
|
$this->persons = []; |
|
24
|
|
|
$this->unions = []; |
|
25
|
|
|
$this->links = []; |
|
26
|
|
|
$this->nest = $nest; |
|
27
|
|
|
$this->getGraphData((int) $start_id); |
|
28
|
|
|
$ret['persons'] = $this->persons; |
|
29
|
|
|
$ret['unions'] = $this->unions; |
|
30
|
|
|
$ret['links'] = $this->links; |
|
31
|
|
|
|
|
32
|
|
|
return $ret; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
private function getParents(Person $person) |
|
36
|
|
|
{ |
|
37
|
|
|
$p_family_id = $person->child_in_family_id; |
|
|
|
|
|
|
38
|
|
|
$p_family = Family::find($p_family_id); |
|
39
|
|
|
if ($p_family === null) { |
|
40
|
|
|
return []; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
return Person::where('id', $p_family->husband_id)->orwhere('id', $p_family->wife_id)->get(); |
|
|
|
|
|
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
private function getGraphData($start_id, $nest = 1) |
|
47
|
|
|
{ |
|
48
|
|
|
if ($this->nest < $nest) { |
|
49
|
|
|
return; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$person = Person::find($start_id); |
|
53
|
|
|
// do not process for null |
|
54
|
|
|
if ($person === null) { |
|
55
|
|
|
return; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$families = Family::where('husband_id', $start_id)->orwhere('wife_id', $start_id)->get(); |
|
59
|
|
|
if ((is_countable($families) ? count($families) : 0) === 0) { |
|
|
|
|
|
|
60
|
|
|
$person->setAttribute('own_unions', []); |
|
61
|
|
|
$person['generation'] = $nest; |
|
62
|
|
|
$this->persons[$start_id] = $person; |
|
63
|
|
|
|
|
64
|
|
|
return true; |
|
65
|
|
|
} |
|
66
|
|
|
$own_unions = $families->pluck('id')->map(fn ($id) => 'u'.$id)->toArray(); |
|
67
|
|
|
$person->setAttribute('own_unions', $own_unions); |
|
68
|
|
|
$person['generation'] = $nest; |
|
69
|
|
|
$this->persons[$start_id] = $person; |
|
70
|
|
|
|
|
71
|
|
|
foreach ($families as $family) { |
|
72
|
|
|
$union_id = 'u'.$family->id; |
|
73
|
|
|
$own_unions[] = $union_id; |
|
74
|
|
|
$this->links[] = [$start_id, $union_id]; |
|
75
|
|
|
$this->unions[$union_id] = [ |
|
76
|
|
|
'id' => $union_id, |
|
77
|
|
|
'partner' => [$family->husband_id ?? null, $family->wife_id ?? null], |
|
78
|
|
|
'children' => $family->children->pluck('id')->toArray(), |
|
79
|
|
|
]; |
|
80
|
|
|
|
|
81
|
|
|
$parents = $this->getParents($person); |
|
82
|
|
|
foreach ($parents as $parent) { |
|
83
|
|
|
$this->links[] = ['u'.$family->id, $parent->id]; |
|
84
|
|
|
$this->persons[$parent->id] = $parent; |
|
85
|
|
|
$this->getGraphData($parent->id, $nest + 1); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
// private function getGraphData($start_id, $nest = 1) |
|
91
|
|
|
// { |
|
92
|
|
|
// if ($this->nest >= $nest) { |
|
93
|
|
|
|
|
94
|
|
|
// $person = Person::find($start_id); |
|
95
|
|
|
|
|
96
|
|
|
// // do not process for null |
|
97
|
|
|
// if ($person == null) { |
|
98
|
|
|
// return; |
|
99
|
|
|
// } |
|
100
|
|
|
|
|
101
|
|
|
// $families = Family::where('husband_id', $start_id)->orwhere('wife_id', $start_id)->get(); |
|
102
|
|
|
|
|
103
|
|
|
// if (!count($families)) { |
|
104
|
|
|
// $person->setAttribute('own_unions', []); |
|
105
|
|
|
// $person['generation'] = $nest; |
|
106
|
|
|
// $this->persons[$start_id] = $person; |
|
107
|
|
|
|
|
108
|
|
|
// return true; |
|
109
|
|
|
// } |
|
110
|
|
|
|
|
111
|
|
|
// foreach ($families as $family) { |
|
112
|
|
|
|
|
113
|
|
|
// $_union_ids[] = 'u' . $person->child_in_family_id; |
|
114
|
|
|
|
|
115
|
|
|
// $person->setAttribute('own_unions', $_union_ids); |
|
116
|
|
|
// $this->persons[$start_id] = $person; |
|
117
|
|
|
// $this->links[] = [$start_id, 'u' . $person->child_in_family_id]; |
|
118
|
|
|
|
|
119
|
|
|
// // get self's parents data |
|
120
|
|
|
// $p_family_id = $person->child_in_family_id; |
|
121
|
|
|
// $p_family_ids = []; |
|
122
|
|
|
|
|
123
|
|
|
// if (!empty($p_family_id)) { |
|
124
|
|
|
|
|
125
|
|
|
// $p_family = Family::find($p_family_id); |
|
126
|
|
|
|
|
127
|
|
|
// $parent = Person::where('id', $p_family->husband_id)->orwhere('id', $p_family->wife_id)->get(); |
|
128
|
|
|
|
|
129
|
|
|
// foreach ($parent as $p_person) { |
|
130
|
|
|
// $p_data = Person::find($p_person->id); |
|
131
|
|
|
// $p_union_ids = []; |
|
132
|
|
|
// $p_union_ids[] = 'u' . $p_person->child_in_family_id; |
|
133
|
|
|
// $p_data->setAttribute('own_unions', $p_union_ids); |
|
134
|
|
|
// $p_data['generation'] = $nest + 1; |
|
135
|
|
|
// $this->persons[$p_person->id] = $p_data; |
|
136
|
|
|
|
|
137
|
|
|
// // add union-parent link |
|
138
|
|
|
// $this->links[] = ['u' . $family->id, $p_person->id]; |
|
139
|
|
|
// $p_family_ids[] = $p_person->id; |
|
140
|
|
|
// $this->getGraphData($p_person->id, $nest + 1); |
|
141
|
|
|
// } |
|
142
|
|
|
|
|
143
|
|
|
// // get Parents' Partners |
|
144
|
|
|
|
|
145
|
|
|
// // if ($nest <= 6) { |
|
146
|
|
|
// // $parentFamilyIds = Person::where('id', $p_family->husband_id)->orwhere('id', $p_family->wife_id)->select('child_in_family_id')->get(); |
|
147
|
|
|
// // foreach($parentFamilyIds as $parentFamilyId) { |
|
148
|
|
|
// // if (!empty($parentFamilyId)) { |
|
149
|
|
|
// // array_unshift($this->links, ['u'.$parentFamilyId->child_in_family_id, $start_id]); |
|
150
|
|
|
|
|
151
|
|
|
// // $parentFamily = Family::find($parentFamilyId->child_in_family_id); |
|
152
|
|
|
|
|
153
|
|
|
// // if (isset($parentFamily)) { |
|
154
|
|
|
// // // find Parent's Partner |
|
155
|
|
|
// // $parentPartners = Person::where('child_in_family_id', $parentFamily->id)->get(); |
|
156
|
|
|
|
|
157
|
|
|
// // foreach($parentPartners as $parentPartner) { |
|
158
|
|
|
// // $pp_data = Person::find($parentPartner->id); |
|
159
|
|
|
// // $pp_union_ids = []; |
|
160
|
|
|
// // $pp_union_ids[] = 'u'.$parentFamily->id; |
|
161
|
|
|
// // $pp_data->setAttribute('own_unions', $pp_union_ids); |
|
162
|
|
|
// // $pp_data['generation'] = $nest + 1; |
|
163
|
|
|
// // $this->persons[$parentPartner->id] = $pp_data; |
|
164
|
|
|
|
|
165
|
|
|
// // // add union-parent link |
|
166
|
|
|
// // $this->links[] = ['u'.$family->id, $parentPartner->id]; |
|
167
|
|
|
// // $p_family_ids[] = $parentPartner->id; |
|
168
|
|
|
// // $this->getGraphData($parentPartner->id, $nest + 1); |
|
169
|
|
|
// // } |
|
170
|
|
|
// // } |
|
171
|
|
|
// // } |
|
172
|
|
|
// // } |
|
173
|
|
|
// // } |
|
174
|
|
|
|
|
175
|
|
|
// // compose union item and add to unions |
|
176
|
|
|
// $union = []; |
|
177
|
|
|
// $union['id'] = 'u' . $family->id; |
|
178
|
|
|
// $union['partner'] = [isset($family->husband_id) ? $family->husband_id : null, isset($family->wife_id) ? $family->wife_id : null]; |
|
179
|
|
|
// $union['children'] = $p_family_ids; |
|
180
|
|
|
// $this->unions['u' . $p_family_id] = $union; |
|
181
|
|
|
// } |
|
182
|
|
|
// } |
|
183
|
|
|
// } |
|
184
|
|
|
|
|
185
|
|
|
// return true; |
|
186
|
|
|
// } |
|
187
|
|
|
} |
|
188
|
|
|
|