Skip to content

Commit 48b9bc0

Browse files
committed
add support for variable creation and deletion
1 parent dcf2f6f commit 48b9bc0

File tree

5 files changed

+198
-4
lines changed

5 files changed

+198
-4
lines changed

app/Http/Controllers/Admin/ServiceController.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,34 @@ public function postOptionVariable(Request $request, $service, $option, $variabl
202202
return redirect()->route('admin.services.option', [$service, $option])->withInput();
203203
}
204204

205+
public function getNewVariable(Request $request, $service, $option)
206+
{
207+
return view('admin.services.options.variable', [
208+
'service' => Models\Service::findOrFail($service),
209+
'option' => Models\ServiceOptions::where('parent_service', $service)->where('id', $option)->firstOrFail()
210+
]);
211+
}
212+
213+
public function postNewVariable(Request $request, $service, $option)
214+
{
215+
try {
216+
$repo = new ServiceRepository\Variable;
217+
$repo->create($option, $request->except([
218+
'_token'
219+
]));
220+
Alert::success('Successfully added new variable to this option.')->flash();
221+
return redirect()->route('admin.services.option', [$service, $option])->withInput();
222+
} catch (DisplayValidationException $ex) {
223+
return redirect()->route('admin.services.option.variable.new', [$service, $option])->withErrors(json_decode($ex->getMessage()))->withInput();
224+
} catch (DisplayException $ex) {
225+
Alert::danger($ex->getMessage())->flash();
226+
} catch (\Exception $ex) {
227+
Log::error($ex);
228+
Alert::danger('An error occurred while attempting to add this variable.')->flash();
229+
}
230+
return redirect()->route('admin.services.option.variable.new', [$service, $option])->withInput();
231+
}
232+
205233
public function newOption(Request $request, $service)
206234
{
207235
return view('admin.services.options.new', [
@@ -227,4 +255,19 @@ public function postNewOption(Request $request, $service)
227255
return redirect()->route('admin.services.option.new', $service)->withInput();
228256
}
229257

258+
public function deleteVariable(Request $request, $service, $option, $variable)
259+
{
260+
try {
261+
$repo = new ServiceRepository\Variable;
262+
$repo->delete($variable);
263+
Alert::success('Deleted variable.')->flash();
264+
} catch (DisplayException $ex) {
265+
Alert::danger($ex->getMessage())->flash();
266+
} catch (\Exception $ex) {
267+
Log::error($ex);
268+
Alert::danger('An error occured while attempting to delete that variable.')->flash();
269+
}
270+
return redirect()->route('admin.services.option', [$service, $option]);
271+
}
272+
230273
}

app/Http/Routes/AdminRoutes.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,10 +392,24 @@ public function map(Router $router) {
392392
'uses' => 'Admin\ServiceController@deleteOption'
393393
]);
394394

395+
$router->get('/service/{service}/option/{option}/variable/new', [
396+
'as' => 'admin.services.option.variable.new',
397+
'uses' => 'Admin\ServiceController@getNewVariable'
398+
]);
399+
400+
$router->post('/service/{service}/option/{option}/variable/new', [
401+
'uses' => 'Admin\ServiceController@postNewVariable'
402+
]);
403+
395404
$router->post('/service/{service}/option/{option}/variable/{variable}', [
396405
'as' => 'admin.services.option.variable',
397406
'uses' => 'Admin\ServiceController@postOptionVariable'
398407
]);
408+
409+
$router->get('/service/{service}/option/{option}/variable/{variable}/delete', [
410+
'as' => 'admin.services.option.variable.delete',
411+
'uses' => 'Admin\ServiceController@deleteVariable'
412+
]);
399413
});
400414

401415
}

app/Repositories/ServiceRepository/Variable.php

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,58 @@ public function __construct()
4040
//
4141
}
4242

