ProductController.php 9.0 KB

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