ShopProductController.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Models\ShopProduct;
  4. use App\Settings\LocaleSettings;
  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(LocaleSettings $locale_settings)
  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. ) {
  28. $isPaymentSetup = true;
  29. }
  30. return view('admin.store.index', [
  31. 'isPaymentSetup' => $isPaymentSetup,
  32. 'locale_datatables' => $locale_settings->datatables
  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(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. return datatables($query)
  127. ->addColumn('actions', function (ShopProduct $shopProduct) {
  128. return '
  129. <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>
  130. <form class="d-inline" onsubmit="return submitResult();" method="post" action="' . route('admin.store.destroy', $shopProduct->id) . '">
  131. ' . csrf_field() . '
  132. ' . method_field('DELETE') . '
  133. <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>
  134. </form>
  135. ';
  136. })
  137. ->addColumn('disabled', function (ShopProduct $shopProduct) {
  138. $checked = $shopProduct->disabled == false ? 'checked' : '';
  139. return '
  140. <form class="d-inline" onsubmit="return submitResult();" method="post" action="' . route('admin.store.disable', $shopProduct->id) . '">
  141. ' . csrf_field() . '
  142. ' . method_field('PATCH') . '
  143. <div class="custom-control custom-switch">
  144. <input ' . $checked . ' name="disabled" onchange="this.form.submit()" type="checkbox" class="custom-control-input" id="switch' . $shopProduct->id . '">
  145. <label class="custom-control-label" for="switch' . $shopProduct->id . '"></label>
  146. </div>
  147. </form>
  148. ';
  149. })
  150. ->editColumn('created_at', function (ShopProduct $shopProduct) {
  151. return $shopProduct->created_at ? $shopProduct->created_at->diffForHumans() : '';
  152. })
  153. ->editColumn('price', function (ShopProduct $shopProduct) {
  154. return $shopProduct->formatToCurrency($shopProduct->price);
  155. })
  156. ->rawColumns(['actions', 'disabled'])
  157. ->make();
  158. }
  159. }