ProductController.php 7.4 KB

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