ProductController.php 11 KB

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