Skip to content

Commit f5a4ec9

Browse files
committed
Add ability to create new service variable.
1 parent b1b1f44 commit f5a4ec9

File tree

4 files changed

+104
-13
lines changed

4 files changed

+104
-13
lines changed

app/Http/Controllers/Admin/OptionController.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function new(Request $request)
5555

5656
/**
5757
* Handles POST request to create a new option.
58-
58+
*
5959
* @param Request $request
6060
* @return \Illuminate\Response\RedirectResponse
6161
*/
@@ -84,6 +84,36 @@ public function create(Request $request)
8484
return redirect()->route('admin.services.option.new')->withInput();
8585
}
8686

87+
/**
88+
* Handles POST request to create a new option variable.
89+
*
90+
* @param Request $request
91+
* @param int $id The ID of the service option to assign this variable to.
92+
* @return \Illuminate\Response\RedirectResponse
93+
*/
94+
public function createVariable(Request $request, $id)
95+
{
96+
$repo = new VariableRepository;
97+
98+
try {
99+
$variable = $repo->create($id, $request->only([
100+
'name', 'description', 'env_variable',
101+
'default_value', 'options', 'rules',
102+
]));
103+
104+
Alert::success('New variable successfully assigned to this service option.')->flash();
105+
} catch (DisplayValidationException $ex) {
106+
return redirect()->route('admin.services.option.variables', $id)->withErrors(json_decode($ex->getMessage()));
107+
} catch (DisplayException $ex) {
108+
Alert::danger($ex->getMessage())->flash();
109+
} catch (\Exception $ex) {
110+
Log::error($ex);
111+
Alert::danger('An unhandled exception was encountered while attempting to process that request. This error has been logged.')->flash();
112+
}
113+
114+
return redirect()->route('admin.services.option.variables', $id);
115+
}
116+
87117
/**
88118
* Display option overview page.
89119
*

app/Http/Routes/AdminRoutes.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -429,18 +429,19 @@ public function map(Router $router)
429429
'uses' => 'Admin\OptionController@viewConfiguration',
430430
]);
431431

432+
$router->post('/option/{id}', 'Admin\OptionController@editConfiguration');
433+
432434
$router->get('/option/{id}/variables', [
433435
'as' => 'admin.services.option.variables',
434436
'uses' => 'Admin\OptionController@viewVariables',
435437
]);
436438

439+
$router->post('/option/{id}/variables', 'Admin\OptionController@createVariable');
440+
437441
$router->post('/option/{id}/variables/{variable}', [
438442
'as' => 'admin.services.option.variables.edit',
439443
'uses' => 'Admin\OptionController@editVariable',
440444
]);
441-
442-
$router->post('/option/{id}', 'Admin\OptionController@editConfiguration');
443-
444445
});
445446

446447
// Service Packs

app/Repositories/VariableRepository.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,28 @@
2626

2727
use DB;
2828
use Validator;
29+
use Pterodactyl\Models\ServiceOption;
2930
use Pterodactyl\Models\ServiceVariable;
3031
use Pterodactyl\Exceptions\DisplayException;
3132
use Pterodactyl\Exceptions\DisplayValidationException;
3233

3334
class VariableRepository
3435
{
35-
public function __construct()
36+
/**
37+
* Create a new service variable.
38+
*
39+
* @param int $option
40+
* @param array $data
41+
* @return \Pterodactyl\Models\ServiceVariable
42+
*
43+
* @throws \Pterodactyl\Exceptions\DisplayException
44+
* @throws \Pterodactyl\Exceptions\DisplayValidationException
45+
*/
46+
public function create($option, array $data)
3647
{
37-
//
38-
}
48+
$option = ServiceOption::select('id')->findOrFail($option);
3949

