ProductController.php 11 KB

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