Passed
Push — master ( 7df7f9...ae7f27 )
by Curtis
07:07
created

FamilyController::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 2
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers\Family;
4
5
use App\Family;
6
use App\Forms\Builders\FamilyForm;
7
use App\Http\Controllers\Controller;
8
use App\Http\Requests\ValidateFamilyRequest;
9
use App\Individual;
10
use App\Person;
11
12
class FamilyController extends Controller
13
{
14
    private function attach($family, $father_id, $mother_id, $individual_list)
15
    {
16
        Individual::find($father_id)->families()->attach($family->id, ['type_id' => 1]);
17
        $individuals = Individual::findOrFail($father_id);
18
        $individuals->parents()->attach($individual_list);
19
20
        $individuals = Individual::findOrFail($mother_id);
21
        $individuals->parents()->attach($individual_list);
22
        Individual::find($mother_id)->families()->attach($family->id, ['type_id' => 2]);
23
24
        $father = Individual::select('id', 'first_name', 'last_name')->where('id', '=', $father_id)->first();
25
        $mother = Individual::select('id', 'first_name', 'last_name')->where('id', '=', $mother_id)->first();
26
27
        $father_full_name = $father->first_name.' '.$father->last_name;
28
        $mother_full_name = $mother->first_name.' '.$mother->last_name;
29
30
        $father_person = Person::where('name', '=', $father_full_name)
31
            ->first();
32
        $mother_person = Person::where('name', '=', $mother_full_name)
33
            ->first();
34
35
        foreach ($individual_list as $individual) {
36
            $individual = Individual::find($individual);
37
            $individual_full_name = $individual->first_name.' '.$individual->last_name;
38
            $person = Person::select('id', 'name')->where('name', '=', $individual_full_name)
39
                ->first();
40
            $person->father()->associate($father_person);
41
            $person->save();
42
            $person = Person::select('id', 'name')->where('name', '=', $individual_full_name)
43
                ->first();
44
            $person->mother()->associate($mother_person);
45
            $person->save();
46
        }
47
    }
48
49
    public function create(FamilyForm $form)
50
    {
51
        return ['form' => $form->create()];
52
    }
53
54
    public function store(ValidateFamilyRequest $request, Family $family)
55
    {
56
        $family = $family->storeWithIndividuals(
57
            $request->all(),
58
            $request->get('individualList')
59
        );
60
61
        $individuals = $request->get('individualList');
62
63
        $father_id = $request->get('father_id');
64
        $mother_id = $request->get('mother_id');
65
66
        $this->attach($family, $father_id, $mother_id, $individuals);
67
68
69
        return [
70
            'message'  => __('The Family was successfully created'),
71
            'redirect' => 'families.edit',
72
            'id'       => $family->id,
73
        ];
74
    }
75
76
    public function show(Family $family)
77
    {
78
        return $family->individuals()->get(['individuals.id', 'individuals.first_name', 'individuals.last_name']);
79
    }
80
81
    public function edit(Family $family, FamilyForm $form)
82
    {
83
        return ['form' => $form->edit($family)];
84
    }
85
86
    public function update(ValidateFamilyRequest $request, Family $family)
87
    {
88
        $family->updateWithIndividuals(
89
            $request->all(),
90
            $request->get('individualList')
91
        );
92
93
        $individuals = $request->get('individualList');
94
95
        $father_id = $request->get('father_id');
96
        $mother_id = $request->get('mother_id');
97
98
        $this->attach($family, $father_id, $mother_id, $individuals);
99
100
        return ['message' => __('The Family was successfully updated')];
101
    }
102
103
    public function destroy(Family $family)
104
    {
105
        $family->delete();
106
107
        return [
108
            'message'  => __('The Family was successfully deleted'),
109
            'redirect' => 'families.index',
110
        ];
111
    }
112
}
113