diff --git a/app/Http/Controllers/Admin/OptionController.php b/app/Http/Controllers/Admin/OptionController.php index b60c0df7b..837e5a8aa 100644 --- a/app/Http/Controllers/Admin/OptionController.php +++ b/app/Http/Controllers/Admin/OptionController.php @@ -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. * diff --git a/app/Http/Routes/AdminRoutes.php b/app/Http/Routes/AdminRoutes.php index 60a6cb8ed..634568728 100644 --- a/app/Http/Routes/AdminRoutes.php +++ b/app/Http/Routes/AdminRoutes.php @@ -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 diff --git a/app/Repositories/VariableRepository.php b/app/Repositories/VariableRepository.php index b9421897b..91490b4b1 100644 --- a/app/Repositories/VariableRepository.php +++ b/app/Repositories/VariableRepository.php @@ -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'])); diff --git a/resources/themes/pterodactyl/admin/services/options/variables.blade.php b/resources/themes/pterodactyl/admin/services/options/variables.blade.php index c1870d3ee..c763e6cee 100644 --- a/resources/themes/pterodactyl/admin/services/options/variables.blade.php +++ b/resources/themes/pterodactyl/admin/services/options/variables.blade.php @@ -41,6 +41,7 @@
@@ -99,6 +100,59 @@ @endforeach +