2020-05-20 23:29:03 +00:00
|
|
|
<?php
|
|
|
|
|
2020-05-21 00:55:59 +00:00
|
|
|
namespace Pterodactyl\Http\Controllers\Admin;
|
2020-05-20 23:29:03 +00:00
|
|
|
|
2020-10-03 19:42:27 +00:00
|
|
|
use Ramsey\Uuid\Uuid;
|
2022-10-14 16:59:20 +00:00
|
|
|
use Illuminate\View\View;
|
2020-05-21 02:50:56 +00:00
|
|
|
use Illuminate\Http\Request;
|
2020-10-03 19:42:27 +00:00
|
|
|
use Pterodactyl\Models\Nest;
|
2022-10-14 16:59:20 +00:00
|
|
|
use Illuminate\Http\Response;
|
2020-05-21 01:35:30 +00:00
|
|
|
use Pterodactyl\Models\Mount;
|
2020-10-03 19:42:27 +00:00
|
|
|
use Pterodactyl\Models\Location;
|
2022-10-14 16:59:20 +00:00
|
|
|
use Illuminate\Http\RedirectResponse;
|
2020-05-21 00:55:59 +00:00
|
|
|
use Prologue\Alerts\AlertsMessageBag;
|
2022-10-14 16:59:20 +00:00
|
|
|
use Illuminate\View\Factory as ViewFactory;
|
2020-05-20 23:29:03 +00:00
|
|
|
use Pterodactyl\Http\Controllers\Controller;
|
2020-05-21 19:19:59 +00:00
|
|
|
use Pterodactyl\Http\Requests\Admin\MountFormRequest;
|
2020-05-21 00:57:30 +00:00
|
|
|
use Pterodactyl\Repositories\Eloquent\MountRepository;
|
2020-05-21 02:50:56 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\NestRepositoryInterface;
|
|
|
|
use Pterodactyl\Contracts\Repository\LocationRepositoryInterface;
|
2020-05-20 23:29:03 +00:00
|
|
|
|
|
|
|
class MountController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
2020-05-21 00:00:53 +00:00
|
|
|
* MountController constructor.
|
2020-05-20 23:29:03 +00:00
|
|
|
*/
|
|
|
|
public function __construct(
|
2022-10-14 16:59:20 +00:00
|
|
|
protected AlertsMessageBag $alert,
|
|
|
|
protected NestRepositoryInterface $nestRepository,
|
|
|
|
protected LocationRepositoryInterface $locationRepository,
|
|
|
|
protected MountRepository $repository,
|
|
|
|
protected ViewFactory $view
|
2020-05-20 23:29:03 +00:00
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the mount overview page.
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function index(): View
|
2020-05-20 23:29:03 +00:00
|
|
|
{
|
2022-11-28 16:56:03 +00:00
|
|
|
return view('admin.mounts.index', [
|
2020-05-21 00:00:53 +00:00
|
|
|
'mounts' => $this->repository->getAllWithDetails(),
|
2020-05-20 23:29:03 +00:00
|
|
|
]);
|
|
|
|
}
|
2020-05-21 00:55:59 +00:00
|
|
|
|
2020-05-21 01:11:20 +00:00
|
|
|
/**
|
|
|
|
* Return the mount view page.
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function view(string $id): View
|
2020-05-21 01:11:20 +00:00
|
|
|
{
|
2020-10-03 19:42:27 +00:00
|
|
|
$nests = Nest::query()->with('eggs')->get();
|
|
|
|
$locations = Location::query()->with('nodes')->get();
|
2020-05-21 02:50:56 +00:00
|
|
|
|
2022-11-28 16:56:03 +00:00
|
|
|
return view('admin.mounts.view', [
|
2020-05-21 01:11:20 +00:00
|
|
|
'mount' => $this->repository->getWithRelations($id),
|
2020-05-21 02:50:56 +00:00
|
|
|
'nests' => $nests,
|
|
|
|
'locations' => $locations,
|
2020-05-21 01:11:20 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2020-05-21 00:55:59 +00:00
|
|
|
/**
|
|
|
|
* Handle request to create new mount.
|
|
|
|
*
|
|
|
|
* @throws \Throwable
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function create(MountFormRequest $request): RedirectResponse
|
2020-05-21 00:55:59 +00:00
|
|
|
{
|
2020-10-11 00:11:27 +00:00
|
|
|
$model = (new Mount())->fill($request->validated());
|
|
|
|
$model->forceFill(['uuid' => Uuid::uuid4()->toString()]);
|
|
|
|
|
|
|
|
$model->saveOrFail();
|
|
|
|
$mount = $model->fresh();
|
2020-10-03 19:42:27 +00:00
|
|
|
|
2020-05-21 00:55:59 +00:00
|
|
|
$this->alert->success('Mount was created successfully.')->flash();
|
|
|
|
|
2020-05-21 01:35:30 +00:00
|
|
|
return redirect()->route('admin.mounts.view', $mount->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle request to update or delete location.
|
|
|
|
*
|
|
|
|
* @throws \Throwable
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function update(MountFormRequest $request, Mount $mount): RedirectResponse
|
2020-05-21 01:35:30 +00:00
|
|
|
{
|
|
|
|
if ($request->input('action') === 'delete') {
|
|
|
|
return $this->delete($mount);
|
|
|
|
}
|
|
|
|
|
2020-10-17 20:42:08 +00:00
|
|
|
$mount->forceFill($request->validated())->save();
|
2020-10-03 19:42:27 +00:00
|
|
|
|
2020-05-21 01:35:30 +00:00
|
|
|
$this->alert->success('Mount was updated successfully.')->flash();
|
|
|
|
|
|
|
|
return redirect()->route('admin.mounts.view', $mount->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a location from the system.
|
|
|
|
*
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function delete(Mount $mount): RedirectResponse
|
2020-05-21 01:35:30 +00:00
|
|
|
{
|
2020-10-03 19:42:27 +00:00
|
|
|
$mount->delete();
|
2020-05-21 01:35:30 +00:00
|
|
|
|
2020-10-03 19:42:27 +00:00
|
|
|
return redirect()->route('admin.mounts');
|
2020-05-21 00:55:59 +00:00
|
|
|
}
|
2020-05-21 02:50:56 +00:00
|
|
|
|
|
|
|
/**
|
2022-10-14 16:59:20 +00:00
|
|
|
* Adds eggs to the mount's many-to-many relation.
|
2020-05-21 02:50:56 +00:00
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function addEggs(Request $request, Mount $mount): RedirectResponse
|
2020-05-21 02:50:56 +00:00
|
|
|
{
|
|
|
|
$validatedData = $request->validate([
|
|
|
|
'eggs' => 'required|exists:eggs,id',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$eggs = $validatedData['eggs'] ?? [];
|
2020-10-03 19:42:27 +00:00
|
|
|
if (count($eggs) > 0) {
|
|
|
|
$mount->eggs()->attach($eggs);
|
2020-05-21 02:50:56 +00:00
|
|
|
}
|
|
|
|
|
2020-10-03 19:42:27 +00:00
|
|
|
$this->alert->success('Mount was updated successfully.')->flash();
|
|
|
|
|
2020-05-21 02:50:56 +00:00
|
|
|
return redirect()->route('admin.mounts.view', $mount->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-10-14 16:59:20 +00:00
|
|
|
* Adds nodes to the mount's many-to-many relation.
|
2020-05-21 02:50:56 +00:00
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function addNodes(Request $request, Mount $mount): RedirectResponse
|
2020-05-21 02:50:56 +00:00
|
|
|
{
|
2020-10-03 19:42:27 +00:00
|
|
|
$data = $request->validate(['nodes' => 'required|exists:nodes,id']);
|
2020-05-21 02:50:56 +00:00
|
|
|
|
2020-10-03 19:42:27 +00:00
|
|
|
$nodes = $data['nodes'] ?? [];
|
|
|
|
if (count($nodes) > 0) {
|
|
|
|
$mount->nodes()->attach($nodes);
|
2020-05-21 02:50:56 +00:00
|
|
|
}
|
|
|
|
|
2020-10-03 19:42:27 +00:00
|
|
|
$this->alert->success('Mount was updated successfully.')->flash();
|
|
|
|
|
2020-05-21 02:50:56 +00:00
|
|
|
return redirect()->route('admin.mounts.view', $mount->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-10-14 16:59:20 +00:00
|
|
|
* Deletes an egg from the mount's many-to-many relation.
|
2020-05-21 02:50:56 +00:00
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function deleteEgg(Mount $mount, int $egg_id): Response
|
2020-05-21 02:50:56 +00:00
|
|
|
{
|
|
|
|
$mount->eggs()->detach($egg_id);
|
2020-05-21 19:22:32 +00:00
|
|
|
|
2020-05-21 02:50:56 +00:00
|
|
|
return response('', 204);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-10-14 16:59:20 +00:00
|
|
|
* Deletes a node from the mount's many-to-many relation.
|
2020-05-21 02:50:56 +00:00
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function deleteNode(Mount $mount, int $node_id): Response
|
2020-05-21 02:50:56 +00:00
|
|
|
{
|
|
|
|
$mount->nodes()->detach($node_id);
|
2020-05-21 19:22:32 +00:00
|
|
|
|
2020-05-21 02:50:56 +00:00
|
|
|
return response('', 204);
|
|
|
|
}
|
2020-05-20 23:29:03 +00:00
|
|
|
}
|