ProductController.php 8.7 KB

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