ShopProductController.php 6.8 KB

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