ProductController.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 Exception;
  8. use Illuminate\Contracts\Foundation\Application;
  9. use Illuminate\Contracts\View\Factory;
  10. use Illuminate\Contracts\View\View;
  11. use Illuminate\Http\JsonResponse;
  12. use Illuminate\Http\RedirectResponse;
  13. use Illuminate\Http\Request;
  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. 'locations' => Location::with('nodes')->get(),
  34. 'nests' => Nest::with('eggs')->get(),
  35. ]);
  36. }
  37. public function clone(Request $request, Product $product)
  38. {
  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. "billing_period" => "required|in:hourly,daily,weekly,monthly,quarterly,half-annually,annually",
  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. 'minimum_credits' => config('SETTINGS::USER:MINIMUM_REQUIRED_CREDITS_TO_MAKE_SERVER'),
  89. ]);
  90. }
  91. /**
  92. * Show the form for editing the specified resource.
  93. *
  94. * @param Product $product
  95. * @return Application|Factory|View
  96. */
  97. public function edit(Product $product)
  98. {
  99. return view('admin.products.edit', [
  100. 'product' => $product,
  101. 'locations' => Location::with('nodes')->get(),
  102. 'nests' => Nest::with('eggs')->get(),
  103. ]);
  104. }
  105. /**
  106. * Update the specified resource in storage.
  107. *
  108. * @param Request $request
  109. * @param Product $product
  110. * @return RedirectResponse
  111. */
  112. public function update(Request $request, Product $product): RedirectResponse
  113. {
  114. $request->validate([
  115. "name" => "required|max:30",
  116. "price" => "required|numeric|max:1000000|min:0",
  117. "memory" => "required|numeric|max:1000000|min:5",
  118. "cpu" => "required|numeric|max:1000000|min:0",
  119. "swap" => "required|numeric|max:1000000|min:0",
  120. "description" => "required|string|max:191",
  121. "disk" => "required|numeric|max:1000000|min:5",
  122. "io" => "required|numeric|max:1000000|min:0",
  123. "minimum_credits" => "required|numeric|max:1000000|min:-1",
  124. "databases" => "required|numeric|max:1000000|min:0",
  125. "backups" => "required|numeric|max:1000000|min:0",
  126. "allocations" => "required|numeric|max:1000000|min:0",
  127. "nodes.*" => "required|exists:nodes,id",
  128. "eggs.*" => "required|exists:eggs,id",
  129. "disabled" => "nullable",
  130. "billing_period" => "required|in:hourly,daily,weekly,monthly,quarterly,half-annually,annually",
  131. ]);
  132. $disabled = !is_null($request->input('disabled'));
  133. $product->update(array_merge($request->all(), ['disabled' => $disabled]));
  134. //link nodes and eggs
  135. $product->eggs()->detach();
  136. $product->nodes()->detach();
  137. $product->eggs()->attach($request->input('eggs'));
  138. $product->nodes()->attach($request->input('nodes'));
  139. return redirect()->route('admin.products.index')->with('success', __('Product has been updated!'));
  140. }
  141. /**
  142. * @param Request $request
  143. * @param Product $product
  144. * @return RedirectResponse
  145. */
  146. public function disable(Request $request, Product $product)
  147. {
  148. $product->update(['disabled' => !$product->disabled]);
  149. return redirect()->route('admin.products.index')->with('success', 'Product has been updated!');
  150. }
  151. /**
  152. * Remove the specified resource from storage.
  153. *
  154. * @param Product $product
  155. * @return RedirectResponse
  156. */
  157. public function destroy(Product $product)
  158. {
  159. $servers = $product->servers()->count();
  160. if ($servers > 0) {
  161. return redirect()->back()->with('error', "Product cannot be removed while it's linked to {$servers} servers");
  162. }
  163. $product->delete();
  164. return redirect()->back()->with('success', __('Product has been removed!'));
  165. }
  166. /**
  167. * @return JsonResponse|mixed
  168. *
  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('minimum_credits', function (Product $product) {
  210. return $product->minimum_credits == -1 ? config('SETTINGS::USER:MINIMUM_REQUIRED_CREDITS_TO_MAKE_SERVER') : $product->minimum_credits;
  211. })
  212. ->editColumn('created_at', function (Product $product) {
  213. return $product->created_at ? $product->created_at->diffForHumans() : '';
  214. })
  215. ->rawColumns(['actions', 'disabled'])
  216. ->make();
  217. }
  218. }