ShopProductController.php 6.8 KB

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