43+
public function create($id, array $data)
44+
{
45+
$option = Models\ServiceOptions::findOrFail($id);
46+
47+
$validator = Validator::make($data, [
48+
'name' => 'required|string|min:1|max:255',
49+
'description' => 'required|string',
50+
'env_variable' => 'required|regex:/^[\w]{1,255}$/',
51+
'default_value' => 'required|string|max:255',
52+
'user_viewable' => 'sometimes|required|numeric|size:1',
53+
'user_editable' => 'sometimes|required|numeric|size:1',
54+
'required' => 'sometimes|required|numeric|size:1',
55+
'regex' => 'required|string|min:1'
56+
]);
57+
58+
if ($validator->fails()) {
59+
throw new DisplayValidationException($validator->errors());
60+
}
61+
62+
if (!preg_match($data['regex'], $data['default_value'])) {
63+
throw new DisplayException('The default value you entered cannot violate the regex requirements.');
64+
}
65+
66+
if (Models\ServiceVariables::where('env_variable', $data['env_variable'])->where('option_id', $option->id)->first()) {
67+
throw new DisplayException('An environment variable with that name already exists for this option.');
68+
}
69+
70+
$data['user_viewable'] = (isset($data['user_viewable']) && in_array((int) $data['user_viewable'], [0, 1])) ? $data['user_viewable'] : 0;
71+
$data['user_editable'] = (isset($data['user_editable']) && in_array((int) $data['user_editable'], [0, 1])) ? $data['user_editable'] : 0;
72+
$data['required'] = (isset($data['required']) && in_array((int) $data['required'], [0, 1])) ? $data['required'] : 0;
73+
74+
$variable = new Models\ServiceVariables;
75+
$variable->option_id = $option->id;
76+
$variable->fill($data);
77+
$variable->save();
78+
}
79+
80+
public function delete($id) {
81+
$variable = Models\ServiceVariables::findOrFail($id);
82+
83+
DB::beginTransaction();
84+
try {
85+
Models\ServerVariables::where('variable_id', $variable->id)->delete();
86+
$variable->delete();
87+
88+
DB::commit();
89+
} catch (\Exception $ex) {
90+
DB::rollBack();
91+
throw $ex;
92+
}
93+
}
94+
4395
public function update($id, array $data)
4496
{
4597
$variable = Models\ServiceVariables::findOrFail($id);
@@ -48,7 +100,7 @@ public function update($id, array $data)
48100
'name' => 'sometimes|required|string|min:1|max:255',
49101
'description' => 'sometimes|required|string',
50102
'env_variable' => 'sometimes|required|regex:/^[\w]{1,255}$/',
51-
'default_value' => 'sometimes|required|string',
103+
'default_value' => 'sometimes|required|string|max:255',
52104
'user_viewable' => 'sometimes|required|numeric|size:1',
53105
'user_editable' => 'sometimes|required|numeric|size:1',
54106
'required' => 'sometimes|required|numeric|size:1',
@@ -66,6 +118,10 @@ public function update($id, array $data)
66118
throw new DisplayException('The default value you entered cannot violate the regex requirements.');
67119
}
68120

121+
if (Models\ServiceVariables::where('id', '!=', $variable->id)->where('env_variable', $data['env_variable'])->where('option_id', $variable->option_id)->first()) {
122+
throw new DisplayException('An environment variable with that name already exists for this option.');
123+
}
124+
69125
$data['user_viewable'] = (isset($data['user_viewable']) && in_array((int) $data['user_viewable'], [0, 1])) ? $data['user_viewable'] : $variable->user_viewable;
70126
$data['user_editable'] = (isset($data['user_editable']) && in_array((int) $data['user_editable'], [0, 1])) ? $data['user_editable'] : $variable->user_editable;
71127
$data['required'] = (isset($data['required']) && in_array((int) $data['required'], [0, 1])) ? $data['required'] : $variable->required;

