|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Pterodactyl\Http\Controllers\Admin; |
| 4 | + |
| 5 | +use Illuminate\Http\Request; |
| 6 | +use Pterodactyl\Models\Mount; |
| 7 | +use Prologue\Alerts\AlertsMessageBag; |
| 8 | +use Pterodactyl\Exceptions\DisplayException; |
| 9 | +use Pterodactyl\Http\Controllers\Controller; |
| 10 | +use Pterodactyl\Services\Mounts\MountUpdateService; |
| 11 | +use Pterodactyl\Http\Requests\Admin\MountFormRequest; |
| 12 | +use Pterodactyl\Services\Mounts\MountCreationService; |
| 13 | +use Pterodactyl\Services\Mounts\MountDeletionService; |
| 14 | +use Pterodactyl\Repositories\Eloquent\MountRepository; |
| 15 | +use Pterodactyl\Contracts\Repository\NestRepositoryInterface; |
| 16 | +use Pterodactyl\Contracts\Repository\LocationRepositoryInterface; |
| 17 | + |
| 18 | +class MountController extends Controller |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @var \Prologue\Alerts\AlertsMessageBag |
| 22 | + */ |
| 23 | + protected $alert; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var \Pterodactyl\Contracts\Repository\NestRepositoryInterface |
| 27 | + */ |
| 28 | + protected $nestRepository; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var \Pterodactyl\Contracts\Repository\LocationRepositoryInterface |
| 32 | + */ |
| 33 | + protected $locationRepository; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var \Pterodactyl\Repositories\Eloquent\MountRepository |
| 37 | + */ |
| 38 | + protected $repository; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var \Pterodactyl\Services\Mounts\MountCreationService |
| 42 | + */ |
| 43 | + protected $creationService; |
| 44 | + |
| 45 | + /** |
| 46 | + * @var \Pterodactyl\Services\Mounts\MountDeletionService |
| 47 | + */ |
| 48 | + protected $deletionService; |
| 49 | + |
| 50 | + /** |
| 51 | + * @var \Pterodactyl\Services\Mounts\MountUpdateService |
| 52 | + */ |
| 53 | + protected $updateService; |
| 54 | + |
| 55 | + /** |
| 56 | + * MountController constructor. |
| 57 | + * |
| 58 | + * @param \Prologue\Alerts\AlertsMessageBag $alert |
| 59 | + * @param \Pterodactyl\Contracts\Repository\NestRepositoryInterface $nestRepository |
| 60 | + * @param \Pterodactyl\Contracts\Repository\LocationRepositoryInterface $locationRepository |
| 61 | + * @param \Pterodactyl\Repositories\Eloquent\MountRepository $repository |
| 62 | + * @param \Pterodactyl\Services\Mounts\MountCreationService $creationService |
| 63 | + * @param \Pterodactyl\Services\Mounts\MountDeletionService $deletionService |
| 64 | + * @param \Pterodactyl\Services\Mounts\MountUpdateService $updateService |
| 65 | + */ |
| 66 | + public function __construct( |
| 67 | + AlertsMessageBag $alert, |
| 68 | + NestRepositoryInterface $nestRepository, |
| 69 | + LocationRepositoryInterface $locationRepository, |
| 70 | + MountRepository $repository, |
| 71 | + MountCreationService $creationService, |
| 72 | + MountDeletionService $deletionService, |
| 73 | + MountUpdateService $updateService |
| 74 | + ) { |
| 75 | + $this->alert = $alert; |
| 76 | + $this->nestRepository = $nestRepository; |
| 77 | + $this->locationRepository = $locationRepository; |
| 78 | + $this->repository = $repository; |
| 79 | + $this->creationService = $creationService; |
| 80 | + $this->deletionService = $deletionService; |
| 81 | + $this->updateService = $updateService; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Return the mount overview page. |
| 86 | + * |
| 87 | + * @return \Illuminate\View\View |
| 88 | + */ |
| 89 | + public function index() |
| 90 | + { |
| 91 | + return view('admin.mounts.index', [ |
| 92 | + 'mounts' => $this->repository->getAllWithDetails(), |
| 93 | + ]); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Return the mount view page. |
| 98 | + * |
| 99 | + * @param string $id |
| 100 | + * @return \Illuminate\View\View |
| 101 | + * |
| 102 | + * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException |
| 103 | + */ |
| 104 | + public function view($id) |
| 105 | + { |
| 106 | + $nests = $this->nestRepository->all(); |
| 107 | + $nests->load('eggs'); |
| 108 | + |
| 109 | + $locations = $this->locationRepository->all(); |
| 110 | + $locations->load('nodes'); |
| 111 | + |
| 112 | + return view('admin.mounts.view', [ |
| 113 | + 'mount' => $this->repository->getWithRelations($id), |
| 114 | + 'nests' => $nests, |
| 115 | + 'locations' => $locations, |
| 116 | + ]); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Handle request to create new mount. |
| 121 | + * |
| 122 | + * @param \Pterodactyl\Http\Requests\Admin\MountFormRequest $request |
| 123 | + * @return \Illuminate\Http\RedirectResponse |
| 124 | + * |
| 125 | + * @throws \Throwable |
| 126 | + */ |
| 127 | + public function create(MountFormRequest $request) |
| 128 | + { |
| 129 | + $mount = $this->creationService->handle($request->normalize()); |
| 130 | + $this->alert->success('Mount was created successfully.')->flash(); |
| 131 | + |
| 132 | + return redirect()->route('admin.mounts.view', $mount->id); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Handle request to update or delete location. |
| 137 | + * |
| 138 | + * @param \Pterodactyl\Http\Requests\Admin\MountFormRequest $request |
| 139 | + * @param \Pterodactyl\Models\Mount $mount |
| 140 | + * @return \Illuminate\Http\RedirectResponse |
| 141 | + * |
| 142 | + * @throws \Throwable |
| 143 | + */ |
| 144 | + public function update(MountFormRequest $request, Mount $mount) |
| 145 | + { |
| 146 | + if ($request->input('action') === 'delete') { |
| 147 | + return $this->delete($mount); |
| 148 | + } |
| 149 | + |
| 150 | + $this->updateService->handle($mount->id, $request->normalize()); |
| 151 | + $this->alert->success('Mount was updated successfully.')->flash(); |
| 152 | + |
| 153 | + return redirect()->route('admin.mounts.view', $mount->id); |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Delete a location from the system. |
| 158 | + * |
| 159 | + * @param \Pterodactyl\Models\Mount $mount |
| 160 | + * @return \Illuminate\Http\RedirectResponse |
| 161 | + * |
| 162 | + * @throws \Exception |
| 163 | + */ |
| 164 | + public function delete(Mount $mount) |
| 165 | + { |
| 166 | + try { |
| 167 | + $this->deletionService->handle($mount->id); |
| 168 | + |
| 169 | + return redirect()->route('admin.mounts'); |
| 170 | + } catch (DisplayException $ex) { |
| 171 | + $this->alert->danger($ex->getMessage())->flash(); |
| 172 | + } |
| 173 | + |
| 174 | + return redirect()->route('admin.mounts.view', $mount->id); |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * Adds eggs to the mount's many to many relation. |
| 179 | + * |
| 180 | + * @param \Illuminate\Http\Request $request |
| 181 | + * @param \Pterodactyl\Models\Mount $mount |
| 182 | + * @return \Illuminate\Http\RedirectResponse |
| 183 | + */ |
| 184 | + public function addEggs(Request $request, Mount $mount) |
| 185 | + { |
| 186 | + $validatedData = $request->validate([ |
| 187 | + 'eggs' => 'required|exists:eggs,id', |
| 188 | + ]); |
| 189 | + |
| 190 | + $eggs = $validatedData['eggs'] ?? []; |
| 191 | + if (sizeof($eggs) > 0) { |
| 192 | + $mount->eggs()->attach(array_map('intval', $eggs)); |
| 193 | + $this->alert->success('Mount was updated successfully.')->flash(); |
| 194 | + } |
| 195 | + |
| 196 | + return redirect()->route('admin.mounts.view', $mount->id); |
| 197 | + } |
| 198 | + |
| 199 | + /** |
| 200 | + * Adds nodes to the mount's many to many relation. |
| 201 | + * |
| 202 | + * @param \Illuminate\Http\Request $request |
| 203 | + * @param \Pterodactyl\Models\Mount $mount |
| 204 | + * @return \Illuminate\Http\RedirectResponse |
| 205 | + */ |
| 206 | + public function addNodes(Request $request, Mount $mount) |
| 207 | + { |
| 208 | + $validatedData = $request->validate([ |
| 209 | + 'nodes' => 'required|exists:nodes,id', |
| 210 | + ]); |
| 211 | + |
| 212 | + $nodes = $validatedData['nodes'] ?? []; |
| 213 | + if (sizeof($nodes) > 0) { |
| 214 | + $mount->nodes()->attach(array_map('intval', $nodes)); |
| 215 | + $this->alert->success('Mount was updated successfully.')->flash(); |
| 216 | + } |
| 217 | + |
| 218 | + return redirect()->route('admin.mounts.view', $mount->id); |
| 219 | + } |
| 220 | + |
| 221 | + /** |
| 222 | + * Deletes an egg from the mount's many to many relation. |
| 223 | + * |
| 224 | + * @param \Pterodactyl\Models\Mount $mount |
| 225 | + * @param int $egg_id |
| 226 | + * @return \Illuminate\Http\Response |
| 227 | + */ |
| 228 | + public function deleteEgg(Mount $mount, int $egg_id) |
| 229 | + { |
| 230 | + $mount->eggs()->detach($egg_id); |
| 231 | + |
| 232 | + return response('', 204); |
| 233 | + } |
| 234 | + |
| 235 | + /** |
| 236 | + * Deletes an node from the mount's many to many relation. |
| 237 | + * |
| 238 | + * @param \Pterodactyl\Models\Mount $mount |
| 239 | + * @param int $node_id |
| 240 | + * @return \Illuminate\Http\Response |
| 241 | + */ |
| 242 | + public function deleteNode(Mount $mount, int $node_id) |
| 243 | + { |
| 244 | + $mount->nodes()->detach($node_id); |
| 245 | + |
| 246 | + return response('', 204); |
| 247 | + } |
| 248 | +} |
0 commit comments