Test Setup Failed
Push — development ( ac5058...9fa3be )
by Ashutosh
11:12
created

BundleController::update()   A

Complexity

Conditions 6
Paths 18

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 22
rs 9.2222
c 0
b 0
f 0
cc 6
nc 18
nop 2
1
<?php
2
3
namespace App\Http\Controllers\Product;
4
5
use App\Http\Controllers\Controller;
6
use App\Http\Requests\Product\BundleRequest;
7
use App\Model\Product\Product;
8
use App\Model\Product\ProductBundle;
9
use App\Model\Product\ProductBundleRelation;
10
use Illuminate\Http\Request;
11
12
class BundleController extends Controller
13
{
14
    public $product;
15
    public $bundle;
16
    public $relation;
17
18
    public function __construct()
19
    {
20
        $this->middleware('auth');
21
        $this->middleware('admin');
22
23
        $product = new Product();
24
        $this->product = $product;
25
26
        $bundle = new ProductBundle();
27
        $this->bundle = $bundle;
28
29
        $relation = new ProductBundleRelation();
30
        $this->relation = $relation;
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.bundle.index');
42
        } catch (\Exception $ex) {
43
            return redirect()->back()->with('fails', $ex->getMessage());
44
        }
45
    }
46
47
    public function getBundles()
48
    {
49
        return \Datatable::collection($this->bundle->select('id', 'name', 'valid_from', 'valid_till', 'uses', 'maximum_uses')->get())
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 133 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
50
                        ->addColumn('#', function ($model) {
51
                            return "<input type='checkbox' value=".$model->id.' name=select[] id=check>';
52
                        })
53
                        ->showColumns('name', 'valid_from', 'valid_till', 'uses', 'maximum_uses')
54
                        ->addColumn('item', function ($model) {
55
                            $name = $this->relation->where('bundle_id', $model->id)->pluck('product_id');
56
                            //dd($name);
57
                            $result = [];
58
                            foreach ($name as $key => $id) {
59
                                $result[$key] = (string) $this->product->where('id', $id)->first()->name;
60
                            }
61
                            //dd($result);
62
                            return implode(',', $result);
63
                        })
64
                        ->addColumn('action', function ($model) {
65
                            return '<a href='.url('bundles/'.$model->id.'/edit')." class='btn btn-sm btn-primary'>Edit</a>";
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 124 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
66
                        })
67
                        ->searchColumns('name', 'item')
68
                        ->orderColumns('name')
69
                        ->make();
70
    }
71
72
    /**
73
     * Show the form for creating a new resource.
74
     *
75
     * @return \Response
76
     */
77
    public function create()
78
    {
79
        try {
80
            $products = $this->product->lists('name', 'id')->toArray();
81
82
            return view('themes.default1.product.bundle.create', compact('products'));
83
        } catch (\Exception $ex) {
84
            return redirect()->back()->with('fails', $ex->getMessage());
85
        }
86
    }
87
88
    /**
89
     * Store a newly created resource in storage.
90
     *
91
     * @return \Response
92
     */
93
    public function store(BundleRequest $request)
94
    {
95
        try {
96
            $this->bundle->fill($request->input())->save();
97
            $items = $request->input('items');
98
            if (is_array($items) && !empty($items)) {
99
                foreach ($items as $item) {
100
                    $this->relation->create(['product_id' => $item, 'bundle_id' => $this->bundle->id]);
101
                }
102
            }
103
104
            return redirect()->back()->with('success', \Lang::get('message.saved-successfully'));
105
        } catch (\Exception $ex) {
106
            return redirect()->back()->with('fails', $ex->getMessage());
107
        }
108
    }
109
110
    /**
111
     * Display the specified resource.
112
     *
113
     * @param int $id
114
     *
115
     * @return \Response
116
     */
117
    public function show($id)
118
    {
119
        //
120
    }
121
122
    /**
123
     * Show the form for editing the specified resource.
124
     *
125
     * @param int $id
126
     *
127
     * @return \Response
128
     */
129
    public function edit($id)
