PaypalProductController.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Models\PaypalProduct;
  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 PaypalProductController 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. $isPaypalSetup = false;
  22. if (env('PAYPAL_SECRET') && env('PAYPAL_CLIENT_ID')) $isPaypalSetup = true;
  23. return view('admin.store.index' , [
  24. 'isPaypalSetup' => $isPaypalSetup
  25. ]);
  26. }
  27. /**
  28. * Show the form for creating a new resource.
  29. *
  30. * @return Application|Factory|View|Response
  31. */
  32. public function create()
  33. {
  34. return view('admin.store.create', [
  35. 'currencyCodes' => config('currency_codes')
  36. ]);
  37. }
  38. /**
  39. * Store a newly created resource in storage.
  40. *
  41. * @param Request $request
  42. * @return RedirectResponse
  43. */
  44. public function store(Request $request)
  45. {
  46. $request->validate([
  47. "disabled" => "nullable",
  48. "type" => "required|string",
  49. "currency_code" => ["required", "string", "max:3", Rule::in(config('currency_codes'))],
  50. "price" => "required|regex:/^\d+(\.\d{1,2})?$/",
  51. "quantity" => "required|numeric",
  52. "description" => "required|string|max:60",
  53. "display" => "required|string|max:60",
  54. ]);
  55. $disabled = !is_null($request->input('disabled'));
  56. PaypalProduct::create(array_merge($request->all(), ['disabled' => $disabled]));
  57. return redirect()->route('admin.store.index')->with('success', 'store item has been created!');
  58. }
  59. /**
  60. * Display the specified resource.
  61. *
  62. * @param PaypalProduct $paypalProduct
  63. * @return Response
  64. */
  65. public function show(PaypalProduct $paypalProduct)
  66. {
  67. //
  68. }
  69. /**
  70. * Show the form for editing the specified resource.
  71. *
  72. * @param PaypalProduct $paypalProduct
  73. * @return Application|Factory|View|Response
  74. */
  75. public function edit(PaypalProduct $paypalProduct)
  76. {
  77. return view('admin.store.edit', [
  78. 'currencyCodes' => config('currency_codes'),
  79. 'paypalProduct' => $paypalProduct
  80. ]);
  81. }
  82. /**
  83. * Update the specified resource in storage.
  84. *
  85. * @param Request $request
  86. * @param PaypalProduct $paypalProduct
  87. * @return RedirectResponse
  88. */
  89. public function update(Request $request, PaypalProduct $paypalProduct)
  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. $paypalProduct->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 PaypalProduct $paypalProduct
  107. * @return RedirectResponse
  108. */
  109. public function disable(Request $request, PaypalProduct $paypalProduct)
  110. {
  111. $paypalProduct->update(['disabled' => !$paypalProduct->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 PaypalProduct $paypalProduct
  118. * @return RedirectResponse
  119. */
  120. public function destroy(PaypalProduct $paypalProduct)
  121. {
  122. $paypalProduct->delete();
  123. return redirect()->back()->with('success', 'store item has been removed!');
  124. }
  125. public function dataTable()
  126. {
  127. $query = PaypalProduct::query();
  128. return datatables($query)
  129. ->addColumn('actions', function (PaypalProduct $paypalProduct) {
  130. return '
  131. <a data-content="Edit" data-toggle="popover" data-trigger="hover" data-placement="top" href="' . route('admin.store.edit', $paypalProduct->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', $paypalProduct->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 (PaypalProduct $paypalProduct) {
  140. $checked = $paypalProduct->disabled == false ? "checked" : "";
  141. return '
  142. <form class="d-inline" onsubmit="return submitResult();" method="post" action="' . route('admin.store.disable', $paypalProduct->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' . $paypalProduct->id . '">
  147. <label class="custom-control-label" for="switch' . $paypalProduct->id . '"></label>
  148. </div>
  149. </form>
  150. ';
  151. })
  152. ->editColumn('created_at', function (PaypalProduct $paypalProduct) {
  153. return $paypalProduct->created_at ? $paypalProduct->created_at->diffForHumans() : '';
  154. })
  155. ->editColumn('price', function (PaypalProduct $paypalProduct) {
  156. return $paypalProduct->formatCurrency();
  157. })
  158. ->rawColumns(['actions', 'disabled'])
  159. ->make();
  160. }
  161. }