From 28304556a941e84bc8271a4cd2ebc5dc4494a3f9 Mon Sep 17 00:00:00 2001 From: Brady Wetherington Date: Sun, 26 Jul 2015 18:15:06 -0700 Subject: [PATCH 1/2] First pass at this, the ugly way. Just to see wha it looks like. --- app/controllers/admin/ModelsController.php | 29 ++++++++++++++ .../admin/StatuslabelsController.php | 39 +++++++++++++++++++ app/controllers/admin/SuppliersController.php | 23 +++++++++++ app/routes.php | 21 ++++++---- app/views/backend/hardware/edit.blade.php | 31 ++++++++++++--- 5 files changed, 130 insertions(+), 13 deletions(-) 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; +} + From de8180ec70cdf59935679526b28cae49458b70df Mon Sep 17 00:00:00 2001 From: Brady Wetherington Date: Mon, 27 Jul 2015 23:27:45 -0700 Subject: [PATCH 2/2] Abililty to inline-add things when you are creating a new asset --- app/controllers/admin/AssetsController.php | 8 + app/controllers/admin/LocationsController.php | 48 +++++ app/controllers/admin/ModelsController.php | 3 + app/controllers/admin/UsersController.php | 61 ++++++ app/macros.php | 8 +- app/routes.php | 5 +- app/views/backend/hardware/edit.blade.php | 185 +++++++++++++++--- 7 files changed, 290 insertions(+), 28 deletions(-) diff --git a/app/controllers/admin/AssetsController.php b/app/controllers/admin/AssetsController.php index 3e74143715..c376e73410 100755 --- a/app/controllers/admin/AssetsController.php +++ b/app/controllers/admin/AssetsController.php @@ -27,6 +27,7 @@ use Mail; use Datatable; use TCPDF; use Slack; +use Manufacturer; //for embedded-create class AssetsController extends AdminController { @@ -77,6 +78,11 @@ class AssetsController extends AdminController // Grab the dropdown list of status $statuslabel_list = array('' => Lang::get('general.select_statuslabel')) + Statuslabel::orderBy('name', 'asc')->lists('name', 'id'); + + // grap dropdown lists for embedded create drop-downs + $manufacturer_list = array('' => 'Select One') + Manufacturer::lists('name', 'id'); + $category_list = array('' => '') + DB::table('categories')->whereNull('deleted_at')->lists('name', 'id'); + $view = View::make('backend/hardware/edit'); $view->with('supplier_list',$supplier_list); @@ -85,6 +91,8 @@ class AssetsController extends AdminController $view->with('assigned_to',$assigned_to); $view->with('location_list',$location_list); $view->with('asset',new Asset); + $view->with('manufacturer',$manufacturer_list); + $view->with('category',$category_list); if (!is_null($model_id)) { $selected_model = Model::find($model_id); diff --git a/app/controllers/admin/LocationsController.php b/app/controllers/admin/LocationsController.php index dc4d4174d1..22ce45cd62 100755 --- a/app/controllers/admin/LocationsController.php +++ b/app/controllers/admin/LocationsController.php @@ -12,6 +12,8 @@ use Str; use Validator; use View; +use Symfony\Component\HttpFoundation\JsonResponse; + class LocationsController extends AdminController { /** @@ -97,6 +99,52 @@ class LocationsController extends AdminController return Redirect::to('admin/settings/locations/create')->with('error', Lang::get('admin/locations/message.create.error')); } + + public function store() + { + $new = Input::all(); + + $new['currency']=Setting::first()->default_currency; + + // create a new location instance + $location = new Location(); + + // attempt validation + if ($location->validate($new)) { + + // Save the location data + $location->name = e(Input::get('name')); + // if (Input::get('parent_id')=='') { + // $location->parent_id = null; + // } else { + // $location->parent_id = e(Input::get('parent_id')); + // } + $location->currency = Setting::first()->default_currency; //e(Input::get('currency')); + $location->address = ''; //e(Input::get('address')); + // $location->address2 = e(Input::get('address2')); + $location->city = e(Input::get('city')); + $location->state = '';//e(Input::get('state')); + $location->country = e(Input::get('country')); + // $location->zip = e(Input::get('zip')); + $location->user_id = Sentry::getId(); + + // Was the asset created? + if($location->save()) { + // Redirect to the new location page + return JsonResponse::create($location); + //return Redirect::to("admin/settings/locations")->with('success', Lang::get('admin/locations/message.create.success')); + } else { + return JsonResponse::create(["error" => "Couldn't save Location"],500); + } + } else { + // failure + $errors = $location->errors(); + return JsonResponse::create(["error" => "Failed validation: ".print_r($errors->all('
  • :message
  • '),true)],500); + } + + // Redirect to the location create page + return Redirect::to('admin/settings/locations/create')->with('error', Lang::get('admin/locations/message.create.error')); + } /** diff --git a/app/controllers/admin/ModelsController.php b/app/controllers/admin/ModelsController.php index 561a71641c..4f41631a87 100755 --- a/app/controllers/admin/ModelsController.php +++ b/app/controllers/admin/ModelsController.php @@ -134,6 +134,9 @@ class ModelsController extends AdminController return JsonResponse::create(["error" => "Failed validation: ".print_r($validator->messages()->all('
  • :message
  • '),true)],500); } else { $model->name=e(Input::get('name')); + $model->manufacturer_id=e(Input::get('manufacturer_id')); + $model->category_id=e(Input::get('category_id')); + $model->user_id=Sentry::getUser()->id; $model->eol=0; if($model->save()) { diff --git a/app/controllers/admin/UsersController.php b/app/controllers/admin/UsersController.php index 0c1e7a5dec..400b626bde 100755 --- a/app/controllers/admin/UsersController.php +++ b/app/controllers/admin/UsersController.php @@ -27,6 +27,7 @@ use Datatable; use League\Csv\Reader; use Mail; use Accessory; +use Symfony\Component\HttpFoundation\JsonResponse; class UsersController extends AdminController { @@ -186,6 +187,66 @@ class UsersController extends AdminController // Redirect to the user creation page return Redirect::route('create/user')->withInput()->with('error', $error); } + + public function store() + { + // Create a new validator instance from our validation rules + $validator = Validator::make(Input::all(), $this->validationRules); + $permissions = Input::get('permissions', array()); + $this->decodePermissions($permissions); + app('request')->request->set('permissions', $permissions); + + // If validation fails, we'll exit the operation now. + if ($validator->fails()) { + // Ooops.. something went wrong + return JsonResponse::create(["error" => "Failed validation: ".print_r($validator->messages()->all('
  • :message
  • '),true)],500); } + + try { + // We need to reverse the UI specific logic for our + // permissions here before we create the user. + + // Get the inputs, with some exceptions + $inputs = Input::except('csrf_token', 'password_confirm', 'groups','email_user'); + + // @TODO: Figure out WTF I need to do this. + /*if ($inputs['manager_id']=='') { + unset($inputs['manager_id']); + }*/ + + /*if ($inputs['location_id']=='') { + unset($inputs['location_id']); + }*/ + + // Was the user created? + if ($user = Sentry::getUserProvider()->create($inputs)) { + if (Input::get('email_user')==1) { + // Send the credentials through email + + $data = array(); + $data['email'] = e(Input::get('email')); + $data['first_name'] = e(Input::get('first_name')); + $data['password'] = e(Input::get('password')); + + Mail::send('emails.send-login', $data, function ($m) use ($user) { + $m->to($user->email, $user->first_name . ' ' . $user->last_name); + $m->subject('Welcome ' . $user->first_name); + }); + } + + + return JsonResponse::create($user); + } else { + return JsonResponse::create(["error" => "Couldn't save User"],500); + } + + + + } catch (Exception $e) { + + // Redirect to the user creation page + return JsonResponse::create(["error" => "Failed validation: ".print_r($validator->messages()->all('
  • :message
  • '),true)],500); + } + } /** * User update. diff --git a/app/macros.php b/app/macros.php index 53f6e86568..253f6fdcac 100755 --- a/app/macros.php +++ b/app/macros.php @@ -8,7 +8,7 @@ * Country macro * Generates the dropdown menu of countries for the profile form */ -Form::macro('countries', function ($name = "country", $selected = null, $class = null) { +Form::macro('countries', function ($name = "country", $selected = null, $class = null, $id = null) { $countries = array( ''=>"Select a Country", @@ -262,7 +262,11 @@ Form::macro('countries', function ($name = "country", $selected = null, $class = 'ZW'=>'Zimbabwe' ); - $select = ''; foreach ($countries as $abbr => $country) { $select .= ' '; diff --git a/app/routes.php b/app/routes.php index bed26555be..edd1e9d0c6 100755 --- a/app/routes.php +++ b/app/routes.php @@ -48,9 +48,10 @@ Route::group(array('prefix' => 'api', 'namespace' => 'Controllers\Admin', 'befor }); /*---Locations API---*/ Route::group(array('prefix'=>'locations'), function() { + Route::resource('/','LocationsController'); Route::get('{locationID}/check', function ($locationID) { - $location = Location::find($locationID); - return $location; + $location = Location::find($locationID); + return $location; }); }); /*---Improvements API---*/ diff --git a/app/views/backend/hardware/edit.blade.php b/app/views/backend/hardware/edit.blade.php index 8585afc78f..07929eb029 100755 --- a/app/views/backend/hardware/edit.blade.php +++ b/app/views/backend/hardware/edit.blade.php @@ -9,6 +9,81 @@ @endif @parent @stop +{{-- Some room for the modals --}} + {{-- Page content --}} @@ -81,14 +156,14 @@
    @if (isset($selected_model)) - {{ Form::select('model_id', $model_list , $selected_model->id, array('class'=>'select2 model', 'style'=>'min-width:400px')) }} + {{ Form::select('model_id', $model_list , $selected_model->id, array('class'=>'select2 model', 'style'=>'min-width:400px','id' =>'model_select_id')) }} @else - {{ Form::select('model_id', $model_list , Input::old('model_id', $asset->model_id), array('class'=>'select2 model', 'style'=>'min-width:400px')) }} + {{ Form::select('model_id', $model_list , Input::old('model_id', $asset->model_id), array('class'=>'select2 model', 'style'=>'min-width:400px','id' =>'model_select_id')) }} @endif - Add new Model... + Add new Model... {{ $errors->first('model_id', '
    :message') }}
    @@ -117,7 +192,7 @@
    {{ 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... + Add new Supplier... {{ $errors->first('supplier_id', '
    :message') }}
    @@ -167,7 +242,7 @@
    {{ 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... + Add new Status...

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

    @@ -182,7 +257,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... + Add new User... {{ $errors->first('assigned_to', '
    :message') }}
    @@ -202,7 +277,7 @@
    {{ 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... + Add new Location... {{ $errors->first('rtd_location_id', '
    :message') }}
    @@ -293,26 +368,88 @@ } }; +$(function () { + var model,select; -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; + $('#createModal').on("show.bs.modal",function (event) { + var link = $(event.relatedTarget); + // data-dependency="model" data-select="model_select_id" + model=link.data("dependency"); + select=link.data("select"); - 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); + var modal = $(this); + modal.find('.modal-title').text('Add a new ' + model); + //modal.find('.modal-body').text("This is where I should be AJAX'ing in the contents for the new " +model+" that you'are about to add!"); + //use a spinner instead? + $('.dynamic-form-element').hide(); + function show_er(selector) { + $(selector).show().parent().show(); + } + show_er('#modal-name'); + switch(model) { + case 'model': + show_er('#modal-manufacturer_id'); + show_er('#modal-category_id'); + break; + + case 'user': + $('.dynamic-form-element').hide(); //we don't want a generic "name" + show_er("#modal-first_name"); + show_er("#modal-last_name"); + show_er("#modal-username"); + show_er("#modal-password"); + show_er("#modal-password_confirm"); + break; + + case 'location': + show_er('#modal-city'); + show_er('#modal-country'); + break; + + case 'supplier': + case 'status': + //do nothing, they just need 'name' + } + + console.warn("The Model is: "+model+" and the select is: "+select); }); - return false; -} - - + + $('#modal-save').on('click',function () { + var data={}; + console.warn("We are about to SAVE!!! for model: "+model+" and select ID: "+select); + $('.modal-body input:visible').each(function (index,elem) { + console.warn("["+index+"]: "+elem.id+" = "+$(elem).val()); + var bits=elem.id.split("-"); + if(bits[0]==="modal") { + data[bits[1]]=$(elem).val(); + } + }); + $('.modal-body select:visible').each(function (index,elem) { + var bits=elem.id.split("-"); + data[bits[1]]=$(elem).val(); + }); + + console.dir(data); + + $.post("{{Config::get('app.url')}}/api/"+model+"s",data,function (result) { + var id=result.id; + var name=result.name || (result.first_name+" "+result.last_name); + $('.modal-body input:visible').val(""); + $('#createModal').modal('hide'); + + console.warn("The select ID thing we're going for is: "+select); + var selector=document.getElementById(select); + selector.options[selector.length]=new Option(name,id); + selector.selectedIndex=selector.length-1; + $(selector).trigger("change"); + }).fail(function (result) { + //console.dir(result.responseJSON); + msg=result.responseJSON.error.message || result.responseJSON.error; + window.alert("Unable to add new "+model+" - error: "+msg); + }); + + }); +}); @stop