Passed
Push — master ( c3c113...578dae )
by Gianluca
06:19
created

AdminDetailController::page()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 2
c 2
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
4
namespace Mongi\Mongicommerce\Http\Controllers\admin;
5
6
use Illuminate\Http\Request;
7
use Mongi\Mongicommerce\Http\Controllers\Controller;
8
use Mongi\Mongicommerce\Models\Detail;
9
use Mongi\Mongicommerce\Models\DetailValue;
10
11
class AdminDetailController extends Controller
12
{
13
    public function page(){
14
        $types_details = config('mongicommerce.details');
15
        return view('mongicommerce::admin.pages.details.create_details',['types' =>$types_details ]);
16
    }
17
18
    public function setNewDetail(Request $r){
19
20
        $r->validate([
21
            'name' => 'required',
22
            'type' => 'required',
23
            'category' => 'required'
24
        ]);
25
26
        $name = $r->get('name');
27
        $type = $r->get('type');
28
        $values = $r->get('values');
29
        $category_id = $r->get('category');
30
31
        $detail = new Detail();
32
        $detail->category_id = $category_id;
33
        $detail->type = $type;
34
        $detail->name = $name;
35
        $detail->save();
36
37
        if($type === 'select' || $type === 'checkbox' || $type === 'radio'){
38
            foreach ($values as $value){
39
                $datails_value = new DetailValue();
40
                $datails_value->detail_id = $detail->id;
41
                $datails_value->value = $value;
42
                $datails_value->save();
43
            }
44
        }
45
46
        return response()->json(true);
47
    }
48
49
    public function getDetails(Request $r){
50
        $category_id = $r->get('category_id');
51
        $details = Detail::where('category_id',$category_id)->get();
52
        $d = [];
53
        foreach ($details as $detail){
54
            $d[] = [
55
                'name' => $detail->name,
56
                'type' => $detail->type,
57
                'values' => $detail->values,
58
                'html' => $this->generateDetailHtml($detail,$detail->values)
59
            ];
60
        }
61
62
        return response()->json($d);
63
64
    }
65
66
    public function generateDetailHtml($detail,$values){
67
        $type = $detail->type;
68
69
        if($type === 'select'){
70
            $html = '';
71
            $html .= '<select class="form-control">';
72
            $html .= '<option value="">Seleziona</option>';
73
            foreach($values as $value){
74
                $html .= '<option value="'.$value->id.'">'.$value->value.'</option>';
75
            }
76
            $html .= '</select>';
77
        }
78
79
        if($type === 'checkbox'){
80
            $html = '';
81
82
            foreach($values as $value){
83
                $html .= '<div class="custom-control custom-checkbox">';
84
                $html .= '<input type="checkbox" class="custom-control-input" id="defaultUnchecked_'.$value->id.'">';
85
                $html .= '<label class="custom-control-label" for="defaultUnchecked_'.$value->id.'">'.$value->value.'</label>';
86
                $html .= '</div>';
87
            }
88
        }
89
90
        if($type === 'radio'){
91
            $html = '';
92
            foreach($values as $value){
93
                $html .= '<div class="custom-control custom-radio">';
94
                $html .= '<input name="radio_'.$value->detail_id.'"  type="radio" class="custom-control-input" id="defaultradio_'.$value->id.'">';
95
                $html .= '<label class="custom-control-label" for="defaultradio_'.$value->id.'">'.$value->value.'</label>';
96
                $html .= '</div>';
97
            }
98
        }
99
100
        if($type === 'text'){
101
            $html = '<input  type="text" class="form-control" id="text_' . $detail->id . '">';
102
        }
103
104
        if($type === 'number'){
105
            $html = '<input  type="number" class="form-control" id="number_' . $detail->id . '">';
106
        }
107
108
        if($type === 'textarea'){
109
            $html = '<textarea class="form-control" id="textarea_'.$detail->id.'"></textarea>';
110
        }
111
112
        return $html;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $html does not seem to be defined for all execution paths leading up to this point.
Loading history...
113
    }
114
}
115