ShopProductController.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\ShopProduct;
  5. use App\Settings\GeneralSettings;
  6. use App\Settings\LocaleSettings;
  7. use Illuminate\Contracts\Foundation\Application;
  8. use Illuminate\Contracts\View\Factory;
  9. use Illuminate\Contracts\View\View;
  10. use Illuminate\Http\RedirectResponse;
  11. use Illuminate\Http\Request;
  12. use Illuminate\Http\Response;
  13. use Illuminate\Validation\Rule;
  14. class ShopProductController extends Controller
  15. {
  16. const READ_PERMISSION = 'admin.store.read';
  17. const WRITE_PERMISSION = 'admin.store.write';
  18. const DISABLE_PERMISSION = 'admin.store.disable';
  19. /**
  20. * Display a listing of the resource.
  21. *
  22. * @return Application|Factory|View|Response
  23. */
  24. public function index(LocaleSettings $locale_settings, GeneralSettings $general_settings)
  25. {
  26. $this->checkPermission(self::READ_PERMISSION);
  27. $isStoreEnabled = $general_settings->store_enabled;
  28. return view('admin.store.index', [
  29. 'isStoreEnabled' => $isStoreEnabled,
  30. 'locale_datatables' => $locale_settings->datatables
  31. ]);
  32. }
  33. /**
  34. * Show the form for creating a new resource.
  35. *
  36. * @return Application|Factory|View|Response
  37. */
  38. public function create(GeneralSettings $general_settings)
  39. {
  40. $this->checkPermission(self::WRITE_PERMISSION);
  41. return view('admin.store.create', [
  42. 'currencyCodes' => config('currency_codes'),
  43. 'credits_display_name' => $general_settings->credits_display_name
  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, GeneralSettings $general_settings)
  74. {
  75. $this->checkPermission(self::WRITE_PERMISSION);
  76. return view('admin.store.edit', [
  77. 'currencyCodes' => config('currency_codes'),
  78. 'shopProduct' => $shopProduct,
  79. 'credits_display_name' => $general_settings->credits_display_name
  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. $this->checkPermission(self::DISABLE_PERMISSION);
  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. $this->checkPermission(self::WRITE_PERMISSION);
  124. $shopProduct->delete();
  125. return redirect()->back()->with('success', __('Store item has been removed!'));
  126. }
  127. public function dataTable(Request $request)
  128. {
  129. $query = ShopProduct::query();
  130. return datatables($query)
  131. ->addColumn('actions', function (ShopProduct $shopProduct) {
  132. return '
  133. <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>
  134. <form class="d-inline" onsubmit="return submitResult();" method="post" action="' . route('admin.store.destroy', $shopProduct->id) . '">
  135. ' . csrf_field() . '
  136. ' . method_field('DELETE') . '
  137. <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>
  138. </form>
  139. ';
  140. })
  141. ->addColumn('disabled', function (ShopProduct $shopProduct) {
  142. $checked = $shopProduct->disabled == false ? 'checked' : '';
  143. return '
  144. <form class="d-inline" onsubmit="return submitResult();" method="post" action="' . route('admin.store.disable', $shopProduct->id) . '">
  145. ' . csrf_field() . '
  146. ' . method_field('PATCH') . '
  147. <div class="custom-control custom-switch">
  148. <input ' . $checked . ' name="disabled" onchange="this.form.submit()" type="checkbox" class="custom-control-input" id="switch' . $shopProduct->id . '">
  149. <label class="custom-control-label" for="switch' . $shopProduct->id . '"></label>
  150. </div>
  151. </form>
  152. ';
  153. })
  154. ->editColumn('created_at', function (ShopProduct $shopProduct) {
  155. return $shopProduct->created_at ? $shopProduct->created_at->diffForHumans() : '';
  156. })
  157. ->editColumn('price', function (ShopProduct $shopProduct) {
  158. return $shopProduct->formatToCurrency($shopProduct->price);
  159. })
  160. ->rawColumns(['actions', 'disabled'])
  161. ->make();
  162. }
  163. }