A free open source IT asset/license management system
Fixed custom fields edit behavior with cutom format always selected
snipecdfd720c659d
diff --git a/app/Models/CustomField.php b/app/Models/CustomField.phpindex 696b0454b5..350b1e8408 100644--- a/app/Models/CustomField.php+++ b/app/Models/CustomField.php@@ -16,12 +16,17 @@ class CustomField extends Modelpublic static $PredefinedFormats=["ANY" => "","ALPHA" => "alpha",+ "ALPHA-DASH" => "alpha_dash",+ "NUMERIC" => "numeric",+ "ALPHA-NUMERIC" => "alpha_num","EMAIL" => "email","DATE" => "date","URL" => "url",- "NUMERIC" => "numeric",- "MAC" => "regex:/^[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}$/","IP" => "ip",+ "IPV4" => "ipv4",+ "IPV6" => "ipv6",+ "MAC" => "regex:/^[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}$/",+ "BOOLEAN" => "boolean",];public $rules = [@@ -30,27 +35,54 @@ class CustomField extends Model// This is confusing, since it's actually the custom fields table that// we're usually modifying, but since we alter the assets table, we have to- // say that here+ // say that here, otherwise the new fields get added onto the custom fields+ // table instead of the assets table.public static $table_name = "assets";++ /**+ * Convert the custom field's name property to a db-safe string.+ *+ * We could probably have used str_slug() here but not sure what it would+ * do with previously existing values. - @snipe+ *+ * @author [A. Gianotto] [<[email protected]>]+ * @since [v3.4]+ * @return String+ */public static function name_to_db_name($name){return "_snipeit_" . preg_replace("/[^a-zA-Z0-9]/", "_", strtolower($name));}+ /**+ * Set some boot methods for creating and updating.+ *+ * There is never ever a time when we wouldn't want to be updating those asset+ * column names and the values of the db column name in the custom fields table+ * if they have changed, so we handle that here so that we don't have to remember+ * to do it in the controllers.+ *+ * @author [A. Gianotto] [<[email protected]>]+ * @since [v3.4]+ * @return Boolean+ */public static function boot(){self::created(function ($custom_field) {- // column exists - nothing to do here+ // Column already exists on the assets table - nothing to do here.+ // This *shouldn't* happen in the wild.if (Schema::hasColumn(CustomField::$table_name, $custom_field->convertUnicodeDbSlug())) {return false;}+ // Update the column name in the assets tableSchema::table(CustomField::$table_name, function ($table) use ($custom_field) {$table->text($custom_field->convertUnicodeDbSlug())->nullable();});+ // Update the db_column property in the custom fields table$custom_field->db_column = $custom_field->convertUnicodeDbSlug();$custom_field->save();});@@ -58,8 +90,9 @@ class CustomField extends Modelself::updating(function ($custom_field) {- // Column already exists. Nothing to update.+ // Column already exists on the assets table - nothing to do here.if ($custom_field->isDirty("name")) {+if (Schema::hasColumn(CustomField::$table_name, $custom_field->convertUnicodeDbSlug())) {return true;}@@ -113,11 +146,22 @@ class CustomField extends Modelreturn $this->db_column;}- // mutators for 'format' attribute+ /**+ * Mutator for the 'format' attribute.+ *+ * This is used by the dropdown to store the laravel-specific+ * validator strings in the database but still return the+ * user-friendly text in the dropdowns, and in the custom fields display.+ *+ * @author [A. Gianotto] [<[email protected]>]+ * @since [v3.4]+ * @return Array+ */public function getFormatAttribute($value){foreach (self::$PredefinedFormats as $name => $pattern) {- if ($pattern===$value) {+ \Log::debug($name.'=>'.$pattern);+ if ($pattern === $value) {return $name;}}@@ -168,6 +212,13 @@ class CustomField extends Modelreturn $result;}+ /**+ * Check whether the field is encrypted+ *+ * @author [A. Gianotto] [<[email protected]>]+ * @since [v3.4]+ * @return Boolean+ */public function isFieldDecryptable($string){if (($this->field_encrypted=='1') && ($string!='')) {@@ -177,6 +228,14 @@ class CustomField extends Model}+ /**+ * Convert non-UTF-8 or weirdly encoded text into something that+ * won't break the database.+ *+ * @author [A. Gianotto] [<[email protected]>]+ * @since [v3.4]+ * @return Boolean+ */public function convertUnicodeDbSlug($original = null){$name = $original ? $original : $this->name;diff --git a/resources/views/custom_fields/fields/edit.blade.php b/resources/views/custom_fields/fields/edit.blade.phpindex ff57ba2753..4422d33ca3 100644--- a/resources/views/custom_fields/fields/edit.blade.php+++ b/resources/views/custom_fields/fields/edit.blade.php@@ -28,7 +28,6 @@<div class="box box-default"><div class="box-body">-<!-- Name --><div class="form-group {{ $errors->has('name') ? ' has-error' : '' }}"><label for="name" class="col-md-4 control-label">@@ -71,7 +70,7 @@{{ trans('admin/custom_fields/general.field_format') }}</label><div class="col-md-6 required">- {{ Form::select("format",\App\Helpers\Helper::predefined_formats(),"ANY", array('class'=>'format select2 form-control')) }}+ {{ Form::select("format",\App\Helpers\Helper::predefined_formats(), $field->format, array('class'=>'format select2 form-control')) }}{!! $errors->first('format', '<span class="alert-msg"><i class="fa fa-times"></i> :message</span>') !!}</div></div>@@ -82,10 +81,12 @@{{ trans('admin/custom_fields/general.field_custom_format') }}</label><div class="col-md-6 required">- {{ Form::text('custom_format', Input::old('custom_format', (($field->format!='') && ($field->format!='ANY')) ? $field->format : ''), array('class' => 'form-control', 'id' => 'custom_format', 'placeholder'=>'regex:/^[0-9]{15}$/')) }}++ {{ Form::text('custom_format', Input::old('custom_format', (($field->format!='') && (stripos($field->format,'regex')===0)) ? $field->format : ''), array('class' => 'form-control', 'id' => 'custom_format', 'placeholder'=>'regex:/^[0-9]{15}$/')) }}<p class="help-block">{!! trans('admin/custom_fields/general.field_custom_format_help') !!}</p>{!! $errors->first('custom_format', '<span class="alert-msg"><i class="fa fa-times"></i> :message</span>') !!}+</div></div>@@ -143,6 +144,7 @@// If the custom_regex is ever NOT the last element in the format// listbox, we will need to refactor this.if ($('#custom_format').val()!='') {+ // console.log('value is ' + $('#custom_format').val());$('.format').prop('selectedIndex', $('.format')[0].options.length - 1);}