ProductController.php 11 KB

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