CreditProductController.php 6.7 KB

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