|
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
|
|
View Code Duplication |
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 |
|
|
|
|
|
|
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) { |
|
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
|
|
View Code Duplication |
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>×</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>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '. |
|
208
|
|
|
/* @scrutinizer ignore-type */\Lang::get('message.success').' |
|
209
|
|
|
<button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
|
210
|
|
|
'./* @scrutinizer ignore-type */\Lang::get('message.deleted-successfully').' |
|
211
|
|
|
</div>'; |
|
212
|
|
|
} else { |
|
213
|
|
|
echo "<div class='alert alert-danger alert-dismissable'> |
|
214
|
|
|
<i class='fa fa-ban'></i> |
|
215
|
|
|
<b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '. |
|
216
|
|
|
/* @scrutinizer ignore-type */\Lang::get('message.failed').' |
|
217
|
|
|
<button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
|
218
|
|
|
'./* @scrutinizer ignore-type */\Lang::get('message.select-a-row').' |
|
219
|
|
|
</div>'; |
|
220
|
|
|
//echo \Lang::get('message.select-a-row'); |
|
221
|
|
|
} |
|
222
|
|
|
} catch (\Exception $e) { |
|
223
|
|
|
echo "<div class='alert alert-danger alert-dismissable'> |
|
224
|
|
|
<i class='fa fa-ban'></i> |
|
225
|
|
|
<b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '. |
|
226
|
|
|
/* @scrutinizer ignore-type */\Lang::get('message.failed').' |
|
227
|
|
|
<button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
|
228
|
|
|
'.$e->getMessage().' |
|
229
|
|
|
</div>'; |
|
230
|
|
|
} |
|
231
|
|
|
} |
|
232
|
|
|
} |
|
233
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: