ProductController.php 7.0 KB

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