ShopProductController.php 6.8 KB

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