Completed
Push — development ( d5f549...e7964c )
by Ashutosh
09:05
created

CategoryController   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 14
eloc 66
dl 0
loc 146
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A update() 0 11 2
A getCategory() 0 23 2
A store() 0 8 2
A index() 0 3 1
B destroy() 0 57 6
A __construct() 0 7 1
1
<?php
2
3
namespace App\Http\Controllers\Product;
4
5
use App\Http\Controllers\Controller;
6
use App\Model\Product\ProductCategory;
7
use Bugsnag;
8
use Illuminate\Http\Request;
9
10
class CategoryController extends Controller
11
{
12
    public $productCategory;
13
14
    public function __construct()
15
    {
16
        $this->middleware('auth');
17
        $this->middleware('admin');
18
19
        $productCategory = new ProductCategory();
20
        $this->productCategory = $productCategory;
21
    }
22
23
    /**
24
     * Display a listing of the resource.
25
     *
26
     * @return \Illuminate\Http\Response
27
     */
28
    public function index()
29
    {
30
        return view('themes.default1.category.index');
31
    }
32
33
    /**
34
     * Show the form for creating a new resource.
35
     *
36
     * @return \Illuminate\Http\Response
37
     */
38
    public function store(Request $request)
39
    {
40
        try {
41
            $productCategory = $this->productCategory->fill($request->input())->save();
42
43
            return redirect()->back()->with('success', \Lang::get('message.saved-successfully'));
44
        } catch (\Exception $ex) {
45
            return redirect()->back()->with('fails', $ex->getMessage());
46
        }
47
    }
48
49
    /*
50
    * Get All the categories
51
    */
52
    public function getCategory()
53
    {
54
        try {
55
            $allCategories = $this->productCategory->select('id', 'category_name')->get();
56
57
            return \DataTables::of($allCategories)
58
         ->addColumn('checkbox', function ($model) {
59
             return "<input type='checkbox' class='category_checkbox' 
60
            value=".$model->id.' name=select[] id=check>';
61
         })
62
         ->addColumn('category_name', function ($model) {
63
             return ucfirst($model->category_name);
64
         })
65
         ->addColumn('action', function ($model) {
66
             return "<p><button data-toggle='modal' 
67
             data-id=".$model->id." data-name= '$model->category_name' 
68
             class='btn btn-sm btn-primary btn-xs editCat'><i class='fa fa-edit'
69
             style='color:white;'> </i>&nbsp;&nbsp;Edit</button>&nbsp;</p>";
70
         })
71
         ->rawColumns(['checkbox', 'category_name', 'action'])
72
         ->make(true);
73
        } catch (\Exception $ex) {
74
            return redirect()->back()->with('fails', $ex->getMessage());
75
        }
76
    }
77
78
    public function update(Request $request, $id)
79
    {
80
        try {
81
            $cat_name = $request->input('category_name');
82
            $category = $this->productCategory->where('id', $id)->update(['category_name' =>$cat_name]);
83
84
            return redirect()->back()->with('success', \Lang::get('message.updated-successfully'));
85
        } catch (\Exception $ex) {
86
            Bugsnag::notifyException($ex);
87
88
            return redirect()->back()->with('fails', $ex->getMessage());
89
        }
90
    }
91
92
    /**
93
     * Remove the specified resource from storage.
94
     *
95
     * @param int $id
96
     *
97
     * @return \Illuminate\Http\Response
98
     */
99
    public function destroy(Request $request)
100
    {
101
        try {
102
            $ids = $request->input('select');
103
            if (!empty($ids)) {
104
                foreach ($ids as $id) {
105
                    if ($id != 1) {
106
                        $category = $this->productCategory->where('id', $id)->first();
107
                        if ($category) {
108
                            $category->delete();
109
                        } else {
110
                            echo "<div class='alert alert-danger alert-dismissable'>
111
                    <i class='fa fa-ban'></i>
112
                    <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
113
                    /* @scrutinizer ignore-type */\Lang::get('message.failed').'
114
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
115
                        './* @scrutinizer ignore-type */\Lang::get('message.no-record').'
116
                </div>';
117
                            //echo \Lang::get('message.no-record') . '  [id=>' . $id . ']';
118
                        }
119
                        echo "<div class='alert alert-success alert-dismissable'>
120
                    <i class='fa fa-ban'></i>
121
122
                    <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
123
                    /* @scrutinizer ignore-type */\Lang::get('message.success').'
124
125
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
126
                        './* @scrutinizer ignore-type */\Lang::get('message.deleted-successfully').'
127
                </div>';
128
                    } else {
129
                        echo "<div class='alert alert-danger alert-dismissable'>
130
                    <i class='fa fa-ban'></i>
131
                    <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
132
                    /* @scrutinizer ignore-type */\Lang::get('message.failed').'
133
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
134
                        './* @scrutinizer ignore-type */\Lang::get('message.can-not-delete-default').'
135
                </div>';
136
                    }
137
                }
138
            } else {
139
                echo "<div class='alert alert-danger alert-dismissable'>
140
                    <i class='fa fa-ban'></i>
141
                    <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
142
                    /* @scrutinizer ignore-type */\Lang::get('message.failed').'
143
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
144
                        './* @scrutinizer ignore-type */\Lang::get('message.select-a-row').'
145
                </div>';
146
                //echo \Lang::get('message.select-a-row');
147
            }
148
        } catch (\Exception $e) {
149
            echo "<div class='alert alert-danger alert-dismissable'>
150
                    <i class='fa fa-ban'></i>
151
                    <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
152
                    /* @scrutinizer ignore-type */
153
                    \Lang::get('message.failed').'
154
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
155
                        '.$e->getMessage().'
156
                </div>';
157
        }
158
    }
159
}
160