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 @@
+
+
+= Form::open(['url' => '/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 @@
+
+?>
+
+