Show   A
last analyzed

Complexity

Total Complexity 38

Size/Duplication

Total Lines 252
Duplicated Lines 0 %

Importance

Changes 4
Bugs 2 Features 2
Metric Value
wmc 38
eloc 151
c 4
b 2
f 2
dl 0
loc 252
rs 9.36

3 Methods

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