130
    {
131
        try {
132
            $bundle = $this->bundle->where('id', $id)->first();
133
            $products = $this->product->lists('name', 'id')->toArray();
134
            $relation = $this->relation->where('bundle_id', $id)->lists('product_id', 'product_id')->toArray();
135
            if ($bundle->valid_till != 0) {
136
                $date = new \DateTime($bundle->valid_till);
137
                $till = \Carbon\Carbon::createFromFormat('d/m/Y', $date->format('d/m/Y'));
138
            } else {
139
                $till = null;
140
            }
141
            if ($bundle->valid_from != 0) {
142
                $date2 = new \DateTime($bundle->valid_from);
143
                $from = \Carbon\Carbon::createFromFormat('d/m/Y', $date2->format('d/m/Y'));
144
            } else {
145
                $from = null;
146
            }
147
148
            return view('themes.default1.product.bundle.edit', compact('products', 'bundle', 'relation', 'till', 'from'));
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 122 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
149
        } catch (\Exception $e) {
150
            return redirect()->back()->with('fails', $e->getMessage());
151
        } catch (\InvalidArgumentException $e) {
152
            if ($e->getMessage() == 'Unexpected data found.') {
153
                $till = null;
154
                $from = null;
155
156
                return view('themes.default1.product.bundle.edit', compact('products', 'bundle', 'relation', 'till', 'from'));
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 126 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
157
            }
158
        }
159
    }
160
161
    /**
162
     * Update the specified resource in storage.
163
     *
164
     * @param int $id
165
     *
166
     * @return \Response
167
     */
168
    public function update($id, BundleRequest $request)
169
    {
170
        // dd($request);
171
        try {
172
            $bundle = $this->bundle->where('id', $id)->first();
173
            $bundle->fill($request->input())->save();
174
175
            $items = $request->input('items');
176
            if (is_array($items) && !empty($items)) {
177
                $delete = $this->relation->where('bundle_id', $id)->get();
178
                foreach ($delete as $del) {
179
                    $del->delete();
180
                }
181
182
                foreach ($items as $item) {
183
                    $this->relation->create(['product_id' => $item, 'bundle_id' => $bundle->id]);
184
                }
185
            }
186
187
            return redirect()->back()->with('success', \Lang::get('message.updated-successfully'));
188
        } catch (\Exception $ex) {
189
            return redirect()->back()->with('fails', $ex->getMessage());
190
        }
191
    }
192
193
    /**
194
     * Remove the specified resource from storage.
195
     *
196
     *
197
     * @return \Response
198
     */
199
    public function destroy(Request $request)
200
    {
201
        try {
202
            $ids = $request->input('select');
203
            if (!empty($ids)) {
204
                foreach ($ids as $id) {
205
                    $bundle = $this->bundle->where('id', $id)->first();
206
                    if ($bundle) {
207
                        $bundle->delete();
208
                    } else {
209
                        echo "<div class='alert alert-danger alert-dismissable'>
210
                    <i class='fa fa-ban'></i>
211
                    <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
212
                    /* @scrutinizer ignore-type */\Lang::get('message.failed').'
213
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
214
                        './* @scrutinizer ignore-type */\Lang::get('message.no-record').'
215
                </div>';
216
                        //echo \Lang::get('message.no-record') . '  [id=>' . $id . ']';
217
                    }
218
                }
219
                echo "<div class='alert alert-success alert-dismissable'>
220
                    <i class='fa fa-ban'></i>
221
222
                    <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
223
                    /* @scrutinizer ignore-type */\Lang::get('message.success').'
224
225
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
226
                        './* @scrutinizer ignore-type */\Lang::get('message.deleted-successfully').'
227
                </div>';
228
            } else {
229
                echo "<div class='alert alert-danger alert-dismissable'>
230
                    <i class='fa fa-ban'></i>
231
                    <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
232
                    /* @scrutinizer ignore-type */\Lang::get('message.failed').'
233
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
234
                        './* @scrutinizer ignore-type */\Lang::get('message.select-a-row').'
235
                </div>';
236
                //echo \Lang::get('message.select-a-row');
237
            }
238
        } catch (\Exception $e) {
239
            echo "<div class='alert alert-danger alert-dismissable'>
240
                    <i class='fa fa-ban'></i>
241
                    <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
242
                    /* @scrutinizer ignore-type */\Lang::get('message.failed').'
243
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
244
                        '.$e->getMessage().'
245
                </div>';
246
        }
247
    }
248
}
249