resources/views/admin/services/options/variable.blade.php

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,94 @@
2929
<li><a href="/admin">Admin Control</a></li>
3030
<li><a href="/admin/services">Services</a></li>
3131
<li><a href="{{ route('admin.services.service', $service->id) }}">{{ $service->name }}</a></li>
32-
<li><a href="{{ route('admin.services.option', $option->id) }}">{{ $option->name }}</a></li>
32+
<li><a href="{{ route('admin.services.option', [$service->id, $option->id]) }}">{{ $option->name }}</a></li>
3333
<li class="active">New Variable</li>
3434
</ul>
3535
<h3>New Option Variable</h3><hr />
36+
<form action="{{ route('admin.services.option.variable.new', [$service->id, $option->id]) }}" method="POST">
37+
<div class="well">
38+
<div class="row">
39+
<div class="col-md-12 form-group">
40+
<label class="control-label">Variable Name:</label>
41+
<div>
42+
<input type="text" name="name" class="form-control" value="{{ old('name') }}" />
43+
</div>
44+
</div>
45+
</div>
46+
<div class="row">
47+
<div class="col-md-12 form-group">
48+
<label class="control-label">Variable Description:</label>
49+
<div>
50+
<textarea name="description" class="form-control" rows="4">{{ old('description') }}</textarea>
51+
</div>
52+
</div>
53+
</div>
54+
<div class="row">
55+
<div class="col-md-12 form-group">
56+
<label class="control-label">Regex:</label>
57+
<div>
58+
<input type="text" name="regex" class="form-control" value="{{ old('regex') }}" />
59+
<p class="text-muted"><small>Regex code to use when verifying the contents of the field.</small></p>
60+
</div>
61+
</div>
62+
</div>
63+
<div class="row">
64+
<div class="col-md-6 form-group">
65+
<label class="control-label">Environment Variable:</label>
66+
<div>
67+
<input type="text" name="env_variable" id="env_var" class="form-control" value="{{ old('env_variable') }}" />
68+
<p class="text-muted"><small>Accessed in startup by using <code>&#123;&#123;&#125;&#125;</code> parameter.</small></p>
69+
</div>
70+
</div>
71+
<div class="col-md-6 form-group">
72+
<label class="control-label">Default Value:</label>
73+
<div>
74+
<input type="text" name="default_value" class="form-control" value="{{ old('default_value') }}" />
75+
<p class="text-muted"><small>The default value to use for this field.</small></p>
76+
</div>
77+
</div>
78+
</div>
79+
<div class="row fuelux">
80+
<div class="col-md-4">
81+
<div class="checkbox highlight">
82+
<label class="checkbox-custom highlight" data-initialize="checkbox">
83+
<input class="sr-only" name="user_viewable" type="checkbox" value="1" @if((int) old('user_viewable') === 1)checked="checked"@endif> <strong>User Viewable</strong>
84+
<p class="text-muted"><small>Can users view this variable?</small><p>
85+
</label>
86+
</div>
87+
</div>
88+
<div class="col-md-4">
89+
<div class="checkbox highlight">
90+
<label class="checkbox-custom highlight" data-initialize="checkbox">
91+
<input class="sr-only" name="user_editable" type="checkbox" value="1" @if((int) old('user_editable') === 1)checked="checked"@endif> <strong>User Editable</strong>
92+
<p class="text-muted"><small>Can users edit this variable?</small><p>
93+
</label>
94+
</div>
95+
</div>
96+
<div class="col-md-4">
97+
<div class="checkbox highlight">
98+
<label class="checkbox-custom highlight" data-initialize="checkbox">
99+
<input class="sr-only" name="required" type="checkbox" value="1" @if((int) old('required') === 1)checked="checked"@endif> <strong>Required</strong>
100+
<p class="text-muted"><small>This this variable required?</small><p>
101+
</label>
102+
</div>
103+
</div>
104+
</div>
105+
<div class="row">
106+
<div class="col-md-12">
107+
{!! csrf_field() !!}
108+
<input type="submit" class="btn btn-sm btn-primary" value="Add Variable" />
109+
</div>
110+
</div>
111+
</div>
112+
</form>
36113
</div>
37114
<script>
38115
$(document).ready(function () {
39116
$('#sidebar_links').find("a[href='/admin/services']").addClass('active');
117+
$('#env_var').on('keyup', function () {
118+
$(this).parent().find('code').html('&#123;&#123;' + escape($(this).val()) + '&#125;&#125;');
119+
});
40120
});
41121
</script>
42122
@endsection

resources/views/admin/services/options/view.blade.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888
</div>
8989
</div>
9090
</form>
91-
<h3>Variables</h3><hr />
91+
<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 />
9292
@foreach($variables as $variable)
9393
<form action="{{ route('admin.services.option.variable', [$service->id, $option->id, $variable->id]) }}" method="POST">
9494
<div class="well">
@@ -158,7 +158,8 @@
158158
<div class="row">
159159
<div class="col-md-12">
160160
{!! csrf_field() !!}
161-
<input type="submit" class="btn btn-sm btn-success pull-right" value="Update Variable" />
161+
<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>
162+
<input type="submit" class="btn btn-sm btn-success" value="Update Variable" />
162163
</div>
163164
</div>
164165
</div>

0 commit comments

Comments
 (0)