ShopProductController.php 6.6 KB

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