Passed
Push — master ( ccdc18...c3c113 )
by Gianluca
05:54
created

AdminShowDetailController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 28
dl 0
loc 45
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A page() 0 2 1
A getDetails() 0 14 2
A generateDetailHtml() 0 22 5
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 AdminShowDetailController extends Controller
12
{
13
    public function page(){
14
        return view('mongicommerce::admin.pages.details.show_details');
15
    }
16
17
    public function getDetails(Request $r){
18
        $category_id = $r->get('category_id');
19
        $details = Detail::where('category_id',$category_id)->get();
20
        $d = [];
21
        foreach ($details as $detail){
22
            $d[] = [
23
                'name' => $detail->name,
24
                'type' => $detail->type,
25
                'values' => $detail->values,
26
                'html' => $this->generateDetailHtml($detail->type,$detail->values)
27
            ];
28
        }
29
30
        return response()->json($d);
31
32
    }
33
34
    public function generateDetailHtml($type,$values){
35
        if($type === 'select'){
36
            $html = '';
37
            $html .= '<select class="form-control">';
38
            $html .= '<option value="">Seleziona</option>';
39
            foreach($values as $value){
40
                $html .= '<option value="'.$value->id.'">'.$value->value.'</option>';
41
            }
42
            $html .= '</select>';
43
            return $html;
44
        }
45
        if($type === 'checkbox'){
46
            $html = '';
47
48
            foreach($values as $value){
49
                $html .= '<div class="custom-control custom-checkbox">';
50
                $html .= '<input type="checkbox" class="custom-control-input" id="defaultUnchecked_'.$value->id.'">';
51
                $html .= '<label class="custom-control-label" for="defaultUnchecked_'.$value->id.'">'.$value->value.'</label>';
52
                $html .= '</div>';
53
            }
54
55
            return $html;
56
        }
57
    }
58
59
}
60