misc_pterodactyl-panel/app/Http/Controllers/Admin/MountController.php

225 lines
6.3 KiB
PHP
Raw Normal View History

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;
use Illuminate\Http\Request;
2020-10-03 19:42:27 +00:00
use Pterodactyl\Models\Nest;
use Pterodactyl\Models\Mount;
2020-10-03 19:42:27 +00:00
use Pterodactyl\Models\Location;
2020-05-21 00:55:59 +00:00
use Prologue\Alerts\AlertsMessageBag;
2020-05-20 23:29:03 +00:00
use Pterodactyl\Http\Controllers\Controller;
use Pterodactyl\Services\Mounts\MountUpdateService;
2020-05-21 19:19:59 +00:00
use Pterodactyl\Http\Requests\Admin\MountFormRequest;
2020-05-21 00:55:59 +00:00
use Pterodactyl\Services\Mounts\MountCreationService;
use Pterodactyl\Services\Mounts\MountDeletionService;
2020-05-21 00:57:30 +00:00
use Pterodactyl\Repositories\Eloquent\MountRepository;
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:55:59 +00:00
/**
* @var \Prologue\Alerts\AlertsMessageBag
*/
protected $alert;
/**
* @var \Pterodactyl\Contracts\Repository\NestRepositoryInterface
*/
protected $nestRepository;
/**
* @var \Pterodactyl\Contracts\Repository\LocationRepositoryInterface
*/
protected $locationRepository;
2020-05-20 23:29:03 +00:00
/**
* @var \Pterodactyl\Repositories\Eloquent\MountRepository
2020-05-20 23:29:03 +00:00
*/
protected $repository;
/**
* MountController constructor.
2020-05-20 23:29:03 +00:00
*
2020-05-21 00:55:59 +00:00
* @param \Prologue\Alerts\AlertsMessageBag $alert
* @param \Pterodactyl\Contracts\Repository\NestRepositoryInterface $nestRepository
* @param \Pterodactyl\Contracts\Repository\LocationRepositoryInterface $locationRepository
* @param \Pterodactyl\Repositories\Eloquent\MountRepository $repository
2020-05-20 23:29:03 +00:00
*/
public function __construct(
2020-05-21 00:55:59 +00:00
AlertsMessageBag $alert,
NestRepositoryInterface $nestRepository,
LocationRepositoryInterface $locationRepository,
2020-10-03 19:42:27 +00:00
MountRepository $repository
2020-05-20 23:29:03 +00:00
) {
2020-05-21 00:55:59 +00:00
$this->alert = $alert;
$this->nestRepository = $nestRepository;
$this->locationRepository = $locationRepository;
2020-05-20 23:29:03 +00:00
$this->repository = $repository;
}
/**
* Return the mount overview page.
*
* @return \Illuminate\View\View
*/
public function index()
{
return view('admin.mounts.index', [
'mounts' => $this->repository->getAllWithDetails(),
2020-05-20 23:29:03 +00:00
]);
}
2020-05-21 00:55:59 +00:00
/**
* Return the mount view page.
*
* @param string $id
* @return \Illuminate\View\View
*
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function view($id)
{
2020-10-03 19:42:27 +00:00
$nests = Nest::query()->with('eggs')->get();
$locations = Location::query()->with('nodes')->get();
return view('admin.mounts.view', [
'mount' => $this->repository->getWithRelations($id),
'nests' => $nests,
'locations' => $locations,
]);
}
2020-05-21 00:55:59 +00:00
/**
* Handle request to create new mount.
*
* @param \Pterodactyl\Http\Requests\Admin\MountFormRequest $request
* @return \Illuminate\Http\RedirectResponse
*
* @throws \Throwable
*/
public function create(MountFormRequest $request)
{
2020-10-03 19:42:27 +00:00
/** @var \Pterodactyl\Models\Mount $mount */
$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();
return redirect()->route('admin.mounts.view', $mount->id);
}
/**
* Handle request to update or delete location.
*
* @param \Pterodactyl\Http\Requests\Admin\MountFormRequest $request
* @param \Pterodactyl\Models\Mount $mount
* @return \Illuminate\Http\RedirectResponse
*
* @throws \Throwable
*/
public function update(MountFormRequest $request, Mount $mount)
{
if ($request->input('action') === 'delete') {
return $this->delete($mount);
}
2020-10-03 19:42:27 +00:00
$mount->forceFill($request->validated())->save();
$this->alert->success('Mount was updated successfully.')->flash();
return redirect()->route('admin.mounts.view', $mount->id);
}
/**
* Delete a location from the system.
*
* @param \Pterodactyl\Models\Mount $mount
* @return \Illuminate\Http\RedirectResponse
*
* @throws \Exception
*/
public function delete(Mount $mount)
{
2020-10-03 19:42:27 +00:00
$mount->delete();
2020-10-03 19:42:27 +00:00
return redirect()->route('admin.mounts');
2020-05-21 00:55:59 +00:00
}
/**
* Adds eggs to the mount's many to many relation.
*
* @param \Illuminate\Http\Request $request
* @param \Pterodactyl\Models\Mount $mount
* @return \Illuminate\Http\RedirectResponse
*/
public function addEggs(Request $request, Mount $mount)
{
$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-10-03 19:42:27 +00:00
$this->alert->success('Mount was updated successfully.')->flash();
return redirect()->route('admin.mounts.view', $mount->id);
}
/**
* Adds nodes to the mount's many to many relation.
*
* @param \Illuminate\Http\Request $request
* @param \Pterodactyl\Models\Mount $mount
* @return \Illuminate\Http\RedirectResponse
*/
public function addNodes(Request $request, Mount $mount)
{
2020-10-03 19:42:27 +00:00
$data = $request->validate(['nodes' => 'required|exists:nodes,id']);
2020-10-03 19:42:27 +00:00
$nodes = $data['nodes'] ?? [];
if (count($nodes) > 0) {
$mount->nodes()->attach($nodes);
}
2020-10-03 19:42:27 +00:00
$this->alert->success('Mount was updated successfully.')->flash();
return redirect()->route('admin.mounts.view', $mount->id);
}
/**
* Deletes an egg from the mount's many to many relation.
*
* @param \Pterodactyl\Models\Mount $mount
* @param int $egg_id
* @return \Illuminate\Http\Response
*/
public function deleteEgg(Mount $mount, int $egg_id)
{
$mount->eggs()->detach($egg_id);
2020-05-21 19:22:32 +00:00
return response('', 204);
}
/**
* Deletes an node from the mount's many to many relation.
*
* @param \Pterodactyl\Models\Mount $mount
* @param int $node_id
* @return \Illuminate\Http\Response
*/
public function deleteNode(Mount $mount, int $node_id)
{
$mount->nodes()->detach($node_id);
2020-05-21 19:22:32 +00:00
return response('', 204);
}
2020-05-20 23:29:03 +00:00
}