diff --git a/app/controllers/admin/ModelsController.php b/app/controllers/admin/ModelsController.php index 25bc43762c..561a71641c 100755 --- a/app/controllers/admin/ModelsController.php +++ b/app/controllers/admin/ModelsController.php @@ -16,6 +16,9 @@ use Validator; use View; use Datatable; +//use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpFoundation\JsonResponse; + class ModelsController extends AdminController { /** @@ -114,6 +117,32 @@ class ModelsController extends AdminController return Redirect::to('hardware/models/create')->with('error', Lang::get('admin/models/message.create.error')); } + + public function store() + { + //COPYPASTA!!!! FIXME + $model = new Model; + + $settings=Input::all(); + $settings['eol']=0; + // + + $validator = Validator::make($settings, $model->validationRules()); + if ($validator->fails()) + { + // The given data did not pass validation + return JsonResponse::create(["error" => "Failed validation: ".print_r($validator->messages()->all('
  • :message
  • '),true)],500); + } else { + $model->name=e(Input::get('name')); + $model->eol=0; + + if($model->save()) { + return JsonResponse::create($model); + } else { + return JsonResponse::create(["error" => "Couldn't save Model"],500); + } + } + } /** * Model update. diff --git a/app/controllers/admin/StatuslabelsController.php b/app/controllers/admin/StatuslabelsController.php index 0185052e43..8dc616ce0d 100755 --- a/app/controllers/admin/StatuslabelsController.php +++ b/app/controllers/admin/StatuslabelsController.php @@ -12,6 +12,8 @@ use Str; use Validator; use View; +use Symfony\Component\HttpFoundation\JsonResponse; + class StatuslabelsController extends AdminController { /** @@ -89,6 +91,43 @@ class StatuslabelsController extends AdminController return Redirect::to('admin/settings/statuslabels/create')->with('error', Lang::get('admin/statuslabels/message.create.error')); } + + public function store() + { + // get the POST data + $new = Input::all(); + + $new['statuslabel_types']="deployable"; + + // create a new model instance + $statuslabel = new Statuslabel(); + + // attempt validation + if ($statuslabel->validate($new)) { + + //$statustype = Statuslabel::getStatuslabelTypesForDB(Input::get('statuslabel_types')); + + // Save the Statuslabel data + $statuslabel->name = e(Input::get('name')); + $statuslabel->user_id = Sentry::getId(); + //$statuslabel->notes = e(Input::get('notes')); + $statuslabel->deployable = true; //$statustype['deployable']; + $statuslabel->pending = false; //$statustype['pending']; + $statuslabel->archived = false; //$statustype['archived']; + + // Was the asset created? + if($statuslabel->save()) { + // Redirect to the new Statuslabel page + return JsonResponse::create($statuslabel); + } else { + return JsonResponse::create(["error" => "Couldn't save Statuslabel"],500); + } + } else { + // failure + $errors = $statuslabel->errors(); + return JsonResponse::create(["error" => "Failed validation: ".print_r($errors->all('
  • :message
  • '),true)],500); + } + } /** diff --git a/app/controllers/admin/SuppliersController.php b/app/controllers/admin/SuppliersController.php index a1f71a00d0..2244459285 100755 --- a/app/controllers/admin/SuppliersController.php +++ b/app/controllers/admin/SuppliersController.php @@ -13,6 +13,9 @@ use Str; use Validator; use View; +use Symfony\Component\HttpFoundation\JsonResponse; + + class SuppliersController extends AdminController { /** @@ -100,6 +103,26 @@ class SuppliersController extends AdminController return Redirect::to('admin/settings/suppliers/create')->with('error', Lang::get('admin/suppliers/message.create.error')); } + + public function store() + { + $supplier=new Supplier; + $new=Input::all(); + $validator = Validator::make($new, $supplier->validationRules()); + if($validator->fails()) { + return JsonResponse::create(["error" => "Failed validation: ".print_r($validator->messages()->all('
  • :message
  • '),true)],500); + } else { + //$supplier->fill($new); + $supplier->name=$new['name']; + $supplier->user_id = Sentry::getId(); + + if($supplier->save()) { + return JsonResponse::create($supplier); + } else { + return JsonResponse::create(["error" => "Couldn't save Supplier"]); + } + } + } /** * Supplier update. diff --git a/app/routes.php b/app/routes.php index 4aaf17d725..bed26555be 100755 --- a/app/routes.php +++ b/app/routes.php @@ -13,14 +13,15 @@ Route::group(array('prefix' => 'api', 'namespace' => 'Controllers\Admin', 'befor /*---Status Label API---*/ Route::group(array('prefix'=>'statuslabels'), function() { - Route::get('{statuslabelId}/deployable', function ($statuslabelId) { - $statuslabel = Statuslabel::find($statuslabelId); - if (($statuslabel->deployable=='1') && ($statuslabel->pending!='1') && ($statuslabel->archived!='1')) { - return '1'; - } else { - return '0'; - } - }); + Route::resource('/','StatuslabelsController'); + Route::get('{statuslabelId}/deployable', function ($statuslabelId) { + $statuslabel = Statuslabel::find($statuslabelId); + if (($statuslabel->deployable=='1') && ($statuslabel->pending!='1') && ($statuslabel->archived!='1')) { + return '1'; + } else { + return '0'; + } + }); }); /*---Accessories API---*/ @@ -75,6 +76,10 @@ Route::group(array('prefix' => 'api', 'namespace' => 'Controllers\Admin', 'befor Route::get('list', ['as'=>'api.categories.list', 'uses'=>'CategoriesController@getDatatable']); Route::get('{categoryID}/view', ['as'=>'api.categories.view', 'uses'=>'CategoriesController@getDataView']); }); + /*-- Suppliers API (mostly for creating new ones in-line while creating an asset) --*/ + Route::group(['prefix'=>'suppliers'], function () { + Route::resource('/', 'SuppliersController'); + }); }); /* diff --git a/app/views/backend/hardware/edit.blade.php b/app/views/backend/hardware/edit.blade.php index 104ee15730..8585afc78f 100755 --- a/app/views/backend/hardware/edit.blade.php +++ b/app/views/backend/hardware/edit.blade.php @@ -88,7 +88,7 @@ @endif - + Add new Model... {{ $errors->first('model_id', '
    :message') }} @@ -116,7 +116,8 @@
    - {{ Form::select('supplier_id', $supplier_list , Input::old('supplier_id', $asset->supplier_id), array('class'=>'select2', 'style'=>'min-width:350px')) }} + {{ Form::select('supplier_id', $supplier_list , Input::old('supplier_id', $asset->supplier_id), array('class'=>'select2', 'style'=>'min-width:350px','id'=>'supplier_select_id')) }} + Add new Supplier... {{ $errors->first('supplier_id', '
    :message') }}
    @@ -165,8 +166,8 @@
    - {{ Form::select('status_id', $statuslabel_list , Input::old('status_id', $asset->status_id), array('class'=>'select2 status_id', 'style'=>'width:350px')) }} - + {{ Form::select('status_id', $statuslabel_list , Input::old('status_id', $asset->status_id), array('class'=>'select2 status_id', 'style'=>'width:350px','id'=>'status_select_id')) }} + Add new Status...

    @lang('admin/hardware/form.help_checkout')

    @@ -181,6 +182,7 @@
    {{ Form::select('assigned_to', $assigned_to , Input::old('assigned_to', $asset->assigned_to), array('class'=>'select2', 'id'=>'assigned_to', 'style'=>'min-width:350px')) }} + Add new User... {{ $errors->first('assigned_to', '
    :message') }}
    @@ -199,7 +201,8 @@
    - {{ Form::select('rtd_location_id', $location_list , Input::old('rtd_location_id', $asset->rtd_location_id), array('class'=>'select2', 'style'=>'width:350px')) }} + {{ Form::select('rtd_location_id', $location_list , Input::old('rtd_location_id', $asset->rtd_location_id), array('class'=>'select2', 'style'=>'width:350px','id'=>'rtd_location_select')) }} + Add new Location... {{ $errors->first('rtd_location_id', '
    :message') }}
    @@ -291,6 +294,24 @@ }; +function dependency(what,select_id) { + var name=prompt("Enter the name of your new "+what); + $.post("{{Config::get('app.url')}}/api/"+what+"s",{name: name},function (result) { + var id=result.id; + var name=result.name; + + var select=document.getElementById(select_id); + select.options[select.length]=new Option(name,id); + select.selectedIndex=select.length-1; + $(select).trigger("change"); + }).fail(function (result) { + //console.dir(result.responseJSON); + msg=result.responseJSON.error.message || result.responseJSON.error; + window.alert("Unable to add new "+what+" - error: "+msg); + }); + return false; +} +