Completed
Push — development ( 307e1e...b67852 )
by Ashutosh
09:13
created

AddonController::getAddons()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 23
rs 9.7333
c 0
b 0
f 0
cc 2
nc 1
nop 0
1
<?php
2
3
namespace App\Http\Controllers\Product;
4
5
use App\Http\Controllers\Controller;
6
use App\Http\Requests\Product\AddonRequest;
7
use App\Model\Payment\Plan;
8
use App\Model\Product\Addon;
9
use App\Model\Product\Product;
10
use App\Model\Product\ProductAddonRelation;
11
use App\Model\Product\Subscription;
12
use Illuminate\Http\Request;
13
14
class AddonController extends Controller
15
{
16
    public function __construct()
17
    {
18
        $this->middleware('auth');
19
        $this->middleware('admin');
20
        $product = new Product();
21
        $this->product = $product;
22
        $plan = new plan();
23
        $this->plan = $plan;
24
        $addon = new Addon();
25
        $this->addon = $addon;
26
    }
27
28
    /**
29
     * Display a listing of the resource.
30
     *
31
     * @return \Response
32
     */
33
    public function index()
34
    {
35
        try {
36
            return view('themes.default1.product.addon.index');
37
        } catch (\Exception $ex) {
38
            return redirect()->back()->with('fails', $ex->getMessage());
39
        }
40
    }
41
42
    // public function getAddons()
43
    // {
44
    //     return \Datatable::collection($this->addon->get())
45
    //                     ->addColumn('#', function ($model) {
46
    //                         return "<input type='checkbox' value=".$model->id.' name=select[] id=check>';
47
    //                     })
48
    //                     ->showColumns('name', 'regular_price', 'selling_price')
49
    //                     ->addColumn('associated', function ($model) {
50
    //                         $relations = new ProductAddonRelation();
51
    //                         $relations = $relations->where('addon_id', $model->id)->get();
52
    //                         $products = [];
53
    //                         foreach ($relations as $key => $relation) {
54
    //                             $products[$key] = $this->product->where('id', $relation->product_id)->first()->name;
55
    //                         }
56
57
    //                         return implode(',', $products);
58
    //                     })
59
    //                     ->addColumn('action', function ($model) {
60
    //                         return '<a href='.url('addons/'.$model->id.'/edit')." class='btn btn-sm btn-primary'>Edit</a>";
61
    //                     })
62
    //                     ->searchColumns('name')
63
    //                     ->orderColumns('name')
64
    //                     ->make();
65
    // }
66
67
    /**
68
     * Show the form for creating a new resource.
69
     *
70
     * @return \Response
71
     */
72
    public function create()
73
    {
74
        try {
75
            $product = $this->product->pluck('name', 'id')->toArray();
76
            $subscription = $this->plan->pluck('name', 'id')->toArray();
77
            //dd($subscription);
78
            return view('themes.default1.product.addon.create', compact('product', 'subscription'));
79
        } catch (\Exception $ex) {
80
            return redirect()->back()->with('fails', $ex->getMessage());
81
        }
82
    }
83
84
    /**
85
     * Store a newly created resource in storage.
86
     *
87
     * @return Response
0 ignored issues
show
Bug introduced by
The type App\Http\Controllers\Product\Response was not found. Did you mean Response? If so, make sure to prefix the type with \.
Loading history...
88
     */
89
    public function store(AddonRequest $request)
90
    {
91
        try {
92
            $this->addon->fill($request->input())->save();
93
            $products = $request->input('products');
94
            $relation = new ProductAddonRelation();
95
            if (is_array($products)) {
96
                foreach ($products as $product) {
97
                    if ($product) {
98
                        $relation->create(['addon_id' => $this->addon->id, 'product_id' => $product]);
99
                    }
100
                }
101
            }
102
103
            return redirect()->back()->with('success', \Lang::get('message.saved-successfully'));
104
        } catch (Exception $ex) {
0 ignored issues
show
Bug introduced by
The type App\Http\Controllers\Product\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
105
            return redirect()->back()->with('fails', $ex->getMessage());
106
        }
107
    }
108
109
    /**
110
     * Display the specified resource.
111
     *
112
     * @param int $id
113
     *
114
     * @return Response
115
     */
116
    public function show($id)
117
    {
118
        //
119
    }
120
121
    /**
122
     * Show the form for editing the specified resource.
123
     *
124
     * @param int $id
125
     *
126
     * @return Response
127
     */
128
    public function edit($id)
129
    {
130
        try {
131
            $product = $this->product->pluck('name', 'id')->toArray();
132
            $subscription = $this->plan->pluck('name', 'id')->toArray();
133
            $relation = new ProductAddonRelation();
134
            $relation = $relation->where('addon_id', $id)->pluck('product_id')->toArray();
135
            $addon = $this->addon->where('id', $id)->first();
136
137
            return view('themes.default1.product.addon.edit', compact('product', 'addon', 'subscription', 'relation'));
138
        } catch (\Exception $e) {
139
            return redirect()->back()->with('fails', $e->getMessage());
140
        }
141
    }
142
143
    /**
144
     * Update the specified resource in storage.
145
     *
146
     * @param int $id
147
     *
148
     * @return Response
149
     */
150
    public function update($id, AddonRequest $request)
151
    {
152
        try {
153
            $addon = $this->addon->where('id', $id)->first();
154
            $addon->fill($request->input())->save();
155
156
            $products = $request->input('products');
157
            $relation = new ProductAddonRelation();
158
            if (is_array($products)) {
159
                $delete = $relation->where('addon_id', $id)->get();
160
161
                foreach ($delete as $del) {
162
                    $del->delete();
163
                }
164
165
                foreach ($products as $product) {
166
                    if ($product) {
167
                        $relation->create(['addon_id' => $addon->id, 'product_id' => $product]);
168
                    }
169
                }
170
            }
171
172
            return redirect()->back()->with('success', \Lang::get('message.updated-successfully'));
173
        } catch (\Exception $e) {
174
            return redirect()->back()->with('fails', $e->getMessage());
175
        }
176
    }
177
178
    /**
179
     * Remove the specified resource from storage.
180
     *
181
     * @param int $id
182
     *
183
     * @return Response
184
     */
185
    public function destroy(Request $request)
186
    {
187
        try {
188
            $ids = $request->input('select');
189
            if (!empty($ids)) {
190
                foreach ($ids as $id) {
191
                    $addon = $this->addon->where('id', $id)->first();
192
                    if ($addon) {
193
                        $addon->delete();
194
                    } else {
195
                        echo "<div class='alert alert-danger alert-dismissable'>
196
                    <i class='fa fa-ban'></i>
197
                    <b>"./** @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
198
                    /** @scrutinizer ignore-type */\Lang::get('message.failed').'
199
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
200
                        './** @scrutinizer ignore-type */\Lang::get('message.no-record').'
201
                </div>';
202
                        //echo \Lang::get('message.no-record') . '  [id=>' . $id . ']';
203
                    }
204
                }
205
                echo "<div class='alert alert-success alert-dismissable'>
206
                    <i class='fa fa-ban'></i>
207
                    <b>".\Lang::get('message.alert').'!</b> './** @scrutinizer ignore-type */\Lang::get('message.success').'
0 ignored issues
show
Bug introduced by
Are you sure Lang::get('message.alert') of type null|string|array can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

207
                    <b>"./** @scrutinizer ignore-type */ \Lang::get('message.alert').'!</b> './** @scrutinizer ignore-type */\Lang::get('message.success').'
Loading history...
208
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
209
                        './** @scrutinizer ignore-type */\Lang::get('message.deleted-successfully').'
210
                </div>';
211
            } else {
212
                echo "<div class='alert alert-danger alert-dismissable'>
213
                    <i class='fa fa-ban'></i>
214
                    <b>"./** @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
215
                    /** @scrutinizer ignore-type */\Lang::get('message.failed').'
216
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
217
                        './** @scrutinizer ignore-type */\Lang::get('message.select-a-row').'
218
                </div>';
219
                //echo \Lang::get('message.select-a-row');
220
            }
221
        } catch (\Exception $e) {
222
            echo "<div class='alert alert-danger alert-dismissable'>
223
                    <i class='fa fa-ban'></i>
224
                    <b>".\Lang::get('message.alert').'!</b> '.\Lang::get('message.failed').'
0 ignored issues
show
Bug introduced by
Are you sure Lang::get('message.failed') of type null|string|array can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

224
                    <b>".\Lang::get('message.alert').'!</b> './** @scrutinizer ignore-type */ \Lang::get('message.failed').'
Loading history...
225
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
226
                        '.$e->getMessage().'
227
                </div>';
228
        }
229
    }
230
}
231