add support for variable creation and deletion

This commit is contained in:
Dane Everitt 2016-02-21 00:38:03 -05:00
parent dcf2f6fa0a
commit 48b9bc0c52
5 changed files with 198 additions and 4 deletions

View File

@ -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]);
}
}

View File

@ -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'
]);
});
}

View File

@ -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;

View File

@ -29,14 +29,94 @@
<li><a href="/admin">Admin Control</a></li>
<li><a href="/admin/services">Services</a></li>
<li><a href="{{ route('admin.services.service', $service->id) }}">{{ $service->name }}</a></li>
<li><a href="{{ route('admin.services.option', $option->id) }}">{{ $option->name }}</a></li>
<li><a href="{{ route('admin.services.option', [$service->id, $option->id]) }}">{{ $option->name }}</a></li>
<li class="active">New Variable</li>
</ul>
<h3>New Option Variable</h3><hr />
<form action="{{ route('admin.services.option.variable.new', [$service->id, $option->id]) }}" method="POST">
<div class="well">
<div class="row">
<div class="col-md-12 form-group">
<label class="control-label">Variable Name:</label>
<div>
<input type="text" name="name" class="form-control" value="{{ old('name') }}" />
</div>
</div>
</div>
<div class="row">
<div class="col-md-12 form-group">
<label class="control-label">Variable Description:</label>
<div>
<textarea name="description" class="form-control" rows="4">{{ old('description') }}</textarea>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12 form-group">
<label class="control-label">Regex:</label>
<div>
<input type="text" name="regex" class="form-control" value="{{ old('regex') }}" />
<p class="text-muted"><small>Regex code to use when verifying the contents of the field.</small></p>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6 form-group">
<label class="control-label">Environment Variable:</label>
<div>
<input type="text" name="env_variable" id="env_var" class="form-control" value="{{ old('env_variable') }}" />
<p class="text-muted"><small>Accessed in startup by using <code>&#123;&#123;&#125;&#125;</code> parameter.</small></p>
</div>
</div>
<div class="col-md-6 form-group">
<label class="control-label">Default Value:</label>
<div>
<input type="text" name="default_value" class="form-control" value="{{ old('default_value') }}" />
<p class="text-muted"><small>The default value to use for this field.</small></p>
</div>
</div>
</div>
<div class="row fuelux">
<div class="col-md-4">
<div class="checkbox highlight">
<label class="checkbox-custom highlight" data-initialize="checkbox">
<input class="sr-only" name="user_viewable" type="checkbox" value="1" @if((int) old('user_viewable') === 1)checked="checked"@endif> <strong>User Viewable</strong>
<p class="text-muted"><small>Can users view this variable?</small><p>
</label>
</div>
</div>
<div class="col-md-4">
<div class="checkbox highlight">
<label class="checkbox-custom highlight" data-initialize="checkbox">
<input class="sr-only" name="user_editable" type="checkbox" value="1" @if((int) old('user_editable') === 1)checked="checked"@endif> <strong>User Editable</strong>
<p class="text-muted"><small>Can users edit this variable?</small><p>
</label>
</div>
</div>
<div class="col-md-4">
<div class="checkbox highlight">
<label class="checkbox-custom highlight" data-initialize="checkbox">
<input class="sr-only" name="required" type="checkbox" value="1" @if((int) old('required') === 1)checked="checked"@endif> <strong>Required</strong>
<p class="text-muted"><small>This this variable required?</small><p>
</label>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
{!! csrf_field() !!}
<input type="submit" class="btn btn-sm btn-primary" value="Add Variable" />
</div>
</div>
</div>
</form>
</div>
<script>
$(document).ready(function () {
$('#sidebar_links').find("a[href='/admin/services']").addClass('active');
$('#env_var').on('keyup', function () {
$(this).parent().find('code').html('&#123;&#123;' + escape($(this).val()) + '&#125;&#125;');
});
});
</script>
@endsection

View File

@ -88,7 +88,7 @@
</div>
</div>
</form>
<h3>Variables</h3><hr />
<h3>Variables <small><a href="{{ route('admin.services.option.variable.new', [$service->id, $option->id]) }}"><i class="fa fa-plus"></i></a></small></h3><hr />
@foreach($variables as $variable)
<form action="{{ route('admin.services.option.variable', [$service->id, $option->id, $variable->id]) }}" method="POST">
<div class="well">
@ -158,7 +158,8 @@
<div class="row">
<div class="col-md-12">
{!! csrf_field() !!}
<input type="submit" class="btn btn-sm btn-success pull-right" value="Update Variable" />
<a href="{{ route('admin.services.option.variable.delete', [$service->id, $option->id, $variable->id]) }}"><button type="button" class="btn btn-sm btn-danger pull-right"><i class="fa fa-times"></i></button></a>
<input type="submit" class="btn btn-sm btn-success" value="Update Variable" />
</div>
</div>
</div>