ProductController.php 9.1 KB

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