ProductController.php 9.7 KB

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