Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Http/Controllers/Admin/LocationController.php
10279 views
1
<?php
2
3
namespace Pterodactyl\Http\Controllers\Admin;
4
5
use Illuminate\View\View;
6
use Pterodactyl\Models\Location;
7
use Illuminate\Http\RedirectResponse;
8
use Prologue\Alerts\AlertsMessageBag;
9
use Illuminate\View\Factory as ViewFactory;
10
use Pterodactyl\Exceptions\DisplayException;
11
use Pterodactyl\Http\Controllers\Controller;
12
use Pterodactyl\Http\Requests\Admin\LocationFormRequest;
13
use Pterodactyl\Services\Locations\LocationUpdateService;
14
use Pterodactyl\Services\Locations\LocationCreationService;
15
use Pterodactyl\Services\Locations\LocationDeletionService;
16
use Pterodactyl\Contracts\Repository\LocationRepositoryInterface;
17
18
class LocationController extends Controller
19
{
20
/**
21
* LocationController constructor.
22
*/
23
public function __construct(
24
protected AlertsMessageBag $alert,
25
protected LocationCreationService $creationService,
26
protected LocationDeletionService $deletionService,
27
protected LocationRepositoryInterface $repository,
28
protected LocationUpdateService $updateService,
29
protected ViewFactory $view,
30
) {
31
}
32
33
/**
34
* Return the location overview page.
35
*/
36
public function index(): View
37
{
38
return view('admin.locations.index', [
39
'locations' => $this->repository->getAllWithDetails(),
40
]);
41
}
42
43
/**
44
* Return the location view page.
45
*
46
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
47
*/
48
public function view(int $id): View
49
{
50
return view('admin.locations.view', [
51
'location' => $this->repository->getWithNodes($id),
52
]);
53
}
54
55
/**
56
* Handle request to create new location.
57
*
58
* @throws \Throwable
59
*/
60
public function create(LocationFormRequest $request): RedirectResponse
61
{
62
$location = $this->creationService->handle($request->normalize());
63
$this->alert->success('Location was created successfully.')->flash();
64
65
return redirect()->route('admin.locations.view', $location->id);
66
}
67
68
/**
69
* Handle request to update or delete location.
70
*
71
* @throws \Throwable
72
*/
73
public function update(LocationFormRequest $request, Location $location): RedirectResponse
74
{
75
if ($request->input('action') === 'delete') {
76
return $this->delete($location);
77
}
78
79
$this->updateService->handle($location->id, $request->normalize());
80
$this->alert->success('Location was updated successfully.')->flash();
81
82
return redirect()->route('admin.locations.view', $location->id);
83
}
84
85
/**
86
* Delete a location from the system.
87
*
88
* @throws \Exception
89
* @throws DisplayException
90
*/
91
public function delete(Location $location): RedirectResponse
92
{
93
try {
94
$this->deletionService->handle($location->id);
95
96
return redirect()->route('admin.locations');
97
} catch (DisplayException $ex) {
98
$this->alert->danger($ex->getMessage())->flash();
99
}
100
101
return redirect()->route('admin.locations.view', $location->id);
102
}
103
}
104
105