Add ability to create new service variable.
This commit is contained in:
parent
b1b1f448e9
commit
f5a4ec981d
4 changed files with 104 additions and 13 deletions
|
@ -55,7 +55,7 @@ class OptionController extends Controller
|
|||
|
||||
/**
|
||||
* Handles POST request to create a new option.
|
||||
|
||||
*
|
||||
* @param Request $request
|
||||
* @return \Illuminate\Response\RedirectResponse
|
||||
*/
|
||||
|
@ -84,6 +84,36 @@ class OptionController extends Controller
|
|||
return redirect()->route('admin.services.option.new')->withInput();
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles POST request to create a new option variable.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param int $id The ID of the service option to assign this variable to.
|
||||
* @return \Illuminate\Response\RedirectResponse
|
||||
*/
|
||||
public function createVariable(Request $request, $id)
|
||||
{
|
||||
$repo = new VariableRepository;
|
||||
|
||||
try {
|
||||
$variable = $repo->create($id, $request->only([
|
||||
'name', 'description', 'env_variable',
|
||||
'default_value', 'options', 'rules',
|
||||
]));
|
||||
|
||||
Alert::success('New variable successfully assigned to this service option.')->flash();
|
||||
} catch (DisplayValidationException $ex) {
|
||||
return redirect()->route('admin.services.option.variables', $id)->withErrors(json_decode($ex->getMessage()));
|
||||
} catch (DisplayException $ex) {
|
||||
Alert::danger($ex->getMessage())->flash();
|
||||
} catch (\Exception $ex) {
|
||||
Log::error($ex);
|
||||
Alert::danger('An unhandled exception was encountered while attempting to process that request. This error has been logged.')->flash();
|
||||
}
|
||||
|
||||
return redirect()->route('admin.services.option.variables', $id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display option overview page.
|
||||
*
|
||||
|
|
|
@ -429,18 +429,19 @@ class AdminRoutes
|
|||
'uses' => 'Admin\OptionController@viewConfiguration',
|
||||
]);
|
||||
|
||||
$router->post('/option/{id}', 'Admin\OptionController@editConfiguration');
|
||||
|
||||
$router->get('/option/{id}/variables', [
|
||||
'as' => 'admin.services.option.variables',
|
||||
'uses' => 'Admin\OptionController@viewVariables',
|
||||
]);
|
||||
|
||||
$router->post('/option/{id}/variables', 'Admin\OptionController@createVariable');
|
||||
|
||||
$router->post('/option/{id}/variables/{variable}', [
|
||||
'as' => 'admin.services.option.variables.edit',
|
||||
'uses' => 'Admin\OptionController@editVariable',
|
||||
]);
|
||||
|
||||
$router->post('/option/{id}', 'Admin\OptionController@editConfiguration');
|
||||
|
||||
});
|
||||
|
||||
// Service Packs
|
||||
|
|
|
@ -26,21 +26,28 @@ namespace Pterodactyl\Repositories;
|
|||
|
||||
use DB;
|
||||
use Validator;
|
||||
use Pterodactyl\Models\ServiceOption;
|
||||
use Pterodactyl\Models\ServiceVariable;
|
||||
use Pterodactyl\Exceptions\DisplayException;
|
||||
use Pterodactyl\Exceptions\DisplayValidationException;
|
||||
|
||||
class VariableRepository
|
||||
{
|
||||
public function __construct()
|
||||
/**
|
||||
* Create a new service variable.
|
||||
*
|
||||
* @param int $option
|
||||
* @param array $data
|
||||
* @return \Pterodactyl\Models\ServiceVariable
|
||||
*
|
||||
* @throws \Pterodactyl\Exceptions\DisplayException
|
||||
* @throws \Pterodactyl\Exceptions\DisplayValidationException
|
||||
*/
|
||||
public function create($option, array $data)
|
||||
{
|
||||
//
|
||||
}
|
||||
$option = ServiceOption::select('id')->findOrFail($option);
|
||||
|
||||
public function create(array $data)
|
||||
{
|
||||
$validator = Validator::make($data, [
|
||||
'option_id' => 'required|numeric|exists:service_options,id',
|
||||
'name' => 'required|string|min:1|max:255',
|
||||
'description' => 'sometimes|nullable|string',
|
||||
'env_variable' => 'required|regex:/^[\w]{1,255}$/',
|
||||
|
@ -60,9 +67,7 @@ class VariableRepository
|
|||
}
|
||||
|
||||
if (isset($data['env_variable'])) {
|
||||
$search = ServiceVariable::where('env_variable', $data['env_variable'])
|
||||
->where('option_id', $variable->option_id)
|
||||
->where('id', '!=', $variable->id);
|
||||
$search = ServiceVariable::where('env_variable', $data['env_variable'])->where('option_id', $option->id);
|
||||
if ($search->first()) {
|
||||
throw new DisplayException('The envionment variable name assigned to this variable must be unique for this service option.');
|
||||
}
|
||||
|
@ -72,6 +77,7 @@ class VariableRepository
|
|||
$data['options'] = [];
|
||||
}
|
||||
|
||||
$data['option_id'] = $option->id;
|
||||
$data['user_viewable'] = (in_array('user_viewable', $data['options']));
|
||||
$data['user_editable'] = (in_array('user_editable', $data['options']));
|
||||
$data['required'] = (in_array('required', $data['options']));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue