|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Trees; |
|
4
|
|
|
|
|
5
|
|
|
use App\Note; |
|
6
|
|
|
use Illuminate\Routing\Controller; |
|
7
|
|
|
use Illuminate\Http\Request; |
|
8
|
|
|
use App\Family; |
|
9
|
|
|
use App\Person; |
|
10
|
|
|
|
|
11
|
|
|
class Show extends Controller |
|
12
|
|
|
{ |
|
13
|
|
|
private $persons; |
|
14
|
|
|
private $unions; |
|
15
|
|
|
private $links; |
|
16
|
|
|
private $nest; |
|
17
|
|
|
public function __invoke(Request $request) |
|
18
|
|
|
{ |
|
19
|
|
|
$start_id = $request->get('start_id', 1); |
|
20
|
|
|
$nest = $request->get('nest', 3); |
|
21
|
|
|
$ret = array(); |
|
22
|
|
|
$ret['start'] = $start_id; |
|
23
|
|
|
$this->persons = array(); |
|
24
|
|
|
$this->unions = array(); |
|
25
|
|
|
$this->links = []; |
|
26
|
|
|
$this->nest = $nest; |
|
27
|
|
|
$this->getGraphData(2); |
|
28
|
|
|
$ret['persons'] = $this->persons; |
|
29
|
|
|
$ret['unions'] = $this->unions; |
|
30
|
|
|
$ret['links'] = $this->links; |
|
31
|
|
|
return $ret; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
private function getGraphData($start_id, $nest = 1){ |
|
35
|
|
|
if($this->nest >= $nest){ |
|
36
|
|
|
$nest++; |
|
37
|
|
|
// get unions first |
|
38
|
|
|
$person = Person::find($start_id); |
|
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
// add family |
|
41
|
|
|
$families = Family::where('husband_id', $start_id)->orwhere('wife_id', $start_id)->get(); |
|
42
|
|
|
$own_unions = []; |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
// add children |
|
47
|
|
|
foreach($families as $family) { |
|
48
|
|
|
$family_id = $family->id; |
|
49
|
|
|
|
|
50
|
|
|
$father = Person::find($family->husband_id); |
|
51
|
|
|
$mother = Person::find($family->wife_id); |
|
52
|
|
|
// add partner to person |
|
53
|
|
|
// add partner link |
|
54
|
|
|
|
|
55
|
|
|
if(isset($father->id)){ |
|
56
|
|
|
$_families = Family::where('husband_id', $father->id)->orwhere('wife_id', $father->id)->select('id')->get()->toArray(); |
|
57
|
|
|
$father->setAttribute('own_unions', $_families); |
|
58
|
|
|
$this->persons[$father->id] = $father; |
|
59
|
|
|
$this->links[] = [$father->id, $family_id]; |
|
60
|
|
|
} |
|
61
|
|
|
if(isset($mother->id)){ |
|
62
|
|
|
$_families = Family::where('husband_id', $mother->id)->orwhere('wife_id', $mother->id)->select('id')->get()->toArray(); |
|
63
|
|
|
$mother->setAttribute('own_unions', $_families); |
|
64
|
|
|
$this->persons[$mother->id] = $mother; |
|
65
|
|
|
$this->links[] = [$mother->id, $family_id]; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
// "u1": { "id": "u1", "partner": ["id1", "id2"], "children": ["id3", "id4", "id11"] }, |
|
69
|
|
|
|
|
70
|
|
|
// find children |
|
71
|
|
|
$children = Person::where('child_in_family_id', $family_id)->get(); |
|
72
|
|
|
$children_ids = []; |
|
73
|
|
|
foreach($children as $child){ |
|
74
|
|
|
$child_id = $child->id; |
|
75
|
|
|
// add child to person |
|
76
|
|
|
// parent_union |
|
77
|
|
|
$child_data = Person::find($child_id); |
|
78
|
|
|
$_families = Family::where('husband_id', $child_id)->orwhere('wife_id', $child_id)->select('id')->get()->toArray(); |
|
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
$this->persons[$child_id] = $child_data; |
|
81
|
|
|
|
|
82
|
|
|
// add union-child link |
|
83
|
|
|
$this->links[] = [$family_id, $child_id]; |
|
84
|
|
|
|
|
85
|
|
|
// make union child filds |
|
86
|
|
|
$children_ids[] = $child_id; |
|
87
|
|
|
$this->getGraphData($child_id,$nest); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
// compose union item and add to unions |
|
91
|
|
|
$union = array(); |
|
92
|
|
|
$union['id'] = $family_id; |
|
93
|
|
|
$union['partner'] = [isset($father->id) ? $father->id : null, isset($mother->id)? $mother->id: null]; |
|
94
|
|
|
$union['children'] = $children_ids; |
|
95
|
|
|
$this->unions[$family_id] = $union; |
|
96
|
|
|
$own_unions[] = $family_id; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
return true; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|