Issues (465)

app/Http/Controllers/TreeController.php (16 issues)

1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Models\Company;
6
use App\Models\Family;
7
use App\Models\Person;
8
use App\Models\Tree;
9
use App\Models\User;
10
use Artisan;
11
use DB;
12
use Illuminate\Http\Request;
13
use Spatie\Multitenancy\Models\Concerns\UsesLandlordConnection;
14
15
class TreeController extends Controller
16
{
17
    use UsesLandlordConnection;
18
19
    private $persons;
20
    private $unions;
21
    private $links;
22
    private $nest;
23
24
    /**
25
     * Display a listing of the resource.
26
     *
27
     * @return \Illuminate\Http\Response
28
     */
29
    public function index(Request $request)
30
    {
31
        $query = Tree::query()->with('company');
32
33
        if ($request->has('searchTerm')) {
34
            $columnsToSearch = ['name'];
35
            $search_term = json_decode($request->searchTerm)->searchTerm;
36
            if (! empty($search_term)) {
37
                $searchQuery = '%'.$search_term.'%';
38
                foreach ($columnsToSearch as $column) {
39
                    $query->orWhere($column, 'LIKE', $searchQuery);
40
                }
41
            }
42
        }
43
44
        if ($request->has('columnFilters')) {
45
            $filters = get_object_vars(json_decode($request->columnFilters));
46
47
            foreach ($filters as $key => $value) {
48
                if (! empty($value)) {
49
                    $query->orWhere($key, 'like', '%'.$value.'%');
50
                }
51
            }
52
        }
53
54
        $user = auth()->user();
55
        $company = $user->Company()->pluck('companies.id');
56
        $query->whereIn('company_id', $company);
57
58
        if ($request->has('sort.0')) {
59
            $sort = json_decode($request->sort[0]);
60
            $query->orderBy($sort->field, $sort->type);
61
        }
62
63
        if ($request->has('perPage')) {
64
            $rows = $query->paginate($request->perPage);
65
        }
66
        if (! count($request->all())) {
67
            $rows = $query->get()->toArray();
68
        }
69
70
        return $rows;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $rows does not seem to be defined for all execution paths leading up to this point.
Loading history...
71
    }
72
73
    /**
74
     * Show the form for creating a new resource.
75
     *
76
     * @return \Illuminate\Http\Response
77
     */
78
    public function create()
79
    {
80
        $user = auth()->user();
81
        $companies_id = $user->Company()->pluck('companies.id');
82
        $roles = $user->roles;
83
        $role = $roles[0]->id;
84
        if ($role == 7 || $role == 8) {
85
            if (Tree::whereIn('company_id', $companies_id)->count() < 1) {
86
                return response()->json(['create_tree' => true]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return response()->json(...'create_tree' => true)) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
87
            } else {
88
                return response()->json(['create_tree' => false]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return response()->json(...create_tree' => false)) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
89
            }
90
        } elseif ($role == 5 || $role == 6) {
91
            if (Tree::whereIn('company_id', $companies_id)->count() < 10) {
92
                return response()->json(['create_tree' => true]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return response()->json(...'create_tree' => true)) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
93
            } else {
94
                return response()->json(['create_tree' => false]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return response()->json(...create_tree' => false)) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
95
            }
96
        } elseif ($role == 3 || $role == 4) {
97
            return response()->json(['create_tree' => true]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return response()->json(...'create_tree' => true)) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
98
        } else {
99
            return response()->json(['create_tree' => false]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return response()->json(...create_tree' => false)) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
100
        }
101
    }
102
103
    /**
104
     * Store a newly created resource in storage.
105
     *
106
     * @param  \Illuminate\Http\Request  $request
107
     * @return \Illuminate\Http\Response
108
     */
109
    public function store(Request $request)
110
    {
111
        $request->validate([
112
            'name' => 'required',
113
            'company_id' => 'required',
114
        ]);
115
        $tree_id = Tree::create([
116
            'name' => $request->name,
117
            'company_id' => $request->company_id,
118
            'current_tenant' => 0,
119
            'description' => $request->description,
120
        ])->id;
121
122
        $tenant_id = DB::connection($this->getConnectionName())->table('tenants')->insertGetId([
0 ignored issues
show
The assignment to $tenant_id is dead and can be removed.
Loading history...
123
            'name' => 'tenant'.$tree_id,
124
            'tree_id' => $tree_id,
125
            'database' => 'tenant'.$tree_id,
126
        ]);
127
128
        DB::statement('create database tenant'.$tree_id);
129
130
        Artisan::call('tenants:artisan "migrate --database=tenant --force"');
131
    }
132
133
    /**
134
     * Display the specified resource.
135
     *
136
     * @param  int  $id
137
     * @return \Illuminate\Http\Response
138
     */
139
    public function show(Request $request)
140
    {
141
        $start_id = $request->get('start_id', 3);
142
        $nest = $request->get('nest', 3);
143
        $ret = [];
144
        $ret['start'] = (int) $start_id;
145
        $this->persons = [];
146
        $this->unions = [];
147
        $this->links = [];
148
        $this->nest = $nest;
149
        // $this->getGraphData((int)$start_id);
150
        $this->getGraphDataUpward((int) $start_id);
151
        $ret['persons'] = $this->persons;
152
        $ret['unions'] = $this->unions;
153
        $ret['links'] = $this->links;
154
        // ExportGedCom::dispatch(2, $request);
155
        // $file = 'file.GED';
156
        // $destinationPath = public_path().'/upload/';
157
        // $ret['link'] = $destinationPath.$file;
158
159
        return $ret;
160
    }
161
162
    private function getGraphData($start_id, $nest = 1)
163
    {
164
        $conn = $this->getConnection();
0 ignored issues
show
The method getConnection() does not exist on App\Http\Controllers\TreeController. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

164
        /** @scrutinizer ignore-call */ 
165
        $conn = $this->getConnection();
Loading history...
165
        $db = $this->getDB();
0 ignored issues
show
The assignment to $db is dead and can be removed.
Loading history...
166
167
        if ($this->nest >= $nest) {
168
            $nest++;
169
170
            // add family
171
            $families = Family::on($conn)->where('husband_id', $start_id)->orwhere('wife_id', $start_id)->get();
172
            $own_unions = [];
0 ignored issues
show
The assignment to $own_unions is dead and can be removed.
Loading history...
173
174
            // add children
175
            foreach ($families as $family) {
176
                $family_id = $family->id;
177
                $father = Person::on($conn)->find($family->husband_id);
178
                $mother = Person::on($conn)->find($family->wife_id);
179
                // add partner to person
180
                // add partner link
181
182
                if (isset($father->id)) {
183
                    $_families = Family::on($conn)->where('husband_id', $father->id)->orwhere('wife_id', $father->id)->select('id')->get();
184
                    $_union_ids = [];
185
                    foreach ($_families as $item) {
186
                        $_union_ids[] = 'u'.$item->id;
187
                    }
188
                    $father->setAttribute('own_unions', $_union_ids);
189
                    $this->persons[$father->id] = $father;
190
                    $this->links[] = [$father->id, 'u'.$family_id];
191
                }
192
                if (isset($mother->id)) {
193
                    $_families = Family::on($conn)->where('husband_id', $mother->id)->orwhere('wife_id', $mother->id)->select('id')->get();
194
                    $_union_ids = [];
195
                    foreach ($_families as $item) {
196
                        $_union_ids[] = $item->id;
197
                    }
198
                    $mother->setAttribute('own_unions', $_union_ids);
199
                    $this->persons[$mother->id] = $mother;
200
                    $this->links[] = [$mother->id, 'u'.$family_id];
201
                }
202
203
                // find children
204
                $children = Person::on($conn)->where('child_in_family_id', $family_id)->get();
205
                $children_ids = [];
206
                foreach ($children as $child) {
207
                    $child_id = $child->id;
208
                    // add child to person
209
                    // parent_union
210
                    $child_data = Person::on($conn)->find($child_id);
211
                    $_families = Family::on($conn)->where('husband_id', $child_id)->orwhere('wife_id', $child_id)->select('id')->get();
212
                    $_union_ids = [];
213
                    foreach ($_families as $item) {
214
                        $_union_ids[] = $item->id;
215
                    }
216
                    $child_data->setAttribute('own_unions', $_union_ids);
217
                    $this->persons[$child_id] = $child_data;
218
219
                    // add union-child link
220
                    $this->links[] = ['u'.$family_id, $child_id];
221
222
                    // make union child filds
223
                    $children_ids[] = $child_id;
224
                    $this->getGraphData($child_id, $nest);
225
                }
226
227
                // compose union item and add to unions
228
                $union = [];
229
                $union['id'] = 'u'.$family_id;
230
                $union['partner'] = [isset($father->id) ? $father->id : null, isset($mother->id) ? $mother->id : null];
231
                $union['children'] = $children_ids;
232
                $this->unions['u'.$family_id] = $union;
233
            }
234
        }
235
236
        return true;
237
    }
238
239
    private function getGraphDataUpward($start_id, $nest = 0)
240
    {
241
        $conn = $this->getConnection();
242
        $db = $this->getDB();
0 ignored issues
show
The assignment to $db is dead and can be removed.
Loading history...
243
244
        $threshold = (int) ($this->nest) * 1;
245
        $has = (int) ($nest) * 1;
246
        if ($threshold >= $has) {
247
            $person = Person::on($conn)->find($start_id);
248
            // do not process for null
249
            if ($person == null) {
250
                return;
251
            }
252
253
            // do not process again
254
            if (array_key_exists($start_id, $this->persons)) {
255
                return;
256
            }
257
            // do self
258
            if (! array_key_exists($start_id, $this->persons)) {
259
                // this is not added
260
                $_families = Family::on($conn)->where('husband_id', $start_id)->orwhere('wife_id', $start_id)->select('id')->get();
261
                $_union_ids = [];
262
                foreach ($_families as $item) {
263
                    $_union_ids[] = 'u'.$item->id;
264
                    // add current family link
265
                    // $this->links[] = [$start_id, 'u'.$item->id];
266
                    array_unshift($this->links, [$start_id, 'u'.$item->id]);
267
                }
268
                $person->setAttribute('own_unions', $_union_ids);
269
                $person->setAttribute('parent_union', 'u'.$person->child_in_family_id);
270
                // add to persons
271
                $this->persons[$start_id] = $person;
272
273
                // get self's parents data
274
                $p_family_id = $person->child_in_family_id;
275
                if (! empty($p_family_id)) {
276
                    // add parent family link
277
                    // $this->links[] = ['u'.$p_family_id,  $start_id] ;
278
                    array_unshift($this->links, ['u'.$p_family_id,  $start_id]);
279
                    $p_family = Family::on($conn)->find($p_family_id);
280
                    if (isset($p_family->husband_id)) {
281
                        $p_fatherid = $p_family->husband_id;
282
                        $this->getGraphDataUpward($p_fatherid, $nest + 1);
283
                    }
284
                    if (isset($p_family->wife_id)) {
285
                        $p_motherid = $p_family->wife_id;
286
                        $this->getGraphDataUpward($p_motherid, $nest + 1);
287
                    }
288
                }
289
            }
290
            // get partner
291
            $cu_families = Family::on($conn)->where('husband_id', $start_id)->orwhere('wife_id', $start_id)->get();
292
            foreach ($cu_families as $family) {
293
                $family_id = $family->id;
294
                $father = Person::on($conn)->find($family->husband_id);
295
                $mother = Person::on($conn)->find($family->wife_id);
296
                if (isset($father->id)) {
297
                    if (! array_key_exists($father->id, $this->persons)) {
298
                        // this is not added
299
                        $_families = Family::on($conn)->where('husband_id', $father->id)->orwhere('wife_id', $father->id)->select('id')->get();
300
                        $_union_ids = [];
301
                        foreach ($_families as $item) {
302
                            $_union_ids[] = 'u'.$item->id;
303
                        }
304
                        $father->setAttribute('own_unions', $_union_ids);
305
                        $father->setAttribute('parent_union', 'u'.$father->child_in_family_id);
306
                        // add to persons
307
                        $this->persons[$father->id] = $father;
308
309
                        // add current family link
310
                        // $this->links[] = [$father->id, 'u'.$family_id];
311
                        array_unshift($this->links, [$father->id, 'u'.$family_id]);
312
                        // get husband's parents data
313
                        $p_family_id = $father->child_in_family_id;
314
                        if (! empty($p_family_id)) {
315
                            // add parent family link
316
                            // $this->links[] = ['u'.$p_family_id,  $father->id] ;
317
                            array_unshift($this->links, ['u'.$p_family_id,  $father->id]);
318
                            $p_family = Family::on($conn)->find($p_family_id);
319
                            if (isset($p_family->husband_id)) {
320
                                $p_fatherid = $p_family->husband_id;
321
                                $this->getGraphDataUpward($p_fatherid, $nest + 1);
322
                            }
323
                            if (isset($p_family->wife_id)) {
324
                                $p_motherid = $p_family->wife_id;
325
                                $this->getGraphDataUpward($p_motherid, $nest + 1);
326
                            }
327
                        }
328
                    }
329
                }
330
                if (isset($mother->id)) {
331
                    if (! array_key_exists($mother->id, $this->persons)) {
332
                        // this is not added
333
                        $_families = Family::on($conn)->where('husband_id', $mother->id)->orwhere('wife_id', $mother->id)->select('id')->get();
334
                        $_union_ids = [];
335
                        foreach ($_families as $item) {
336
                            $_union_ids[] = $item->id;
337
                        }
338
                        $mother->setAttribute('own_unions', $_union_ids);
339
                        $mother->setAttribute('parent_union', 'u'.$mother->child_in_family_id);
340
                        // add to person
341
                        $this->persons[$mother->id] = $mother;
342
                        // add current family link
343
                        // $this->links[] = [$mother->id, 'u'.$family_id];
344
                        array_unshift($this->links, [$mother->id, 'u'.$family_id]);
345
                        // get wifee's parents data
346
                        $p_family_id = $mother->child_in_family_id;
347
                        if (! empty($p_family_id)) {
348
                            // add parent family link
349
                            // $this->links[] = ['u'.$p_family_id,  $father->id] ;
350
                            array_unshift($this->links, ['u'.$p_family_id,  $mother->id]);
351
352
                            $p_family = Family::on($conn)->find($p_family_id);
353
                            if (isset($p_family->husband_id)) {
354
                                $p_fatherid = $p_family->husband_id;
355
                                $this->getGraphDataUpward($p_fatherid, $nest + 1);
356
                            }
357
                            if (isset($p_family->wife_id)) {
358
                                $p_motherid = $p_family->wife_id;
359
                                $this->getGraphDataUpward($p_motherid, $nest + 1);
360
                            }
361
                        }
362
                    }
363
                }
364
365
                // find children
366
                $children = Person::on($conn)->where('child_in_family_id', $family_id)->get();
367
                $children_ids = [];
368
                foreach ($children as $child) {
369
                    $child_id = $child->id;
370
                    $children_ids[] = $child_id;
371
                }
372
373
                // compose union item and add to unions
374
                $union = [];
375
                $union['id'] = 'u'.$family_id;
376
                $union['partner'] = [isset($father->id) ? $father->id : null, isset($mother->id) ? $mother->id : null];
377
                $union['children'] = $children_ids;
378
                $this->unions['u'.$family_id] = $union;
379
            }
380
            // get brother/sisters
381
            $brothers = Person::on($conn)->where('child_in_family_id', $person->child_in_family_id)
382
                ->whereNotNull('child_in_family_id')
383
                ->where('id', '<>', $start_id)->get();
384
            // $nest = $nest -1;
385
            foreach ($brothers as $brother) {
386
                $this->getGraphDataUpward($brother->id, $nest);
387
            }
388
        } else {
389
            return;
390
        }
391
    }
392
393
    /**
394
     * Show the form for editing the specified resource.
395
     *
396
     * @param  int  $id
397
     * @return \Illuminate\Http\Response
398
     */
399
    public function edit($id)
400
    {
401
        $tree = Tree::find($id);
402
403
        return $tree;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $tree also could return the type App\Models\Tree which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
404
    }
405
406
    /**
407
     * Update the specified resource in storage.
408
     *
409
     * @param  \Illuminate\Http\Request  $request
410
     * @param  int  $id
411
     * @return \Illuminate\Http\Response
412
     */
413
    public function update(Request $request, $id)
414
    {
415
        $request->validate([
416
            'name' => 'required',
417
            'description' => 'required',
418
        ]);
419
420
        $tree = Tree::find($id);
421
        $tree->name = $request->name;
422
        $tree->description = $request->description;
423
        $tree->save();
424
425
        return $tree;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $tree also could return the type App\Models\Tree which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
426
    }
427
428
    /**
429
     * Remove the specified resource from storage.
430
     *
431
     * @param  int  $id
432
     * @return \Illuminate\Http\Response
433
     */
434
    public function destroy($id)
435
    {
436
        $tree = Tree::find($id);
437
        if ($tree) {
438
            $tree->delete();
439
440
            return 'true';
0 ignored issues
show
Bug Best Practice introduced by
The expression return 'true' returns the type string which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
441
        }
442
443
        return 'false';
0 ignored issues
show
Bug Best Practice introduced by
The expression return 'false' returns the type string which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
444
    }
445
}
446