ShopProductController.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. * Show the form for editing the specified resource.
  67. *
  68. * @param ShopProduct $shopProduct
  69. * @return Application|Factory|View|Response
  70. */
  71. public function edit(ShopProduct $shopProduct)
  72. {
  73. return view('admin.store.edit', [
  74. 'currencyCodes' => config('currency_codes'),
  75. 'shopProduct' => $shopProduct,
  76. ]);
  77. }
  78. /**
  79. * Update the specified resource in storage.
  80. *
  81. * @param Request $request
  82. * @param ShopProduct $shopProduct
  83. * @return RedirectResponse
  84. */
  85. public function update(Request $request, ShopProduct $shopProduct)
  86. {
  87. $request->validate([
  88. 'disabled' => 'nullable',
  89. 'type' => 'required|string',
  90. 'currency_code' => ['required', 'string', 'max:3', Rule::in(config('currency_codes'))],
  91. 'price' => "required|regex:/^\d+(\.\d{1,2})?$/",
  92. 'quantity' => 'required|numeric|max:100000000',
  93. 'description' => 'required|string|max:60',
  94. 'display' => 'required|string|max:60',
  95. ]);
  96. $disabled = !is_null($request->input('disabled'));
  97. $shopProduct->update(array_merge($request->all(), ['disabled' => $disabled]));
  98. return redirect()->route('admin.store.index')->with('success', __('Store item has been updated!'));
  99. }
  100. /**
  101. * @param Request $request
  102. * @param ShopProduct $shopProduct
  103. * @return RedirectResponse
  104. */
  105. public function disable(Request $request, ShopProduct $shopProduct)
  106. {
  107. $shopProduct->update(['disabled' => !$shopProduct->disabled]);
  108. return redirect()->route('admin.store.index')->with('success', __('Product has been updated!'));
  109. }
  110. /**
  111. * Remove the specified resource from storage.
  112. *
  113. * @param ShopProduct $shopProduct
  114. * @return RedirectResponse
  115. */
  116. public function destroy(ShopProduct $shopProduct)
  117. {
  118. $shopProduct->delete();
  119. return redirect()->back()->with('success', __('Store item has been removed!'));
  120. }
  121. public function dataTable(Request $request)
  122. {
  123. $query = ShopProduct::query();
  124. return datatables($query)
  125. ->addColumn('actions', function (ShopProduct $shopProduct) {
  126. return '
  127. <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>
  128. <form class="d-inline" onsubmit="return submitResult();" method="post" action="' . route('admin.store.destroy', $shopProduct->id) . '">
  129. ' . csrf_field() . '
  130. ' . method_field('DELETE') . '
  131. <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>
  132. </form>
  133. ';
  134. })
  135. ->addColumn('disabled', function (ShopProduct $shopProduct) {
  136. $checked = $shopProduct->disabled == false ? 'checked' : '';
  137. return '
  138. <form class="d-inline" onsubmit="return submitResult();" method="post" action="' . route('admin.store.disable', $shopProduct->id) . '">
  139. ' . csrf_field() . '
  140. ' . method_field('PATCH') . '
  141. <div class="custom-control custom-switch">
  142. <input ' . $checked . ' name="disabled" onchange="this.form.submit()" type="checkbox" class="custom-control-input" id="switch' . $shopProduct->id . '">
  143. <label class="custom-control-label" for="switch' . $shopProduct->id . '"></label>
  144. </div>
  145. </form>
  146. ';
  147. })
  148. ->editColumn('created_at', function (ShopProduct $shopProduct) {
  149. return $shopProduct->created_at ? $shopProduct->created_at->diffForHumans() : '';
  150. })
  151. ->editColumn('price', function (ShopProduct $shopProduct) {
  152. return $shopProduct->formatToCurrency($shopProduct->price);
  153. })
  154. ->rawColumns(['actions', 'disabled'])
  155. ->make();
  156. }
  157. }