Issues (465)

app/Http/Controllers/PedigreeController.php (8 issues)

1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Jobs\ExportGedCom;
6
use App\Models\Family;
7
use App\Models\Note;
8
use App\Models\Person;
9
use App\Models\User;
10
use File;
11
// use LaravelEnso\Avatars\Models\Avatar;
12
// use LaravelEnso\Files\Models\File as UploadFile;
13
14
// use App\Traits\ConnectionTrait;
15
use GenealogiaWebsite\LaravelGedcom\Utils\GedcomGenerator;
16
use Illuminate\Http\Request;
17
use Illuminate\Routing\Controller;
0 ignored issues
show
This use statement conflicts with another class in this namespace, App\Http\Controllers\Controller. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
18
use LaravelEnso\Core\Models\User as CoreUser;
0 ignored issues
show
The type LaravelEnso\Core\Models\User was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
use Response;
20
21
class PedigreeController extends Controller
22
{
23
    private $persons;
24
    private $unions;
25
    private $links;
26
    private $all;
27
    private $nest;
28
    private $family_id;
29
    private $person_id;
30
    private $child_id;
31
32
    public function show(Request $request)
33
    {
34
        //dd("Here");
35
        $start_id = $request->get('start_id');
36
        $nest = $request->get('nest', 2);
37
        $ret = [];
38
        $ret['start'] = (int) $start_id;
39
        $this->persons = [];
40
        $this->unions = [];
41
        $this->links = [];
42
        $this->nest = $nest;
43
        $this->family_id = [];
44
        $this->person_id = [];
45
        $this->start_id = [];
0 ignored issues
show
Bug Best Practice introduced by
The property start_id does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
46
        $this->child_id = [];
47
        $this->checklink = [];
0 ignored issues
show
Bug Best Practice introduced by
The property checklink does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
48
        // $this->getGraphData((int)$start_id);
49
        $this->getGraphDataUpward((int) $start_id);
50
        $ret['checklink'] = $this->checklink;
51
        $ret['persons'] = $this->persons;
52
        $ret['unions'] = $this->unions;
53
        $ret['links'] = $this->links;
54
        $ret['all'] = $this->all;
55
        $ret['family_id'] = $this->family_id;
56
        $ret['person_id'] = $this->person_id;
57
        $ret['start_id'] = $this->start_id;
58
        $ret['child_id'] = $this->child_id;
59
        // ExportGedCom::dispatch(2, $request);
60
        // $file = 'file.GED';
61
        // $destinationPath = public_path().'/upload/';
62
        // $ret['link'] = $destinationPath.$file;
63
64
        return $ret;
65
    }
66
67
    private function getGraphDataUpward($start_id, $nest = 0)
68
    {
69
70
        // $conn = $this->getConnection();
71
        // $db = $this->getDB();
72
        if ($this->nest >= $nest) {
73
            $person = Person::find($start_id);
74
75
            if ($person) {
76
                $user = $person->user;
0 ignored issues
show
The property user does not seem to exist on App\Models\Person. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
77
                if ($user) {
78
                    $av = Avatar::where('user_id', '=', $user->id)->first();
0 ignored issues
show
The type App\Http\Controllers\Avatar was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
79
                    $file = UploadFile::where('attachable_id', '=', $av->id)->where('attachable_type', '=', 'avatar')->first();
0 ignored issues
show
The type App\Http\Controllers\UploadFile was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
80
81
                    if ($file) {
82
                        $person->setAttribute('avatar', '/api/core/avatars/'.$av->id);
83
                    } else {
84
                        $person->setAttribute('avatar', '');
85
                    }
86
                }
87
            }
88
89
            // do not process for null
90
            if ($person == null) {
91
                return;
92
            }
93
94
            // do not process again
95
            if (array_key_exists($start_id, $this->persons)) {
96
                return;
97
            }
98
            // do self
99
            if (! array_key_exists($start_id, $this->persons)) {
100
                // this is not added
101
                $_families = Family::where('husband_id', $start_id)->orwhere('wife_id', $start_id)->select('id')->get();
102
                $_union_ids = [];
103
                foreach ($_families as $item) {
104
                    $_union_ids[] = 'u'.$item->id;
105
                    // add current family link
106
                    // $this->links[] = [$start_id, 'u'.$item->id];
107
                    array_unshift($this->links, [$start_id, 'u'.$item->id]);
108
                }
109
                $person->setAttribute('own_unions', $_union_ids);
110
                $person->setAttribute('parent_union', 'u'.$person->child_in_family_id);
111
                // add to persons
112
                $this->persons[$start_id] = $person;
113
114
                // get self's parents data
115
                if ($person) {
116
                    if ($person->id == 680) {
117
                        array_unshift($this->person_id, 'first'.$person->id);
118
                    }
119
                }
120
                $p_family_id = $person->child_in_family_id;
121
                array_unshift($this->start_id, 'family_in_child_id'.$person->child_in_family_id);
122
                if ($p_family_id) {
123
                    array_unshift($this->family_id, 'first'.$p_family_id);
124
                }
125
                if (! empty($p_family_id)) {
126
                    // add parent family link
127
                    // $this->links[] = ['u'.$p_family_id,  $start_id] ;
128
                    array_unshift($this->links, ['u'.$p_family_id,  $start_id]);
129
                    $p_family = Family::find($p_family_id);
130
                    if (isset($p_family->husband_id)) {
131
                        $p_fatherid = $p_family->husband_id;
132
                        $this->getGraphDataUpward($p_fatherid, $nest + 1);
133
                    }
134
                    if (isset($p_family->wife_id)) {
135
                        $p_motherid = $p_family->wife_id;
136
                        $this->getGraphDataUpward($p_motherid, $nest + 1);
137
                    }
138
                }
139
            }
140
            // get partner
141
            $cu_families = Family::where('husband_id', $start_id)->orwhere('wife_id', $start_id)->get();
142
            foreach ($cu_families as $family) {
143
                $family_id = $family->id;
144
                $father = Person::find($family->husband_id);
145
                $mother = Person::find($family->wife_id);
146
147
                $user = null; //$father->user;
148
149
                if ($user) {
150
                    $av = Avatar::where('user_id', '=', $user->id)->first();
151
                    $file = UploadFile::where('attachable_id', '=', $av->id)->where('attachable_type', '=', 'avatar')->first();
152
153
                    if ($file) {
154
                        $father->setAttribute('avatar', '/api/core/avatars/'.$av->id);
155
                    } else {
156
                        $father->setAttribute('avatar', '');
157
                    }
158
                    $father->setAttribute('relation', 'father');
159
                }
160
161
                if ($mother) {
162
                    $user = $mother->user;
163
164
                    if ($user) {
165
                        $av = Avatar::where('user_id', '=', $user->id)->first();
166
                        $file = UploadFile::where('attachable_id', '=', $av->id)->where('attachable_type', '=', 'avatar')->first();
167
168
                        if ($file) {
169
                            $mother->setAttribute('avatar', '/api/core/avatars/'.$av->id);
170
                        } else {
171
                            $mother->setAttribute('avatar', '');
172
                        }
173
                        $mother->setAttribute('relation', 'mother');
174
                    }
175
                }
176
177
                if (isset($father->id)) {
178
                    if (! array_key_exists($father->id, $this->persons)) {
179
                        // this is not added
180
                        $_families = Family::where('husband_id', $father->id)->orwhere('wife_id', $father->id)->select('id')->get();
181
                        $_union_ids = [];
182
                        foreach ($_families as $item) {
183
                            $_union_ids[] = 'u'.$item->id;
184
                        }
185
                        $father->setAttribute('own_unions', $_union_ids);
186
                        $father->setAttribute('parent_union', 'u'.$father->child_in_family_id);
0 ignored issues
show
The property child_in_family_id does not seem to exist on App\Models\Person. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
187
                        // add to persons
188
                        $this->persons[$father->id] = $father;
189
190
                        // add current family link
191
                        // $this->links[] = [$father->id, 'u'.$family_id];
192
                        array_unshift($this->links, [$father->id, 'u'.$family_id]);
193
                        // get husband's parents data
194
                        $p_family_id = $father->child_in_family_id;
195
                        if ($p_family_id) {
196
                            array_unshift($this->family_id, 'father'.$p_family_id);
197
                        }
198
                        if (! empty($p_family_id)) {
199
                            // add parent family link
200
                            // $this->links[] = ['u'.$p_family_id,  $father->id] ;
201
                            array_unshift($this->links, ['u'.$p_family_id,  $father->id]);
202
                            $p_family = Family::find($p_family_id);
203
                            if (isset($p_family->husband_id)) {
204
                                $p_fatherid = $p_family->husband_id;
205
                                $this->getGraphDataUpward($p_fatherid, $nest + 1);
206
                            }
207
                            if (isset($p_family->wife_id)) {
208
                                $p_motherid = $p_family->wife_id;
209
                                $this->getGraphDataUpward($p_motherid, $nest + 1);
210
                            }
211
                        }
212
                    }
213
                }
214
                if (isset($mother->id)) {
215
                    if (! array_key_exists($mother->id, $this->persons)) {
216
                        // this is not added
217
                        $_families = Family::where('husband_id', $mother->id)->orwhere('wife_id', $mother->id)->select('id')->get();
218
                        $_union_ids = [];
219
                        foreach ($_families as $item) {
220
                            $_union_ids[] = $item->id;
221
                        }
222
                        $mother->setAttribute('own_unions', $_union_ids);
223
                        $mother->setAttribute('parent_union', 'u'.$mother->child_in_family_id);
224
                        // add to person
225
                        $this->persons[$mother->id] = $mother;
226
                        // add current family link
227
                        // $this->links[] = [$mother->id, 'u'.$family_id];
228
                        array_unshift($this->links, [$mother->id, 'u'.$family_id]);
229
                        // get wifee's parents data
230
                        $p_family_id = $mother->child_in_family_id;
231
                        if ($p_family_id) {
232
                        }
233
                        if (! empty($p_family_id)) {
234
                            // add parent family link
235
                            // $this->links[] = ['u'.$p_family_id,  $father->id] ;
236
                            array_unshift($this->links, ['u'.$p_family_id,  $mother->id]);
237
238
                            $p_family = Family::find($p_family_id);
239
                            if (isset($p_family->husband_id)) {
240
                                array_unshift($this->family_id, 'mother'.$p_family_id);
241
                                $p_fatherid = $p_family->husband_id;
242
                                $this->getGraphDataUpward($p_fatherid, $nest + 1);
243
                            }
244
                            if (isset($p_family->wife_id)) {
245
                                array_unshift($this->family_id, 'mother'.$p_family_id);
246
                                $p_motherid = $p_family->wife_id;
247
                                $this->getGraphDataUpward($p_motherid, $nest + 1);
248
                            }
249
                        }
250
                    }
251
                }
252
253
                // find children
254
                $children = Person::where('child_in_family_id', $family_id)->get();
255
                $children_ids = [];
256
                foreach ($children as $child) {
257
                    $this->getGraphDataUpward($child->id, $nest + 1);
258
                    if ($child->id == 680) {
259
                        array_unshift($this->family_id, $child->id);
260
                    }
261
                    $child_id = $child->id;
262
                    $children_ids[] = $child_id;
263
                }
264
265
                // compose union item and add to unions
266
                $union = [];
267
                $union['id'] = 'u'.$family_id;
268
                $union['partner'] = [isset($father->id) ? $father->id : null, isset($mother->id) ? $mother->id : null];
269
                $union['children'] = $children_ids;
270
                $this->unions['u'.$family_id] = $union;
271
            }
272
            //get brother/sisters
273
            $i = 0;
274
            $brothers = Person::where('child_in_family_id', $person->child_in_family_id)
275
                ->whereNotNull('child_in_family_id')
276
                ->where('id', '<>', $start_id)->get();
277
278
            foreach ($brothers as $brother) {
279
                // if($i < 2) {
280
                $this->getGraphDataUpward($brother->id, $nest + 1);
281
                // }
282
                $i++;
283
            }
284
        } else {
285
            return;
286
        }
287
    }
288
}
289