ShopProductController.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Models\ShopProduct;
  4. use Illuminate\Contracts\Foundation\Application;
  5. use Illuminate\Contracts\View\Factory;
  6. use Illuminate\Contracts\View\View;
  7. use Illuminate\Http\RedirectResponse;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Http\Response;
  10. use Illuminate\Routing\Controller;
  11. use Illuminate\Validation\Rule;
  12. class ShopProductController extends Controller
  13. {
  14. /**
  15. * Display a listing of the resource.
  16. *
  17. * @return Application|Factory|View|Response
  18. */
  19. public function index(Request $request)
  20. {
  21. $isPaymentSetup = false;
  22. if (
  23. env('APP_ENV') == 'local' ||
  24. config('SETTINGS::PAYMENTS:PAYPAL:SECRET') && config('SETTINGS::PAYMENTS:PAYPAL:CLIENT_ID') ||
  25. config('SETTINGS::PAYMENTS:STRIPE:SECRET') && config('SETTINGS::PAYMENTS:STRIPE:ENDPOINT_SECRET') && config('SETTINGS::PAYMENTS:STRIPE:METHODS')
  26. ) {
  27. $isPaymentSetup = true;
  28. }
  29. return view('admin.store.index', [
  30. 'isPaymentSetup' => $isPaymentSetup,
  31. ]);
  32. }
  33. /**
  34. * Show the form for creating a new resource.
  35. *
  36. * @return Application|Factory|View|Response
  37. */
  38. public function create()
  39. {
  40. return view('admin.store.create', [
  41. 'currencyCodes' => config('currency_codes'),
  42. ]);
  43. }
  44. /**
  45. * Store a newly created resource in storage.
  46. *
  47. * @param Request $request
  48. * @return RedirectResponse
  49. */
  50. public function store(Request $request)
  51. {
  52. $request->validate([
  53. 'disabled' => 'nullable',
  54. 'type' => 'required|string',
  55. 'currency_code' => ['required', 'string', 'max:3', Rule::in(config('currency_codes'))],
  56. 'price' => "required|regex:/^\d+(\.\d{1,2})?$/",
  57. 'quantity' => 'required|numeric',
  58. 'description' => 'required|string|max:60',
  59. 'display' => 'required|string|max:60',
  60. ]);
  61. $disabled = ! is_null($request->input('disabled'));
  62. ShopProduct::create(array_merge($request->all(), ['disabled' => $disabled]));
  63. return redirect()->route('admin.store.index')->with('success', __('Store item has been created!'));
  64. }
  65. /**
  66. * Display the specified resource.
  67. *
  68. * @param ShopProduct $shopProduct
  69. * @return Response
  70. */
  71. public function show(ShopProduct $shopProduct)
  72. {
  73. //
  74. }
  75. /**
  76. * Show the form for editing the specified resource.
  77. *
  78. * @param ShopProduct $shopProduct
  79. * @return Application|Factory|View|Response
  80. */
  81. public function edit(ShopProduct $shopProduct)
  82. {
  83. return view('admin.store.edit', [
  84. 'currencyCodes' => config('currency_codes'),
  85. 'shopProduct' => $shopProduct,
  86. ]);
  87. }
  88. /**
  89. * Update the specified resource in storage.
  90. *
  91. * @param Request $request
  92. * @param ShopProduct $shopProduct
  93. * @return RedirectResponse
  94. */
  95. public function update(Request $request, ShopProduct $shopProduct)
  96. {
  97. $request->validate([
  98. 'disabled' => 'nullable',
  99. 'type' => 'required|string',
  100. 'currency_code' => ['required', 'string', 'max:3', Rule::in(config('currency_codes'))],
  101. 'price' => "required|regex:/^\d+(\.\d{1,2})?$/",
  102. 'quantity' => 'required|numeric|max:100000000',
  103. 'description' => 'required|string|max:60',
  104. 'display' => 'required|string|max:60',
  105. ]);
  106. $disabled = ! is_null($request->input('disabled'));
  107. $shopProduct->update(array_merge($request->all(), ['disabled' => $disabled]));
  108. return redirect()->route('admin.store.index')->with('success', __('Store item has been updated!'));
  109. }
  110. /**
  111. * @param Request $request
  112. * @param ShopProduct $shopProduct
  113. * @return RedirectResponse
  114. */
  115. public function disable(Request $request, ShopProduct $shopProduct)
  116. {
  117. $shopProduct->update(['disabled' => ! $shopProduct->disabled]);
  118. return redirect()->route('admin.store.index')->with('success', __('Product has been updated!'));
  119. }
  120. /**
  121. * Remove the specified resource from storage.
  122. *
  123. * @param ShopProduct $shopProduct
  124. * @return RedirectResponse
  125. */
  126. public function destroy(ShopProduct $shopProduct)
  127. {
  128. $shopProduct->delete();
  129. return redirect()->back()->with('success', __('Store item has been removed!'));
  130. }
  131. public function dataTable()
  132. {
  133. $query = ShopProduct::query();
  134. return datatables($query)
  135. ->addColumn('actions', function (ShopProduct $shopProduct) {
  136. return '
  137. <a data-content="'.__('Edit').'" data-toggle="popover" data-trigger="hover" data-placement="top" href="'.route('admin.store.edit', $shopProduct->id).'" class="btn btn-sm btn-info mr-1"><i class="fas fa-pen"></i></a>
  138. <form class="d-inline" onsubmit="return submitResult();" method="post" action="'.route('admin.store.destroy', $shopProduct->id).'">
  139. '.csrf_field().'
  140. '.method_field('DELETE').'
  141. <button data-content="'.__('Delete').'" data-toggle="popover" data-trigger="hover" data-placement="top" class="btn btn-sm btn-danger mr-1"><i class="fas fa-trash"></i></button>
  142. </form>
  143. ';
  144. })
  145. ->addColumn('disabled', function (ShopProduct $shopProduct) {
  146. $checked = $shopProduct->disabled == false ? 'checked' : '';
  147. return '
  148. <form class="d-inline" onsubmit="return submitResult();" method="post" action="'.route('admin.store.disable', $shopProduct->id).'">
  149. '.csrf_field().'
  150. '.method_field('PATCH').'
  151. <div class="custom-control custom-switch">
  152. <input '.$checked.' name="disabled" onchange="this.form.submit()" type="checkbox" class="custom-control-input" id="switch'.$shopProduct->id.'">
  153. <label class="custom-control-label" for="switch'.$shopProduct->id.'"></label>
  154. </div>
  155. </form>
  156. ';
  157. })
  158. ->editColumn('created_at', function (ShopProduct $shopProduct) {
  159. return $shopProduct->created_at ? $shopProduct->created_at->diffForHumans() : '';
  160. })
  161. ->editColumn('price', function (ShopProduct $shopProduct) {
  162. return $shopProduct->formatToCurrency($shopProduct->price);
  163. })
  164. ->rawColumns(['actions', 'disabled'])
  165. ->make();
  166. }
  167. }