40-
public function create(array $data)
41-
{
4250
$validator = Validator::make($data, [
43-
'option_id' => 'required|numeric|exists:service_options,id',
4451
'name' => 'required|string|min:1|max:255',
4552
'description' => 'sometimes|nullable|string',
4653
'env_variable' => 'required|regex:/^[\w]{1,255}$/',
@@ -60,9 +67,7 @@ public function create(array $data)
6067
}
6168

6269
if (isset($data['env_variable'])) {
63-
$search = ServiceVariable::where('env_variable', $data['env_variable'])
64-
->where('option_id', $variable->option_id)
65-
->where('id', '!=', $variable->id);
70+
$search = ServiceVariable::where('env_variable', $data['env_variable'])->where('option_id', $option->id);
6671
if ($search->first()) {
6772
throw new DisplayException('The envionment variable name assigned to this variable must be unique for this service option.');
6873
}
@@ -72,6 +77,7 @@ public function create(array $data)
7277
$data['options'] = [];
7378
}
7479

80+
$data['option_id'] = $option->id;
7581
$data['user_viewable'] = (in_array('user_viewable', $data['options']));
7682
$data['user_editable'] = (in_array('user_editable', $data['options']));
7783
$data['required'] = (in_array('required', $data['options']));

resources/themes/pterodactyl/admin/services/options/variables.blade.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
<ul class="nav nav-tabs">
4242
<li><a href="{{ route('admin.services.option.view', $option->id) }}">Configuration</a></li>
4343
<li class="active"><a href="{{ route('admin.services.option.variables', $option->id) }}">Variables</a></li>
44+
<li class="tab-success"><a href="#modal" data-toggle="modal" data-target="#newVariableModal">New Variable</a></li>
4445
</ul>
4546
</div>
4647
</div>
@@ -99,6 +100,59 @@
99100
</div>
100101
@endforeach
101102
</div>
103+
<div class="modal fade" id="newVariableModal" tabindex="-1" role="dialog">
104+
<div class="modal-dialog" role="document">
105+
<div class="modal-content">
106+
<div class="modal-header">
107+
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
108+
<h4 class="modal-title">Create New Option Variable</h4>
109+
</div>
110+
<form action="{{ route('admin.services.option.variables', $option->id) }}" method="POST">
111+
<div class="modal-body">
112+
<div class="form-group">
113+
<label class="form-label">Name</label>
114+
<input type="text" name="name" class="form-control" />
115+
</div>
116+
<div class="form-group">
117+
<label class="form-label">Description</label>
118+
<textarea name="description" class="form-control" rows="3"></textarea>
119+
</div>
120+
<div class="row">
121+
<div class="form-group col-md-6">
122+
<label class="form-label">Environment Variable</label>
123+
<input type="text" name="env_variable" class="form-control" />
124+
</div>
125+
<div class="form-group col-md-6">
126+
<label class="form-label">Default Value</label>
127+
<input type="text" name="default_value" class="form-control" />
128+
</div>
129+
<div class="col-xs-12">
130+
<p class="text-muted small">This variable can be accessed in the statup command by entering <code>@{{environment variable value}}</code>.</p>
131+
</div>
132+
</div>
133+
<div class="form-group">
134+
<label class="form-label">Permissions</label>
135+
<select name="options[]" class="pOptions form-control" multiple>
136+
<option value="user_viewable">Users Can View</option>
137+
<option value="user_editable">Users Can Edit</option>
138+
<option value="required">Field Is Required</option>
139+
</select>
140+
</div>
141+
<div class="form-group">
142+
<label class="form-label">Input Rules</label>
143+
<input type="text" name="rules" class="form-control" placeholder="required|string|max:20" />
144+
<p class="text-muted small">These rules are defined using standard Laravel Framework validation rules.</p>
145+
</div>
146+
</div>
147+
<div class="modal-footer">
148+
{!! csrf_field() !!}
149+
<button type="button" class="btn btn-default pull-left" data-dismiss="modal">Close</button>
150+
<button type="submit" class="btn btn-primary">Create Variable</button>
151+
</div>
152+
</form>
153+
</div>
154+
</div>
155+
</div>
102156
@endsection
103157

104158
@section('footer-scripts')

0 commit comments

Comments
 (0)