ProductController.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\Pterodactyl\Location;
  5. use App\Models\Pterodactyl\Nest;
  6. use App\Models\Product;
  7. use App\Settings\UserSettings;
  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(Product $product)
  39. {
  40. return view('admin.products.create', [
  41. 'product' => $product,
  42. 'locations' => Location::with('nodes')->get(),
  43. 'nests' => Nest::with('eggs')->get(),
  44. ]);
  45. }
  46. /**
  47. * Store a newly created resource in storage.
  48. *
  49. * @param Request $request
  50. * @return RedirectResponse
  51. */
  52. public function store(Request $request)
  53. {
  54. $request->validate([
  55. 'name' => 'required|max:30',
  56. 'price' => 'required|numeric|max:1000000|min:0',
  57. 'memory' => 'required|numeric|max:1000000|min:5',
  58. 'cpu' => 'required|numeric|max:1000000|min:0',
  59. 'swap' => 'required|numeric|max:1000000|min:0',
  60. 'description' => 'required|string|max:191',
  61. 'disk' => 'required|numeric|max:1000000|min:5',
  62. 'minimum_credits' => 'required|numeric|max:1000000|min:-1',
  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, UserSettings $user_settings)
  85. {
  86. return view('admin.products.show', [
  87. 'product' => $product,
  88. 'minimum_credits' => $user_settings->min_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. ]);
  131. $disabled = ! is_null($request->input('disabled'));
  132. $product->update(array_merge($request->all(), ['disabled' => $disabled]));
  133. //link nodes and eggs
  134. $product->eggs()->detach();
  135. $product->nodes()->detach();
  136. $product->eggs()->attach($request->input('eggs'));
  137. $product->nodes()->attach($request->input('nodes'));
  138. return redirect()->route('admin.products.index')->with('success', __('Product has been updated!'));
  139. }
  140. /**
  141. * @param Request $request
  142. * @param Product $product
  143. * @return RedirectResponse
  144. */
  145. public function disable(Product $product)
  146. {
  147. $product->update(['disabled' => ! $product->disabled]);
  148. return redirect()->route('admin.products.index')->with('success', 'Product has been updated!');
  149. }
  150. /**
  151. * Remove the specified resource from storage.
  152. *
  153. * @param Product $product
  154. * @return RedirectResponse
  155. */
  156. public function destroy(Product $product)
  157. {
  158. $servers = $product->servers()->count();
  159. if ($servers > 0) {
  160. return redirect()->back()->with('error', "Product cannot be removed while it's linked to {$servers} servers");
  161. }
  162. $product->delete();
  163. return redirect()->back()->with('success', __('Product has been removed!'));
  164. }
  165. /**
  166. * @return JsonResponse|mixed
  167. *
  168. * @throws Exception|Exception
  169. */
  170. public function dataTable()
  171. {
  172. $query = Product::with(['servers']);
  173. return datatables($query)
  174. ->addColumn('actions', function (Product $product) {
  175. return '
  176. <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>
  177. <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>
  178. <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>
  179. <form class="d-inline" onsubmit="return submitResult();" method="post" action="'.route('admin.products.destroy', $product->id).'">
  180. '.csrf_field().'
  181. '.method_field('DELETE').'
  182. <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>
  183. </form>
  184. ';
  185. })
  186. ->addColumn('servers', function (Product $product) {
  187. return $product->servers()->count();
  188. })
  189. ->addColumn('nodes', function (Product $product) {
  190. return $product->nodes()->count();
  191. })
  192. ->addColumn('eggs', function (Product $product) {
  193. return $product->eggs()->count();
  194. })
  195. ->addColumn('disabled', function (Product $product) {
  196. $checked = $product->disabled == false ? 'checked' : '';
  197. return '
  198. <form class="d-inline" onsubmit="return submitResult();" method="post" action="'.route('admin.products.disable', $product->id).'">
  199. '.csrf_field().'
  200. '.method_field('PATCH').'
  201. <div class="custom-control custom-switch">
  202. <input '.$checked.' name="disabled" onchange="this.form.submit()" type="checkbox" class="custom-control-input" id="switch'.$product->id.'">
  203. <label class="custom-control-label" for="switch'.$product->id.'"></label>
  204. </div>
  205. </form>
  206. ';
  207. })
  208. ->editColumn('minimum_credits', function (Product $product) {
  209. return $product->minimum_credits==-1 ? config('SETTINGS::USER:MINIMUM_REQUIRED_CREDITS_TO_MAKE_SERVER') : $product->minimum_credits;
  210. })
  211. ->editColumn('created_at', function (Product $product) {
  212. return $product->created_at ? $product->created_at->diffForHumans() : '';
  213. })
  214. ->rawColumns(['actions', 'disabled'])
  215. ->make();
  216. }
  217. }