Skip to content

Commit fb5533f

Browse files
committed
add location editing
1 parent 21a95a5 commit fb5533f

File tree

4 files changed

+146
-2
lines changed

4 files changed

+146
-2
lines changed

app/Http/Controllers/Admin/LocationsController.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
use DB;
66

77
use Pterodactyl\Models;
8+
use Pterodactyl\Repositories\LocationRepository;
89
use Pterodactyl\Http\Controllers\Controller;
10+
11+
use Pterodactyl\Exceptions\DisplayValidationException;
12+
913
use Illuminate\Http\Request;
1014

1115
class LocationsController extends Controller
@@ -51,4 +55,25 @@ public function deleteLocation(Request $request, $id)
5155
return response('', 204);
5256
}
5357

58+
public function patchLocation(Request $request, $id)
59+
{
60+
try {
61+
$location = new LocationRepository;
62+
$location->edit($id, $request->all());
63+
return response('', 204);
64+
} catch (DisplayValidationException $ex) {
65+
return response()->json([
66+
'error' => 'There was a validation error while processing this request. Location descriptions must be between 1 and 255 characters, and the location code must be between 1 and 10 characters with no spaces or special characters.'
67+
], 422);
68+
} catch (\Exception $ex) {
69+
// This gets caught and processed into JSON anyways.
70+
throw $ex;
71+
}
72+
}
73+
74+
public function postLocation(Request $request)
75+
{
76+
//
77+
}
78+
5479
}

app/Http/Routes/AdminRoutes.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,12 @@ public function map(Router $router) {
219219
$router->delete('/{id}', [
220220
'uses' => 'Admin\LocationsController@deleteLocation'
221221
]);
222+
$router->patch('/{id}', [
223+
'uses' => 'Admin\LocationsController@patchLocation'
224+
]);
225+
$router->post('/', [
226+
'uses' => 'Admin\LocationsController@postLocation'
227+
]);
222228
});
223229

224230
// API Routes
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Pterodactyl\Repositories;
4+
5+
use Validator;
6+
7+
use Pterodactyl\Models;
8+
use Pterodactyl\Exceptions\DisplayException;
9+
use Pterodactyl\Exceptions\DisplayValidationException;
10+
11+
class LocationRepository
12+
{
13+
14+
public function __construct()
15+
{
16+
//
17+
}
18+
19+
public function edit($id, array $data)
20+
{
21+
$validator = Validator::make($data, [
22+
'short' => 'regex:/^[a-z0-9_.-]{1,10}$/i',
23+
'long' => 'string|min:1|max:255'
24+
]);
25+
26+
// Run validator, throw catchable and displayable exception if it fails.
27+
// Exception includes a JSON result of failed validation rules.
28+
if ($validator->fails()) {
29+
throw new DisplayValidationException($validator->errors());
30+
}
31+
32+
$location = Models\Location::findOrFail($id);
33+
34+
if (isset($data['short'])) {
35+
$location->short = $data['short'];
36+
}
37+
38+
if (isset($data['long'])) {
39+
$location->long = $data['long'];
40+
}
41+
42+
return $location->save();
43+
}
44+
}

resources/views/admin/locations/index.blade.php

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
<td>{{ $location->long }}</td>
3030
<td class="text-center">{{ $location->a_nodeCount }}</td>
3131
<td class="text-center">{{ $location->a_serverCount }}</td>
32-
<td class="text-center"><a href="#edit"><i class="fa fa-wrench" data-action="edit" data-id="{{ $location->id }}" data-short="{{ $location->short }}" data-long="{{ $location->long }}"></i></a></td>
32+
<td class="text-center"><a href="#edit"><i class="fa fa-wrench" data-toggle="modal" data-target="#editModal" data-action="edit" data-id="{{ $location->id }}" data-short="{{ $location->short }}" data-long="{{ $location->long }}"></i></a></td>
3333
<td class="text-center"><a href="#delete" class="text-danger" data-action="delete" data-id="{{ $location->id }}"><i class="fa fa-trash-o"></i></a></td>
3434
</tr>
3535
@endforeach
@@ -46,9 +46,78 @@
4646
</div>
4747
</div>
4848
</div>
49+
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
50+
<div class="modal-dialog" role="document">
51+
<div class="modal-content">
52+
<div class="modal-header">
53+
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
54+
<h4 class="modal-title" id="exampleModalLabel">Editing Location</h4>
55+
</div>
56+
<form action="{{ route('admin.locations') }}" method="POST" id="editLocationForm">
57+
<div class="modal-body">
58+
<div class="form-group">
59+
<label for="location-short" class="control-label">Location Code:</label>
60+
<input type="text" class="form-control" id="location-short">
61+
</div>
62+
<div class="form-group">
63+
<label for="location-long" class="control-label">Description:</label>
64+
<input type="text" class="form-control" id="location-long">
65+
</div>
66+
</div>
67+
<div class="modal-footer">
68+
<input type="hidden" id="location-id">
69+
<button type="button" class="btn btn-sm btn-default" data-dismiss="modal">Close</button>
70+
<button type="submit" class="btn btn-sm btn-primary">Edit Location</button>
71+
</div>
72+
</form>
73+
</div>
74+
</div>
75+
</div>
4976
<script>
5077
$(document).ready(function () {
5178
$('#sidebar_links').find("a[href='/admin/locations']").addClass('active');
79+
$('#editModal').on('show.bs.modal', function (event) {
80+
var button = $(event.relatedTarget);
81+
var short = button.data('short');
82+
var long = button.data('long');
83+
var id = button.data('id');
84+
var modal = $(this);
85+
86+
modal.find('#location-id').val(id);
87+
modal.find('#location-short').val(short);
88+
modal.find('#location-long').val(long);
89+
});
90+
$('#editLocationForm').submit(function (event) {
91+
event.preventDefault();
92+
$.ajax({
93+
method: 'PATCH',
94+
url: '{{ route('admin.locations') }}/' + $('#location-id').val(),
95+
headers: {
96+
'X-CSRF-TOKEN': '{{ csrf_token() }}'
97+
},
98+
data: {
99+
short: $('#location-short').val(),
100+
long: $('#location-long').val()
101+
}
102+
}).done(function (data) {
103+
swal({
104+
type: 'success',
105+
title: '',
106+
text: 'Successfully updated location information.',
107+
closeOnConfirm: false,
108+
showLoaderOnConfirm: true
109+
}, function () {
110+
window.location = '{{ route('admin.locations') }}';
111+
});
112+
}).fail(function (jqXHR) {
113+
console.error(jqXHR);
114+
swal({
115+
type: 'error',
116+
title: 'Whoops!',
117+
text: (typeof jqXHR.responseJSON.error !== 'undefined') ? jqXHR.responseJSON.error : 'An error occured while processing this request.'
118+
});
119+
});
120+
});
52121
$('[data-action="delete"]').click(function (event) {
53122
event.preventDefault();
54123
var self = $(this);
@@ -78,7 +147,7 @@
78147
swal({
79148
type: 'error',
80149
title: 'Whoops!',
81-
text: (typeof jqXHR.responseJSON !== 'undefined') ? jqXHR.responseJSON.error : 'An error occured while processing this request.'
150+
text: (typeof jqXHR.responseJSON.error !== 'undefined') ? jqXHR.responseJSON.error : 'An error occured while processing this request.'
82151
});
83152
});
84153
});

0 commit comments

Comments
 (0)