diff --git a/app/Http/Controllers/Admin/ServiceController.php b/app/Http/Controllers/Admin/ServiceController.php index 325c1987d..c61919220 100644 --- a/app/Http/Controllers/Admin/ServiceController.php +++ b/app/Http/Controllers/Admin/ServiceController.php @@ -202,6 +202,34 @@ class ServiceController extends Controller return redirect()->route('admin.services.option', [$service, $option])->withInput(); } + public function getNewVariable(Request $request, $service, $option) + { + return view('admin.services.options.variable', [ + 'service' => Models\Service::findOrFail($service), + 'option' => Models\ServiceOptions::where('parent_service', $service)->where('id', $option)->firstOrFail() + ]); + } + + public function postNewVariable(Request $request, $service, $option) + { + try { + $repo = new ServiceRepository\Variable; + $repo->create($option, $request->except([ + '_token' + ])); + Alert::success('Successfully added new variable to this option.')->flash(); + return redirect()->route('admin.services.option', [$service, $option])->withInput(); + } catch (DisplayValidationException $ex) { + return redirect()->route('admin.services.option.variable.new', [$service, $option])->withErrors(json_decode($ex->getMessage()))->withInput(); + } catch (DisplayException $ex) { + Alert::danger($ex->getMessage())->flash(); + } catch (\Exception $ex) { + Log::error($ex); + Alert::danger('An error occurred while attempting to add this variable.')->flash(); + } + return redirect()->route('admin.services.option.variable.new', [$service, $option])->withInput(); + } + public function newOption(Request $request, $service) { return view('admin.services.options.new', [ @@ -227,4 +255,19 @@ class ServiceController extends Controller return redirect()->route('admin.services.option.new', $service)->withInput(); } + public function deleteVariable(Request $request, $service, $option, $variable) + { + try { + $repo = new ServiceRepository\Variable; + $repo->delete($variable); + Alert::success('Deleted variable.')->flash(); + } catch (DisplayException $ex) { + Alert::danger($ex->getMessage())->flash(); + } catch (\Exception $ex) { + Log::error($ex); + Alert::danger('An error occured while attempting to delete that variable.')->flash(); + } + return redirect()->route('admin.services.option', [$service, $option]); + } + } diff --git a/app/Http/Routes/AdminRoutes.php b/app/Http/Routes/AdminRoutes.php index 7bea4118a..c4d8eb074 100644 --- a/app/Http/Routes/AdminRoutes.php +++ b/app/Http/Routes/AdminRoutes.php @@ -392,10 +392,24 @@ class AdminRoutes { 'uses' => 'Admin\ServiceController@deleteOption' ]); + $router->get('/service/{service}/option/{option}/variable/new', [ + 'as' => 'admin.services.option.variable.new', + 'uses' => 'Admin\ServiceController@getNewVariable' + ]); + + $router->post('/service/{service}/option/{option}/variable/new', [ + 'uses' => 'Admin\ServiceController@postNewVariable' + ]); + $router->post('/service/{service}/option/{option}/variable/{variable}', [ 'as' => 'admin.services.option.variable', 'uses' => 'Admin\ServiceController@postOptionVariable' ]); + + $router->get('/service/{service}/option/{option}/variable/{variable}/delete', [ + 'as' => 'admin.services.option.variable.delete', + 'uses' => 'Admin\ServiceController@deleteVariable' + ]); }); } diff --git a/app/Repositories/ServiceRepository/Variable.php b/app/Repositories/ServiceRepository/Variable.php index 88a21e5c9..714c5c0eb 100644 --- a/app/Repositories/ServiceRepository/Variable.php +++ b/app/Repositories/ServiceRepository/Variable.php @@ -40,6 +40,58 @@ class Variable // } + public function create($id, array $data) + { + $option = Models\ServiceOptions::findOrFail($id); + + $validator = Validator::make($data, [ + 'name' => 'required|string|min:1|max:255', + 'description' => 'required|string', + 'env_variable' => 'required|regex:/^[\w]{1,255}$/', + 'default_value' => 'required|string|max:255', + 'user_viewable' => 'sometimes|required|numeric|size:1', + 'user_editable' => 'sometimes|required|numeric|size:1', + 'required' => 'sometimes|required|numeric|size:1', + 'regex' => 'required|string|min:1' + ]); + + if ($validator->fails()) { + throw new DisplayValidationException($validator->errors()); + } + + if (!preg_match($data['regex'], $data['default_value'])) { + throw new DisplayException('The default value you entered cannot violate the regex requirements.'); + } + + if (Models\ServiceVariables::where('env_variable', $data['env_variable'])->where('option_id', $option->id)->first()) { + throw new DisplayException('An environment variable with that name already exists for this option.'); + } + + $data['user_viewable'] = (isset($data['user_viewable']) && in_array((int) $data['user_viewable'], [0, 1])) ? $data['user_viewable'] : 0; + $data['user_editable'] = (isset($data['user_editable']) && in_array((int) $data['user_editable'], [0, 1])) ? $data['user_editable'] : 0; + $data['required'] = (isset($data['required']) && in_array((int) $data['required'], [0, 1])) ? $data['required'] : 0; + + $variable = new Models\ServiceVariables; + $variable->option_id = $option->id; + $variable->fill($data); + $variable->save(); + } + + public function delete($id) { + $variable = Models\ServiceVariables::findOrFail($id); + + DB::beginTransaction(); + try { + Models\ServerVariables::where('variable_id', $variable->id)->delete(); + $variable->delete(); + + DB::commit(); + } catch (\Exception $ex) { + DB::rollBack(); + throw $ex; + } + } + public function update($id, array $data) { $variable = Models\ServiceVariables::findOrFail($id); @@ -48,7 +100,7 @@ class Variable 'name' => 'sometimes|required|string|min:1|max:255', 'description' => 'sometimes|required|string', 'env_variable' => 'sometimes|required|regex:/^[\w]{1,255}$/', - 'default_value' => 'sometimes|required|string', + 'default_value' => 'sometimes|required|string|max:255', 'user_viewable' => 'sometimes|required|numeric|size:1', 'user_editable' => 'sometimes|required|numeric|size:1', 'required' => 'sometimes|required|numeric|size:1', @@ -66,6 +118,10 @@ class Variable throw new DisplayException('The default value you entered cannot violate the regex requirements.'); } + if (Models\ServiceVariables::where('id', '!=', $variable->id)->where('env_variable', $data['env_variable'])->where('option_id', $variable->option_id)->first()) { + throw new DisplayException('An environment variable with that name already exists for this option.'); + } + $data['user_viewable'] = (isset($data['user_viewable']) && in_array((int) $data['user_viewable'], [0, 1])) ? $data['user_viewable'] : $variable->user_viewable; $data['user_editable'] = (isset($data['user_editable']) && in_array((int) $data['user_editable'], [0, 1])) ? $data['user_editable'] : $variable->user_editable; $data['required'] = (isset($data['required']) && in_array((int) $data['required'], [0, 1])) ? $data['required'] : $variable->required; diff --git a/resources/views/admin/services/options/variable.blade.php b/resources/views/admin/services/options/variable.blade.php index c48a4f21e..9cfceea60 100644 --- a/resources/views/admin/services/options/variable.blade.php +++ b/resources/views/admin/services/options/variable.blade.php @@ -29,14 +29,94 @@
  • Admin Control
  • Services
  • {{ $service->name }}
  • -
  • {{ $option->name }}
  • +
  • {{ $option->name }}
  • New Variable
  • New Option Variable


    +
    +
    +
    +
    + +
    + +
    +
    +
    +
    +
    + +
    + +
    +
    +
    +
    +
    + +
    + +

    Regex code to use when verifying the contents of the field.

    +
    +
    +
    +
    +
    + +
    + +

    Accessed in startup by using {{}} parameter.

    +
    +
    +
    + +
    + +

    The default value to use for this field.

    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + {!! csrf_field() !!} + +
    +
    +
    +
    @endsection diff --git a/resources/views/admin/services/options/view.blade.php b/resources/views/admin/services/options/view.blade.php index e4626356b..b5bf2f548 100644 --- a/resources/views/admin/services/options/view.blade.php +++ b/resources/views/admin/services/options/view.blade.php @@ -88,7 +88,7 @@ -

    Variables


    +

    Variables


    @foreach($variables as $variable)
    @@ -158,7 +158,8 @@
    {!! csrf_field() !!} - + +