ProductController.php 11 KB

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