Passed
Push — master ( 61e4b3...351cd2 )
by Curtis
10:11 queued 05:03
created

Show::__invoke()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 13
c 1
b 0
f 1
dl 0
loc 17
rs 9.8333
cc 1
nc 1
nop 1
1
<?php
2
3
namespace App\Http\Controllers\Trees;
4
5
use App\Family;
6
use App\Note;
7
use App\Person;
8
use Illuminate\Http\Request;
9
use Illuminate\Routing\Controller;
10
11
class Show extends Controller
12
{
13
    private $persons;
14
    private $unions;
15
    private $links;
16
    private $nest;
17
18
    public function __invoke(Request $request)
19
    {
20
        $start_id = $request->get('start_id', 1);
21
        $nest = $request->get('nest', 3);
22
        $ret = [];
23
        $ret['start'] = (int)$start_id;
24
        $this->persons = [];
25
        $this->unions = [];
26
        $this->links = [];
27
        $this->nest = $nest;
28
        // $this->getGraphData($start_id);
29
        $this->getGraphDataUpward((int)$start_id);
30
        $ret['persons'] = $this->persons;
31
        $ret['unions'] = $this->unions;
32
        $ret['links'] = $this->links;
33
34
        return $ret;
35
    }
36
37
    private function getGraphData($start_id, $nest = 1)
38
    {
39
        if ($this->nest >= $nest) {
40
            $nest++;
41
            
42
            // add family
43
            $families = Family::where('husband_id', $start_id)->orwhere('wife_id', $start_id)->get();
44
            $own_unions = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $own_unions is dead and can be removed.
Loading history...
45
46
            // add children
47
            foreach ($families as $family) {
48
                $family_id = $family->id;
49
                $father = Person::find($family->husband_id);
50
                $mother = Person::find($family->wife_id);
51
                // add partner to person
52
                // add partner link
53
54
                if(isset($father->id)){
55
                    $_families = Family::where('husband_id', $father->id)->orwhere('wife_id', $father->id)->select('id')->get();
56
                    $_union_ids = [];
57
                    foreach($_families as $item){
58
                        $_union_ids[] = 'u'.$item->id;
59
                    }
60
                    $father->setAttribute('own_unions', $_union_ids);
61
                    $this->persons[$father->id] = $father;
62
                    $this->links[] = [$father->id, 'u'.$family_id];
63
                }
64
                if(isset($mother->id)){
65
                    $_families = Family::where('husband_id', $mother->id)->orwhere('wife_id', $mother->id)->select('id')->get();
66
                    $_union_ids = [];
67
                    foreach($_families as $item){
68
                        $_union_ids[] = $item->id;
69
                    }
70
                    $mother->setAttribute('own_unions', $_union_ids);
71
                    $this->persons[$mother->id] = $mother;
72
                    $this->links[] = [$mother->id, 'u'.$family_id];
73
                }
74
75
                // find children
76
                $children = Person::where('child_in_family_id', $family_id)->get();
77
                $children_ids = [];
78
                foreach ($children as $child) {
79
                    $child_id = $child->id;
80
                    // add child to person
81
                    // parent_union
82
                    $child_data = Person::find($child_id);
83
                    $_families = Family::where('husband_id', $child_id)->orwhere('wife_id', $child_id)->select('id')->get();
84
                    $_union_ids = [];
85
                    foreach($_families as $item){
86
                        $_union_ids[] = $item->id;
87
                    }
88
                    $child_data->setAttribute('own_unions', $_union_ids);
89
                    $this->persons[$child_id] = $child_data;
90
91
                    // add union-child link
92
                    $this->links[] = ['u'.$family_id, $child_id];
93
94
                    // make union child filds
95
                    $children_ids[] = $child_id;
96
                    $this->getGraphData($child_id, $nest);
97
                }
98
99
                // compose union item and add to unions
100
                $union = array();
101
                $union['id'] = 'u'.$family_id;
102
                $union['partner'] = [isset($father->id) ? $father->id : null, isset($mother->id)? $mother->id: null];
103
                $union['children'] = $children_ids;
104
                $this->unions['u'.$family_id] = $union;
105
            }
106
        }
107
108
        return true;
109
    }
110
111
    private function getGraphDataUpward($start_id, $nest = 1){
112
        if($this->nest >= $nest){
113
            $nest++;
114
            $person = Person::find($start_id);
115
116
            // do not process for null
117
            if($person == null){
118
                return;
119
            }
120
121
            // do not process again
122
            if(array_key_exists($start_id, $this->persons)){
123
                return;
124
            }
125
            // do self
126
            if(!array_key_exists($start_id, $this->persons)){
127
                // this is not added
128
                $_families = Family::where('husband_id', $start_id)->orwhere('wife_id', $start_id)->select('id')->get();
129
                $_union_ids = [];
130
                foreach($_families as $item){
131
                    $_union_ids[] = 'u'.$item->id;
132
                    // add current family link
133
                    // $this->links[] = [$start_id, 'u'.$item->id];
134
                    array_unshift($this->links,[$start_id, 'u'.$item->id]);
135
                }
136
                $person->setAttribute('own_unions', $_union_ids);
137
                $person->setAttribute('parent_union', 'u'.$person->child_in_family_id);
138
                // add to persons
139
                $this->persons[$start_id] = $person;
140
                
141
142
                // get self's parents data
143
                $p_family_id = $person->child_in_family_id;
144
                if(!empty($p_family_id)){
145
                    // add parent family link
146
                    // $this->links[] = ['u'.$p_family_id,  $start_id] ;
147
                    array_unshift($this->links,['u'.$p_family_id,  $start_id]);
148
                    $p_family = Family::find($p_family_id);
149
                    if(isset($p_family->husband_id)){
150
                        $p_fatherid = $p_family->husband_id;
151
                        $this->getGraphDataUpward($p_fatherid, $nest);
152
                    }
153
                    if(isset($p_family->wife_id)){
154
                        $p_motherid = $p_family->wife_id;
155
                        $this->getGraphDataUpward($p_motherid, $nest);
156
                    }
157
                }
158
            }
159
            // get partner
160
            $cu_families = Family::where('husband_id', $start_id)->orwhere('wife_id', $start_id)->get();
161
            foreach($cu_families as $family){
162
                $family_id = $family->id;
163
                $father = Person::find($family->husband_id);
164
                $mother = Person::find($family->wife_id);
165
                if(isset($father->id)){
166
                    if(!array_key_exists($father->id, $this->persons)){
167
                        // this is not added
168
                        $_families = Family::where('husband_id', $father->id)->orwhere('wife_id', $father->id)->select('id')->get();
169
                        $_union_ids = [];
170
                        foreach($_families as $item){
171
                            $_union_ids[] = 'u'.$item->id;
172
                        }
173
                        $father->setAttribute('own_unions', $_union_ids);
174
                        $father->setAttribute('parent_union', 'u'.$father->child_in_family_id);
175
                        // add to persons
176
                        $this->persons[$father->id] = $father;
177
178
                        // add current family link
179
                        // $this->links[] = [$father->id, 'u'.$family_id];
180
                        array_unshift($this->links,[$father->id, 'u'.$family_id]);
181
                        // get husband's parents data
182
                        $p_family_id = $father->child_in_family_id;
183
                        if(!empty($p_family_id)){
184
                            // add parent family link
185
                            // $this->links[] = ['u'.$p_family_id,  $father->id] ;
186
                            array_unshift($this->links,['u'.$p_family_id,  $father->id]);
187
                            $p_family = Family::find($p_family_id);
188
                            if(isset($p_family->husband_id)){
189
                                $p_fatherid = $p_family->husband_id;
190
                                $this->getGraphDataUpward($p_fatherid, $nest);
191
                            }
192
                            if(isset($p_family->wife_id)){
193
                                $p_motherid = $p_family->wife_id;
194
                                $this->getGraphDataUpward($p_motherid, $nest);
195
                            }
196
                        }
197
                    }
198
                }
199
                if(isset($mother->id)){
200
                    if(!array_key_exists($mother->id, $this->persons)){
201
                        // this is not added 
202
                        $_families = Family::where('husband_id', $mother->id)->orwhere('wife_id', $mother->id)->select('id')->get();
203
                        $_union_ids = [];
204
                        foreach($_families as $item){
205
                            $_union_ids[] = $item->id;
206
                        }
207
                        $mother->setAttribute('own_unions', $_union_ids);
208
                        $mother->setAttribute('parent_union', 'u'.$mother->child_in_family_id);
209
                        // add to person
210
                        $this->persons[$mother->id] = $mother;
211
                        // add current family link
212
                        // $this->links[] = [$mother->id, 'u'.$family_id];
213
                        array_unshift($this->links,[$mother->id, 'u'.$family_id]);
214
                        // get wifee's parents data
215
                        $p_family_id = $mother->child_in_family_id;
216
                        if(!empty($p_family_id)){
217
                            // add parent family link
218
                            // $this->links[] = ['u'.$p_family_id,  $father->id] ;
219
                            array_unshift($this->links,['u'.$p_family_id,  $mother->id]);
220
221
                            $p_family = Family::find($p_family_id);
222
                            if(isset($p_family->husband_id)){
223
                                $p_fatherid = $p_family->husband_id;
224
                                $this->getGraphDataUpward($p_fatherid, $nest);
225
                            }
226
                            if(isset($p_family->wife_id)){
227
                                $p_motherid = $p_family->wife_id;
228
                                $this->getGraphDataUpward($p_motherid, $nest);
229
                            }
230
                        }
231
                    }
232
                }
233
234
                // find children
235
                $children = Person::where('child_in_family_id', $family_id)->get();
236
                $children_ids = [];
237
                foreach ($children as $child) {
238
                    $child_id = $child->id;
239
                    $children_ids[] = $child_id;
240
                }
241
242
                // compose union item and add to unions
243
                $union = array();
244
                $union['id'] = 'u'.$family_id;
245
                $union['partner'] = [isset($father->id) ? $father->id : null, isset($mother->id)? $mother->id: null];
246
                $union['children'] = $children_ids;
247
                $this->unions['u'.$family_id] = $union;
248
            }
249
            // get brother/sisters
250
            $brothers = Person::where('child_in_family_id', $person->child_in_family_id)
251
            ->whereNotNull('child_in_family_id')
252
            ->where('id','<>', $start_id)->get();
253
            foreach($brothers as $brother){
254
                $this->getGraphDataUpward($brother->id, $nest--);
255
            }
256
        }
257
        return;
258
    }
259
}
260