ShopProductController.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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, GeneralSettings $general_settings)
  76. {
  77. return view('admin.store.edit', [
  78. 'currencyCodes' => config('currency_codes'),
  79. 'shopProduct' => $shopProduct,
  80. 'credits_display_name' => $general_settings->credits_display_name
  81. ]);
  82. }
  83. /**
  84. * Update the specified resource in storage.
  85. *
  86. * @param Request $request
  87. * @param ShopProduct $shopProduct
  88. * @return RedirectResponse
  89. */
  90. public function update(Request $request, ShopProduct $shopProduct)
  91. {
  92. $request->validate([
  93. 'disabled' => 'nullable',
  94. 'type' => 'required|string',
  95. 'currency_code' => ['required', 'string', 'max:3', Rule::in(config('currency_codes'))],
  96. 'price' => "required|regex:/^\d+(\.\d{1,2})?$/",
  97. 'quantity' => 'required|numeric|max:100000000',
  98. 'description' => 'required|string|max:60',
  99. 'display' => 'required|string|max:60',
  100. ]);
  101. $disabled = !is_null($request->input('disabled'));
  102. $shopProduct->update(array_merge($request->all(), ['disabled' => $disabled]));
  103. return redirect()->route('admin.store.index')->with('success', __('Store item has been updated!'));
  104. }
  105. /**
  106. * @param Request $request
  107. * @param ShopProduct $shopProduct
  108. * @return RedirectResponse
  109. */
  110. public function disable(ShopProduct $shopProduct)
  111. {
  112. $shopProduct->update(['disabled' => !$shopProduct->disabled]);
  113. return redirect()->route('admin.store.index')->with('success', __('Product has been updated!'));
  114. }
  115. /**
  116. * Remove the specified resource from storage.
  117. *
  118. * @param ShopProduct $shopProduct
  119. * @return RedirectResponse
  120. */
  121. public function destroy(ShopProduct $shopProduct)
  122. {
  123. $shopProduct->delete();
  124. return redirect()->back()->with('success', __('Store item has been removed!'));
  125. }
  126. public function dataTable(Request $request)
  127. {
  128. $query = ShopProduct::query();
  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. }