ProductController.php 10 KB

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