Completed
Push — development ( c6edbc...615590 )
by Ashutosh
09:59
created

GroupController::update()   C

Complexity

Conditions 10
Paths 284

Size

Total Lines 47
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 47
rs 5.7833
c 0
b 0
f 0
cc 10
nc 284
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace App\Http\Controllers\Product;
4
5
use App\Http\Controllers\Controller;
6
use App\Http\Requests\Product\GroupRequest;
7
use App\Model\Product\ConfigurableOption;
8
use App\Model\Product\GroupFeatures;
9
use App\Model\Product\ProductGroup;
10
use Illuminate\Http\Request;
11
12
class GroupController extends Controller
13
{
14
    public $group;
15
    public $feature;
16
    public $config;
17
18
    public function __construct()
19
    {
20
        $this->middleware('auth');
21
        // $this->middleware('admin');
22
23
        $group = new ProductGroup();
24
        $this->group = $group;
25
26
        $feature = new GroupFeatures();
27
        $this->feature = $feature;
28
29
        $config = new ConfigurableOption();
30
        $this->config = $config;
31
    }
32
33
    /**
34
     * Display a listing of the resource.
35
     *
36
     * @return \Response
37
     */
38
    public function index()
39
    {
40
        try {
41
            return view('themes.default1.product.group.index');
42
        } catch (\Exception $ex) {
43
            return redirect()->back()->with('fails', $ex->getMessage());
44
        }
45
    }
46
47
    public function getGroups()
48
    {
49
        $product_group = ProductGroup::select('id', 'name')->get();
50
51
        return\ DataTables::of($product_group)
52
53
        // return \Datatable::of($this->group->select('id', 'name')->get())
54
55
                        ->editColumn('#', function ($model) {
56
                            return "<input type='checkbox' value=".$model->id.' name=select[] id=check>';
57
                        })
58
                        // ->showColumns('name')
59
                        ->editColumn('features', function ($model) {
60
                            $features = $this->feature->select('features')->where('group_id', $model->id)->get();
61
                            //dd($features);
62
                            $result = [];
63
                            foreach ($features as $key => $feature) {
64
                                //dd($feature);
65
                                $result[$key] = $feature->features;
66
                            }
67
                            //dd($result);
68
                            return implode(',', $result);
69
                        })
70
                        ->addColumn('action', function ($model) {
71
                            return '<a href='.url('groups/'.$model->id.'/edit')." class='btn btn-sm btn-primary'>Edit</a>";
72
                        })
73
                      ->rawColumns(['name', 'features', 'action'])
74
                        ->make(true);
75
    }
76
77
    /**
78
     * Show the form for creating a new resource.
79
     *
80
     * @return \Response
81
     */
82
    public function create()
83
    {
84
        try {
85
            return view('themes.default1.product.group.create');
86
        } catch (\Exception $ex) {
87
            return redirect()->back()->with('fails', $ex->getMessage());
88
        }
89
    }
90
91
    /**
92
     * Store a newly created resource in storage.
93
     *
94
     * @return \Response
95
     */
96
    public function store(GroupRequest $request)
97
    {
98
        try {
99
            $this->group->fill($request->input())->save();
100
101
            $features = $request->input('features');
102
            foreach ($features as $feature) {
103
                $this->feature->create(['group_id' => $this->group->id, 'features' => $feature['name']]);
104
            }
105
106
            $values = $request->input('value');
107
            $prices = $request->input('price');
108
            $title = $request->input('title');
109
            $type = $request->input('type');
110
            $c = count($prices);
111
            for ($i = 0; $i < $c; $i++) {
112
                $this->config->create(['group_id' => $this->group->id, 'type' => $type, 'title' => $title, 'options' => $values[$i]['name'], 'price' => $prices[$i]['name']]);
113
            }
114
115
            return redirect()->back()->with('success', \Lang::get('message.saved-successfully'));
116
        } catch (\Exception $ex) {
117
            return redirect()->back()->with('fails', $ex->getMessage());
118
        }
119
    }
120
121
    /**
122
     * Display the specified resource.
123
     *
124
     * @param int $id
125
     *
126
     * @return \Response
127
     */
128
    public function show($id)
129
    {
130
    }
131
132
    /**
133
     * Show the form for editing the specified resource.
134
     *
135
     * @param int $id
136
     *
137
     * @return \Response
138
     */
139
    public function edit($id)
140
    {
141
        try {
142
            $group = $this->group->where('id', $id)->first();
143
            $features = $this->feature->select('id', 'group_id', 'features')->where('group_id', $id)->get();
144
            $configs = $this->config->select('price', 'options')->where('group_id', $id)->get();
145
            $title = $this->config->where('group_id', $id)->first()->title;
146
            $type = $this->config->where('group_id', $id)->first()->type;
147
148
            return view('themes.default1.product.group.edit', compact('group', 'features', 'configs', 'title', 'type'));
149
        } catch (\Exception $ex) {
150
            return redirect()->back()->with('fails', $ex->getMessage());
151
        }
152
    }
153
154
    /**
155
     * Update the specified resource in storage.
156
     *
157
     * @param int $id
158
     *
159
     * @return \Response
160
     */
161
    // public function update($id, GroupRequest $request)
162
    // {
163
    //     try {
164
    //         $group = $this->group->where('id', $id)->first();
165
    //         $group->fill($request->input())->save();
166
    //         /*
167
    //          * Features
168
    //          */
169
    //         $selects = $this->feature->where('group_id', $id)->get();
170
    //         if (!empty($selects)) {
171
    //             foreach ($selects as $select) {
172
    //                 if ($select) {
173
    //                     $select->delete();
174
    //                 }
175
    //             }
176
    //         }
177
    //         $features = $request->input('features');
178
179
    //         foreach ($features as $feature) {
180
    //             $this->feature->create(['group_id' => $group->id, 'features' => $feature['name']]);
181
    //         }
182
    //         /*
183
    //          * Configurations
184
    //          */
185
186
    //         $deletes = $this->config->where('group_id', $id)->get();
187
    //         if (!empty($deletes)) {
188
    //             foreach ($deletes as $delete) {
189
    //                 if ($delete) {
190
    //                     $delete->delete();
191
    //                 }
192
    //             }
193
    //         }
194
195
    //         $values = $request->input('value');
196
    //         $prices = $request->input('price');
197
    //         $title = $request->input('title');
198
    //         $type = $request->input('type');
199
    //         $c = count($prices);
200
    //         for ($i = 0; $i < $c; $i++) {
201
    //             $this->config->create(['group_id' => $group->id, 'type' => $type, 'title' => $title, 'options' => $values[$i]['name'], 'price' => $prices[$i]['name']]);
202
    //         }
203
204
    //         return redirect()->back()->with('success', \Lang::get('message.updated-successfully'));
205
    //     } catch (\Exception $ex) {
206
    //         return redirect()->back()->with('fails', $ex->getMessage());
207
    //     }
208
    // }
209
210
    /**
211
     * Remove the specified resource from storage.
212
     *
213
     *
214
     * @return \Response
215
     */
216
    public function destroy(Request $request)
217
    {
218
        try {
219
            $ids = $request->input('select');
220
            if (!empty($ids)) {
221
                foreach ($ids as $id) {
222
                    $group = $this->group->where('id', $id)->first();
223
224
                    if ($group) {
225
                        $group->delete();
226
                    } else {
227
                        echo "<div class='alert alert-danger alert-dismissable'>
228
                    <i class='fa fa-ban'></i>
229
                    <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
230
                    /* @scrutinizer ignore-type */\Lang::get('message.failed').'
231
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
232
                        './* @scrutinizer ignore-type */\Lang::get('message.no-record').'
233
                </div>';
234
                        //echo \Lang::get('message.no-record') . '  [id=>' . $id . ']';
235
                    }
236
                }
237
                echo "<div class='alert alert-success alert-dismissable'>
238
                    <i class='fa fa-ban'></i>
239
240
                    <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
241
                    /* @scrutinizer ignore-type */\Lang::get('message.success').'
242
243
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
244
                        './* @scrutinizer ignore-type */\Lang::get('message.deleted-successfully').'
245
                </div>';
246
            } else {
247
                echo "<div class='alert alert-danger alert-dismissable'>
248
                    <i class='fa fa-ban'></i>
249
                    <b>"./** @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
250
                    /** @scrutinizer ignore-type */\Lang::get('message.failed').'
251
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
252
                        './** @scrutinizer ignore-type */\Lang::get('message.select-a-row').'
253
                </div>';
254
                //echo \Lang::get('message.select-a-row');
255
            }
256
        } catch (\Exception $e) {
257
            echo "<div class='alert alert-danger alert-dismissable'>
258
                    <i class='fa fa-ban'></i>
259
                    <b>"./** @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
260
                    /** @scrutinizer ignore-type */\Lang::get('message.failed').'
261
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
262
                        '.$e->getMessage().'
263
                </div>';
264
        }
265
    }
266
}
267