Skip to content

Commit 644f26f

Browse files
committed
Add location creation
1 parent fb5533f commit 644f26f

File tree

4 files changed

+98
-4
lines changed

4 files changed

+98
-4
lines changed

app/Http/Controllers/Admin/LocationsController.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
namespace Pterodactyl\Http\Controllers\Admin;
44

55
use DB;
6+
use Alert;
67

78
use Pterodactyl\Models;
89
use Pterodactyl\Repositories\LocationRepository;
910
use Pterodactyl\Http\Controllers\Controller;
1011

1112
use Pterodactyl\Exceptions\DisplayValidationException;
13+
use Pterodactyl\Exceptions\DisplayException;
1214

1315
use Illuminate\Http\Request;
1416

@@ -73,7 +75,22 @@ public function patchLocation(Request $request, $id)
7375

7476
public function postLocation(Request $request)
7577
{
76-
//
78+
try {
79+
$location = new LocationRepository;
80+
$id = $location->create($request->except([
81+
'_token'
82+
]));
83+
Alert::success('New location successfully added.')->flash();
84+
return redirect()->route('admin.locations');
85+
} catch (DisplayValidationException $ex) {
86+
return redirect()->route('admin.locations')->withErrors(json_decode($ex->getMessage()))->withInput();
87+
} catch (DisplayException $ex) {
88+
Alert::danger($ex->getMessage())->flash();
89+
} catch (\Exception $ex) {
90+
Log::error($ex);
91+
Alert::danger('An unhandled exception occured while attempting to add this location. Please try again.')->flash();
92+
}
93+
return redirect()->route('admin.locations')->withInput();
7794
}
7895

7996
}

app/Models/Location.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,11 @@ class Location extends Model
1414
*/
1515
protected $table = 'locations';
1616

17+
/**
18+
* Fields that are not mass assignable.
19+
*
20+
* @var array
21+
*/
22+
protected $guarded = ['id', 'created_at', 'updated_at'];
23+
1724
}

app/Repositories/LocationRepository.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use Validator;
66

77
use Pterodactyl\Models;
8-
use Pterodactyl\Exceptions\DisplayException;
98
use Pterodactyl\Exceptions\DisplayValidationException;
109

1110
class LocationRepository
@@ -16,6 +15,42 @@ public function __construct()
1615
//
1716
}
1817

18+
/**
19+
* Creates a new location on the system.
20+
* @param array $data
21+
* @throws Pterodactyl\Exceptions\DisplayValidationException
22+
* @return integer
23+
*/
24+
public function create(array $data)
25+
{
26+
$validator = Validator::make($data, [
27+
'short' => 'required|regex:/^[a-z0-9_.-]{1,10}$/i|unique:locations,short',
28+
'long' => 'required|string|min:1|max:255'
29+
]);
30+
31+
// Run validator, throw catchable and displayable exception if it fails.
32+
// Exception includes a JSON result of failed validation rules.
33+
if ($validator->fails()) {
34+
throw new DisplayValidationException($validator->errors());
35+
}
36+
37+
$location = new Models\Location;
38+
$location->fill([
39+
'long' => $data['long'],
40+
'short' => $data['short']
41+
]);
42+
$location->save();
43+
44+
return $location->id;
45+
}
46+
47+
/**
48+
* Modifies a location based on the fields passed in $data.
49+
* @param integer $id
50+
* @param array $data
51+
* @throws Pterodactyl\Exceptions\DisplayValidationException
52+
* @return boolean
53+
*/
1954
public function edit($id, array $data)
2055
{
2156
$validator = Validator::make($data, [

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

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
<div class="well">
4242
<div class="row">
4343
<div class="col-md-12">
44-
<button class="btn btn-sm btn-success" id="addNewLocation">Add New Location</button>
44+
<button class="btn btn-sm btn-success" data-toggle="modal" data-target="#addModal">Add New Location</button>
4545
</div>
4646
</div>
4747
</div>
@@ -51,17 +51,19 @@
5151
<div class="modal-content">
5252
<div class="modal-header">
5353
<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>
54+
<h4 class="modal-title">Editing Location</h4>
5555
</div>
5656
<form action="{{ route('admin.locations') }}" method="POST" id="editLocationForm">
5757
<div class="modal-body">
5858
<div class="form-group">
5959
<label for="location-short" class="control-label">Location Code:</label>
6060
<input type="text" class="form-control" id="location-short">
61+
<p class="text-muted"><small>This should be a short identifier for this location (e.g. <code>ny1</code>). This field is limited to a maximum of 10 characters from the following list: <code>a-zA-Z0-9_-.</code></small></p>
6162
</div>
6263
<div class="form-group">
6364
<label for="location-long" class="control-label">Description:</label>
6465
<input type="text" class="form-control" id="location-long">
66+
<p class="text-muted"><small>This should be a longer description of the location for internal reference.</small></p>
6567
</div>
6668
</div>
6769
<div class="modal-footer">
@@ -73,6 +75,39 @@
7375
</div>
7476
</div>
7577
</div>
78+
<div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
79+
<div class="modal-dialog" role="document">
80+
<div class="modal-content">
81+
<div class="modal-header">
82+
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
83+
<h4 class="modal-title">Add New Location</h4>
84+
</div>
85+
<form action="{{ route('admin.locations') }}" method="POST" id="addLocationForm">
86+
<div class="modal-body">
87+
<div class="form-group">
88+
<label for="short" class="control-label">Location Code:</label>
89+
<div>
90+
<input type="text" class="form-control" name="short" value="{{ old('short') }}">
91+
<p class="text-muted"><small>This should be a short identifier for this location (e.g. <code>ny1</code>). This field is limited to a maximum of 10 characters from the following list: <code>a-zA-Z0-9_-.</code></small></p>
92+
</div>
93+
</div>
94+
<div class="form-group">
95+
<label for="long" class="control-label">Description:</label>
96+
<div>
97+
<input type="text" class="form-control" name="long" value="{{ old('long') }}">
98+
<p class="text-muted"><small>This should be a longer description of the location for internal reference.</small></p>
99+
</div>
100+
</div>
101+
</div>
102+
<div class="modal-footer">
103+
{!! csrf_field() !!}
104+
<button type="button" class="btn btn-sm btn-default" data-dismiss="modal">Close</button>
105+
<button type="submit" class="btn btn-sm btn-primary">Add Location</button>
106+
</div>
107+
</form>
108+
</div>
109+
</div>
110+
</div>
76111
<script>
77112
$(document).ready(function () {
78113
$('#sidebar_links').find("a[href='/admin/locations']").addClass('active');

0 commit comments

Comments
 (0)