PteroTheme/app/Http/Controllers/Admin/LocationController.php

105 lines
3.0 KiB
PHP

<?php
namespace Pterodactyl\Http\Controllers\Admin;
use Illuminate\View\View;
use Pterodactyl\Models\Location;
use Illuminate\Http\RedirectResponse;
use Prologue\Alerts\AlertsMessageBag;
use Illuminate\View\Factory as ViewFactory;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Http\Controllers\Controller;
use Pterodactyl\Http\Requests\Admin\LocationFormRequest;
use Pterodactyl\Services\Locations\LocationUpdateService;
use Pterodactyl\Services\Locations\LocationCreationService;
use Pterodactyl\Services\Locations\LocationDeletionService;
class LocationController extends Controller
{
/**
* LocationController constructor.
*/
public function __construct(
protected AlertsMessageBag $alert,
protected LocationCreationService $creationService,
protected LocationDeletionService $deletionService,
protected LocationUpdateService $updateService,
protected ViewFactory $view
) {
}
/**
* Return the location overview page.
*/
public function index(): View
{
$locations = Location::query()->withCount(['nodes', 'servers'])->get();
return $this->view->make('admin.locations.index', [
'locations' => $locations,
]);
}
/**
* Return the location view page.
*
*/
public function view(int $id): View
{
$location = Location::with('nodes.servers')->findOrFail($id);
return $this->view->make('admin.locations.view', [
'location' => $location,
]);
}
/**
* Handle request to create new location.
*
* @throws \Throwable
*/
public function create(LocationFormRequest $request): RedirectResponse
{
$location = $this->creationService->handle($request->normalize());
$this->alert->success('Location was created successfully.')->flash();
return redirect()->route('admin.locations.view', $location->id);
}
/**
* Handle request to update or delete location.
*
* @throws \Throwable
*/
public function update(LocationFormRequest $request, Location $location): RedirectResponse
{
if ($request->input('action') === 'delete') {
return $this->delete($location);
}
$this->updateService->handle($location->id, $request->normalize());
$this->alert->success('Location was updated successfully.')->flash();
return redirect()->route('admin.locations.view', $location->id);
}
/**
* Delete a location from the system.
*
* @throws \Exception
* @throws \Pterodactyl\Exceptions\DisplayException
*/
public function delete(Location $location): RedirectResponse
{
try {
$this->deletionService->handle($location->id);
return redirect()->route('admin.locations');
} catch (DisplayException $ex) {
$this->alert->danger($ex->getMessage())->flash();
}
return redirect()->route('admin.locations.view', $location->id);
}
}