From 5890d265451d6bccf363d3034f8b9ed6cb15783c Mon Sep 17 00:00:00 2001 From: Brady Wetherington Date: Thu, 22 Oct 2015 00:38:07 -0700 Subject: [PATCH] Terribly rough Custom Fields creation --- app/controllers/CustomFieldsController.php | 133 ++++++++++++++++++ app/controllers/admin/AssetsController.php | 17 ++- app/controllers/admin/ModelsController.php | 3 +- ...26_create_custom_field_custom_fieldset.php | 2 +- .../2015_09_22_003413_migrate_mac_address.php | 42 +++++- app/helpers.php | 11 ++ app/models/CustomField.php | 66 ++++----- app/models/CustomFieldset.php | 16 ++- app/models/Model.php | 6 +- app/routes.php | 7 + .../backend/custom_fields/create.blade.php | 6 + .../custom_fields/create_field.blade.php | 9 ++ .../backend/custom_fields/index.blade.php | 18 +++ .../backend/custom_fields/show.blade.php | 12 ++ app/views/backend/hardware/edit.blade.php | 17 ++- app/views/backend/hardware/index.blade.php | 6 +- app/views/backend/hardware/view.blade.php | 17 ++- app/views/backend/models/edit.blade.php | 4 +- 18 files changed, 330 insertions(+), 62 deletions(-) create mode 100644 app/controllers/CustomFieldsController.php create mode 100644 app/views/backend/custom_fields/create.blade.php create mode 100644 app/views/backend/custom_fields/create_field.blade.php create mode 100644 app/views/backend/custom_fields/index.blade.php create mode 100644 app/views/backend/custom_fields/show.blade.php diff --git a/app/controllers/CustomFieldsController.php b/app/controllers/CustomFieldsController.php new file mode 100644 index 0000000000..c56156ee37 --- /dev/null +++ b/app/controllers/CustomFieldsController.php @@ -0,0 +1,133 @@ +with("custom_fieldsets",CustomFieldset::all())->with("custom_fields",CustomField::all()); + } + + + /** + * Show the form for creating a new resource. + * + * @return Response + */ + public function getCreate() + { + // + return View::make("backend.custom_fields.create"); + } + + + /** + * Store a newly created resource in storage. + * + * @return Response + */ + public function postIndex() + { + // + $cfset=new CustomFieldset(["name" => Input::get("name")]); + $cfset->save(); + return Redirect::to("/custom_fieldsets/".$cfset->id); //redirect(["asdf" => "alskdjf"]); + + } + + public function postAssociate($id) + { + print "ID is: $id"; + $set=CustomFieldset::find($id); + $results=$set->fields()->attach(Input::get('field_id'),["required" => (Input::get('required') == "on"),"order" => Input::get('order')]); + //return "I assoced it. Results: $results"; + return Redirect::to("/custom_fieldsets/".$id); //redirect(["asdf" => "alskdjf"]); + } + + public function getCreateField() + { + return View::make("backend.custom_fields.create_field"); + } + + public function postCreateField() + { + $field=new CustomField(["name" => Input::get("name"),"element" => Input::get("element")]); + if(!in_array(Input::get('format'),["ALPHA","NUMERIC","MAC","IP"])) { + $field->format=Input::get("custom_format"); + } else { + $field->format=Input::get('format'); + } + $results=$field->save(); + //return "postCreateField: $results"; + if ($results) { + return Redirect::to("/custom_fieldsets/"); + } else { + return Redirect::to("/custom_fieldsets/create-field"); + } + } + + /** + * Display the specified resource. + * + * @param int $id + * @return Response + */ + public function missingMethod($parameters = array()) + { + $id=$parameters[0]; + $cfset=CustomFieldset::find($id); + + //print_r($parameters); + // + $maxid=0; + foreach($cfset->fields AS $field) { + if($field->pivot->order > $maxid) { + $maxid=$field->pivot->order; + } + } + return View::make("backend.custom_fields.show")->with("custom_fieldset",$cfset)->with("maxid",$maxid+1); + } + + + /** + * Show the form for editing the specified resource. + * + * @param int $id + * @return Response + */ + public function edit($id) + { + // + } + + + /** + * Update the specified resource in storage. + * + * @param int $id + * @return Response + */ + public function update($id) + { + // + } + + + /** + * Remove the specified resource from storage. + * + * @param int $id + * @return Response + */ + public function destroy($id) + { + // + } + + +} diff --git a/app/controllers/admin/AssetsController.php b/app/controllers/admin/AssetsController.php index 4c74d11718..ad516e3cf8 100755 --- a/app/controllers/admin/AssetsController.php +++ b/app/controllers/admin/AssetsController.php @@ -242,9 +242,20 @@ class AssetsController extends AdminController // Redirect to the asset management page with error return Redirect::to('hardware')->with('error', Lang::get('admin/hardware/message.does_not_exist')); } + + $input=Input::all(); + if($this->model->fieldset) + { + foreach($this->model->fieldset->fields AS $field) { + $input[$field->db_column_name()]=$input->fields[$field->db_column_name()]; + } + unset($input['fields']); + } //attempt to validate - $validator = Validator::make(Input::all(), $asset->validationRules($assetId)); + $validator = Validator::make($input, $asset->validationRules($assetId) + $this->fieldset->validation_rules()); + + $custom_errors=[]; if ($validator->fails()) { @@ -264,7 +275,7 @@ class AssetsController extends AdminController if (e(Input::get('warranty_months')) == '') { $asset->warranty_months = NULL; } else { - $asset->warranty_months = e(Input::get('warranty_months')); + $asset->warranty_months = e(Input::get('warranty_months')); } if (e(Input::get('purchase_cost')) == '') { @@ -1066,7 +1077,7 @@ class AssetsController extends AdminController public function getDatatable($status = null) { - $assets = Asset::with('model','assigneduser','assigneduser.userloc','assetstatus','defaultLoc','assetlog','model','model.category')->Hardware()->select(array('id', 'name','model_id','assigned_to','asset_tag','serial','status_id','purchase_date','deleted_at','rtd_location_id','notes','order_number','mac_address','warranty_months')); + $assets = Asset::with('model','assigneduser','assigneduser.userloc','assetstatus','defaultLoc','assetlog','model','model.category')->Hardware()->select(array('id', 'name','model_id','assigned_to','asset_tag','serial','status_id','purchase_date','deleted_at','rtd_location_id','notes','order_number','warranty_months')); switch ($status) { diff --git a/app/controllers/admin/ModelsController.php b/app/controllers/admin/ModelsController.php index efc4cc6534..3b7a96e6e3 100755 --- a/app/controllers/admin/ModelsController.php +++ b/app/controllers/admin/ModelsController.php @@ -236,7 +236,8 @@ class ModelsController extends AdminController $model->modelno = e(Input::get('modelno')); $model->manufacturer_id = e(Input::get('manufacturer_id')); $model->category_id = e(Input::get('category_id')); - $model->show_mac_address = e(Input::get('show_mac_address', '0')); + //$model->show_mac_address = e(Input::get('show_mac_address', '0')); + $model->fieldset_id = e(Input::get('custom_fieldset')); if (Input::file('image')) { $image = Input::file('image'); diff --git a/app/database/migrations/2015_09_21_235926_create_custom_field_custom_fieldset.php b/app/database/migrations/2015_09_21_235926_create_custom_field_custom_fieldset.php index ef26b6f181..e219f2ca86 100644 --- a/app/database/migrations/2015_09_21_235926_create_custom_field_custom_fieldset.php +++ b/app/database/migrations/2015_09_21_235926_create_custom_field_custom_fieldset.php @@ -29,7 +29,7 @@ class CreateCustomFieldCustomFieldset extends Migration { */ public function down() { - Schema::drop('custom_field_custom_fieldsets'); + Schema::drop('custom_field_custom_fieldset'); } } diff --git a/app/database/migrations/2015_09_22_003413_migrate_mac_address.php b/app/database/migrations/2015_09_22_003413_migrate_mac_address.php index 92f3f41d47..75a07e46fb 100644 --- a/app/database/migrations/2015_09_22_003413_migrate_mac_address.php +++ b/app/database/migrations/2015_09_22_003413_migrate_mac_address.php @@ -12,6 +12,9 @@ class MigrateMacAddress extends Migration { */ public function up() { + DB::getDoctrineSchemaManager()->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string'); + + // //create empty 'default' fieldset' //$f1=Fieldset::create([name => "Default Asset"]); @@ -19,18 +22,41 @@ class MigrateMacAddress extends Migration { if(!$f2->save()) { throw new Exception("couldn't save customfieldset"); } - $mac=new CustomField(['name' => "MAC Address",'format' =>'MAC','element'=>'text']); - if(!$mac->save()) { - throw new Exception("Mac ID: $macid"); + $macid=DB::table('custom_fields')->insertGetId([ + 'name' => "MAC Address", + 'format' => CustomField::$PredefinedFormats['MAC'], + 'element'=>'text']); + if(!$macid) { + throw new Exception("Can't save MAC Custom field: $macid"); } - $f2->fields()->save($mac,['required' => false, 'order' => 1]); + $f2->fields()->attach($macid,['required' => false, 'order' => 1]); //$f2->push(); - Model::where(["show_mac_address" => true])->update(["fieldset_id"=>$f2->id]); - Schema::table("models",function (Blueprint $table) { + //up to *HERE* works just fine + print "THIS IS FINE!"; + // $ans2=Schema::table("assets",function (Blueprint $table) { + // $table->renameColumn('mac_address','_snipeit_mac_address'); + // }); + DB::statement("ALTER TABLE assets CHANGE mac_address _snipeit_mac_address varchar(255)"); + // print "NOTHING WORKS"; + // if(!$ans2) { + // throw new Exception("Couldn't rename mac_address collumn in Assets table"); + // } + $ans=Schema::table("models",function (Blueprint $table) { $table->renameColumn('show_mac_address','deprecated_mac_address'); }); + print "Does this even ahppen"; + // if(!$ans) { + // throw new Exception("couldn't rename show_mac_address column in Models table"); + // } + // $shit=Schema::table("assets",function (Blueprint $table) { + // $table->renmaeColmnu("fuck you you fucking piece of shit","die in a fucking fire you asshole"); + // }); + // print "seriously"; + // if(!$shit) { + // throw new Exception("something taht should've failed failed. Good."); + // } } /** @@ -47,6 +73,10 @@ class MigrateMacAddress extends Migration { Schema::table("models",function(Blueprint $table) { $table->renameColumn("deprecated_mac_address","show_mac_address"); }); + DB::statement("ALTER TABLE assets CHANGE _snipeit_mac_address mac_address varchar(255)"); + // Schema::table("assets",function (Blueprint $table) { + // $table->renameColumn('_snipeit_mac_address','mac_address'); + // }); } } diff --git a/app/helpers.php b/app/helpers.php index 257a098851..ab5d24d2b8 100755 --- a/app/helpers.php +++ b/app/helpers.php @@ -89,6 +89,17 @@ function usersList() { return $users_list; } +function customFieldsetList() { + $customfields=CustomFieldset::lists('name','id'); + return array('' => Lang::get('general.no_custom_field')) + $customfields; +} + +function predefined_formats() { + $keys=array_keys(CustomField::$PredefinedFormats); + $stuff=array_combine($keys,$keys); + return $stuff+["" => "Custom Format..."]; +} + function barcodeDimensions ($barcode_type = 'QRCODE') { if ($barcode_type == 'C128') { $size['height'] = '-1'; diff --git a/app/models/CustomField.php b/app/models/CustomField.php index 1ddc878bb3..e7816f67b2 100644 --- a/app/models/CustomField.php +++ b/app/models/CustomField.php @@ -14,36 +14,40 @@ class CustomField extends Elegant public static function name_to_db_name($name) { - return preg_replace("/\s/","_",strtolower($name)); + return "_snipeit_".preg_replace("/\s/","_",strtolower($name)); } - public static function creating($custom_field) { - if(in_array($custom_field->db_column_name(),Schema::getColumnListing(CustomField::$table_name))) { - //field already exists when making a new custom field; fail. - return false; - } - return db::exec("ALTER TABLE ".CustomField::$table_name." ADD COLUMN (".$custom_field->db_column_name()." TEXT)"); - } - - public static function updating($custom_field) { - //print(" SAVING CALLBACK FIRING!!!!! "); - if($custom_field->isDirty("name")) { - //print("DIRTINESS DETECTED!"); - //$fields=array_keys($custom_field->getAttributes()); - ; - //add timestamp fields, add id column - //array_push($fields,$custom_field->getKeyName()); - /*if($custom_field::timestamps) { - - }*/ - //print("Fields are: ".print_r($fields,true)); - if(in_array($custom_field->db_column_name(),Schema::getColumnListing(CustomField::$table_name))) { - //field already exists when renaming a custom field + public static function boot() + { + self::creating(function ($custom_field) { + + if(in_array($custom_field->db_column_name(),Schema::getColumnListing(DB::getTablePrefix().CustomField::$table_name))) { + //field already exists when making a new custom field; fail. return false; } - return db::exec("UPDATE ".CustomField::$table_name." RENAME ".self::name_to_db_name($custom_field->get_original("name"))." TO ".$custom_field->db_column_name()); - } - return true; + return DB::statement("ALTER TABLE ".DB::getTablePrefix().CustomField::$table_name." ADD COLUMN (".$custom_field->db_column_name()." TEXT)"); + }); + + self::updating(function ($custom_field) { + //print(" SAVING CALLBACK FIRING!!!!! "); + if($custom_field->isDirty("name")) { + //print("DIRTINESS DETECTED!"); + //$fields=array_keys($custom_field->getAttributes()); + //; + //add timestamp fields, add id column + //array_push($fields,$custom_field->getKeyName()); + /*if($custom_field::timestamps) { + + }*/ + //print("Fields are: ".print_r($fields,true)); + if(in_array($custom_field->db_column_name(),Schema::getColumnListing(CustomField::$table_name))) { + //field already exists when renaming a custom field + return false; + } + return DB::statement("UPDATE ".CustomField::$table_name." RENAME ".self::name_to_db_name($custom_field->get_original("name"))." TO ".$custom_field->db_column_name()); + } + return true; + }); } /*public static function boot() { @@ -56,13 +60,9 @@ class CustomField extends Elegant }*/ public function fieldset() { - return $this->belongsToMany('Fieldset'); //?!?!?!?!?!? + return $this->belongsToMany('CustomFieldset'); //?!?!?!?!?!? } - - public $rules=[ - 'name' => 'unique_column' - ]; - + //public function //need helper to go from regex->English @@ -89,7 +89,7 @@ class CustomField extends Elegant } public function setFormatAttribute($value) { - if(self::$PredefinedFormats[$value]) { + if(isset(self::$PredefinedFormats[$value])) { $this->attributes['format']=self::$PredefinedFormats[$value]; } else { $this->attributes['format']=$value; diff --git a/app/models/CustomFieldset.php b/app/models/CustomFieldset.php index 7cbd749404..c368ed8fe8 100644 --- a/app/models/CustomFieldset.php +++ b/app/models/CustomFieldset.php @@ -4,7 +4,21 @@ class CustomFieldset extends Elegant protected $guarded=["id"]; public $timestamps=false; public function fields() { - return $this->belongsToMany('CustomField')->withPivot(["required","order"]); + return $this->belongsToMany('CustomField')->withPivot(["required","order"])->orderBy("pivot_order"); + } + + public function validation_rules() + { + $rules=[]; + foreach($this->fields AS $field) { + $rule=[]; + if($field->pivot->required) { + $rule[]="required"; + } + array_push($rule,"regex",$field->format); + $rules[$field->db_column_name()]=$rule; + } + return $rules; } //requiredness goes *here* diff --git a/app/models/Model.php b/app/models/Model.php index 32695c38fb..039a771473 100755 --- a/app/models/Model.php +++ b/app/models/Model.php @@ -43,11 +43,9 @@ class Model extends Elegant return $this->belongsTo('Manufacturer','manufacturer_id'); } - public function custom_fieldset() + public function fieldset() { - // $foo=new CustomField(); - // $foo2=new CustomFieldset(); - return $this->belongsTo('custom_fieldset','fieldset_id'); + return $this->belongsTo('CustomFieldset','fieldset_id'); } /** diff --git a/app/routes.php b/app/routes.php index d69bce3ba6..d872f6e214 100755 --- a/app/routes.php +++ b/app/routes.php @@ -108,6 +108,13 @@ } ); } ); + + # Custom fieldset + //Route::get('/custom_fieldsets/{id}','CustomFieldsController@show'); + //Route::get('/custom_fieldsets/create','CustomFieldsController@getCreate'); + Route::post('/custom_fieldsets/{id}/associate','CustomFieldsController@postAssociate'); + Route::controller('/custom_fieldsets','CustomFieldsController' ); + /* |-------------------------------------------------------------------------- | Asset Routes diff --git a/app/views/backend/custom_fields/create.blade.php b/app/views/backend/custom_fields/create.blade.php new file mode 100644 index 0000000000..b1cb366c29 --- /dev/null +++ b/app/views/backend/custom_fields/create.blade.php @@ -0,0 +1,6 @@ + + + '/custom_fieldsets']) ?> + Name:
+ + diff --git a/app/views/backend/custom_fields/create_field.blade.php b/app/views/backend/custom_fields/create_field.blade.php new file mode 100644 index 0000000000..1f42eace24 --- /dev/null +++ b/app/views/backend/custom_fields/create_field.blade.php @@ -0,0 +1,9 @@ + + +{{ Form::open(["url" =>"/custom_fieldsets/create-field"])}} +Name: {{ Form::text("name")}}
+type: {{ Form::select("element",["text" => "Text Box"])}}
+format: {{ Form::select("format",predefined_formats(),"ALPHA") }} +Custom Format (if selected): {{ Form::text("custom_format") }}
+ +{{ Form::close() }} diff --git a/app/views/backend/custom_fields/index.blade.php b/app/views/backend/custom_fields/index.blade.php new file mode 100644 index 0000000000..c8bd7f66b9 --- /dev/null +++ b/app/views/backend/custom_fields/index.blade.php @@ -0,0 +1,18 @@ + + +

Fieldsets

+ + +New Fieldset
+

Custom Field Definitions

+ +New Field diff --git a/app/views/backend/custom_fields/show.blade.php b/app/views/backend/custom_fields/show.blade.php new file mode 100644 index 0000000000..a3a65852cb --- /dev/null +++ b/app/views/backend/custom_fields/show.blade.php @@ -0,0 +1,12 @@ +

Fieldset

+{{{ $custom_fieldset->name }}} + + +{{ Form::open(['url' => '/custom_fieldsets/'.$custom_fieldset->id.'/associate']) }} +{{ Form::checkbox("required","on") }}Required? +{{ Form::text("order",$maxid)}} +{{ Form::select("field_id",["" => "Add New Field to Fieldset"] + CustomField::lists("name","id"),"",["onchange" => "document.forms[0].submit()"]) }} diff --git a/app/views/backend/hardware/edit.blade.php b/app/views/backend/hardware/edit.blade.php index d313f3e418..0f972470a2 100755 --- a/app/views/backend/hardware/edit.blade.php +++ b/app/views/backend/hardware/edit.blade.php @@ -175,7 +175,7 @@ - + + + @if($asset->model->fieldset) +

Custom Fields

+ @foreach($asset->model->fieldset->fields AS $field) +
+ +
+ +
+
+ @endforeach + @endif diff --git a/app/views/backend/hardware/index.blade.php b/app/views/backend/hardware/index.blade.php index 831bd6cbc8..1daacb59bd 100755 --- a/app/views/backend/hardware/index.blade.php +++ b/app/views/backend/hardware/index.blade.php @@ -66,7 +66,7 @@ Lang::get('general.category'), Lang::get('admin/hardware/table.eol'), Lang::get('general.notes'), - Lang::get('admin/hardware/form.mac_address'), + "REMOVEME", Lang::get('admin/hardware/form.order'), Lang::get('admin/hardware/table.checkout_date'), Lang::get('admin/hardware/table.change'), @@ -89,8 +89,8 @@ ), 'sAjaxSource'=> route('api.hardware.list', array(''=>Input::get('status'),'order_number'=>Input::get('order_number'))), 'dom' =>'CT<"clear">lfrtip', - 'colVis'=> array('showAll'=>'Show All','restore'=>'Restore','exclude'=>array(0,13,14),'activate'=>'mouseover'), - 'columnDefs'=> array(array('visible'=>false,'targets'=>array(7,8,9)),array('orderable'=>false,'targets'=>array(0,13,14))), + 'colVis'=> array('showAll'=>'Show All','restore'=>'Restore','exclude'=>array(0,12,13),'activate'=>'mouseover'), + 'columnDefs'=> array(array('visible'=>false,'targets'=>array(7,8,9)),array('orderable'=>false,'targets'=>array(0,12,13))), 'order'=>array(array(1,'asc')), ) ) diff --git a/app/views/backend/hardware/view.blade.php b/app/views/backend/hardware/view.blade.php index f02ba9842b..5ace05266c 100755 --- a/app/views/backend/hardware/view.blade.php +++ b/app/views/backend/hardware/view.blade.php @@ -65,12 +65,6 @@ @endif - @if ($asset->mac_address!='') -
@lang('admin/hardware/form.mac_address'): - {{{ $asset->mac_address }}} -
- @endif - @if ($asset->model->manufacturer)
@lang('admin/hardware/form.manufacturer'): @@ -159,6 +153,17 @@
@endif + @if ($asset->model->fieldset->sortBy('custom_field_custom_fieldset.order')) +
+
FIELDSET: + {{{ $asset->model->fieldset->name }}}
+ @foreach($asset->model->fieldset->fields as $field) +
{{{ $field->name }}}: + {{{ $asset->{$field->db_column_name()} }}} +
+ @endforeach +
+ @endif diff --git a/app/views/backend/models/edit.blade.php b/app/views/backend/models/edit.blade.php index cc336759b2..47e2336bd9 100755 --- a/app/views/backend/models/edit.blade.php +++ b/app/views/backend/models/edit.blade.php @@ -110,8 +110,8 @@