AdminDetailController::setNewDetail()   A
last analyzed

Complexity

Conditions 5
Paths 2

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

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