Fixed custom fields edit behavior with cutom format always selected

This commit is contained in:
snipe 2017-11-27 22:10:46 -08:00
parent c0f791cf13
commit cdfd720c65
2 changed files with 71 additions and 10 deletions

View file

@ -16,12 +16,17 @@ class CustomField extends Model
public static $PredefinedFormats=[ public static $PredefinedFormats=[
"ANY" => "", "ANY" => "",
"ALPHA" => "alpha", "ALPHA" => "alpha",
"ALPHA-DASH" => "alpha_dash",
"NUMERIC" => "numeric",
"ALPHA-NUMERIC" => "alpha_num",
"EMAIL" => "email", "EMAIL" => "email",
"DATE" => "date", "DATE" => "date",
"URL" => "url", "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", "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 = [ public $rules = [
@ -30,27 +35,54 @@ class CustomField extends Model
// This is confusing, since it's actually the custom fields table that // 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 // 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"; 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] [<snipe@snipe.net>]
* @since [v3.4]
* @return String
*/
public static function name_to_db_name($name) public static function name_to_db_name($name)
{ {
return "_snipeit_" . preg_replace("/[^a-zA-Z0-9]/", "_", strtolower($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] [<snipe@snipe.net>]
* @since [v3.4]
* @return Boolean
*/
public static function boot() public static function boot()
{ {
self::created(function ($custom_field) { 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())) { if (Schema::hasColumn(CustomField::$table_name, $custom_field->convertUnicodeDbSlug())) {
return false; return false;
} }
// Update the column name in the assets table
Schema::table(CustomField::$table_name, function ($table) use ($custom_field) { Schema::table(CustomField::$table_name, function ($table) use ($custom_field) {
$table->text($custom_field->convertUnicodeDbSlug())->nullable(); $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->db_column = $custom_field->convertUnicodeDbSlug();
$custom_field->save(); $custom_field->save();
}); });
@ -58,8 +90,9 @@ class CustomField extends Model
self::updating(function ($custom_field) { self::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 ($custom_field->isDirty("name")) {
if (Schema::hasColumn(CustomField::$table_name, $custom_field->convertUnicodeDbSlug())) { if (Schema::hasColumn(CustomField::$table_name, $custom_field->convertUnicodeDbSlug())) {
return true; return true;
} }
@ -113,11 +146,22 @@ class CustomField extends Model
return $this->db_column; return $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] [<snipe@snipe.net>]
* @since [v3.4]
* @return Array
*/
public function getFormatAttribute($value) public function getFormatAttribute($value)
{ {
foreach (self::$PredefinedFormats as $name => $pattern) { foreach (self::$PredefinedFormats as $name => $pattern) {
if ($pattern===$value) { \Log::debug($name.'=>'.$pattern);
if ($pattern === $value) {
return $name; return $name;
} }
} }
@ -168,6 +212,13 @@ class CustomField extends Model
return $result; return $result;
} }
/**
* Check whether the field is encrypted
*
* @author [A. Gianotto] [<snipe@snipe.net>]
* @since [v3.4]
* @return Boolean
*/
public function isFieldDecryptable($string) public function isFieldDecryptable($string)
{ {
if (($this->field_encrypted=='1') && ($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] [<snipe@snipe.net>]
* @since [v3.4]
* @return Boolean
*/
public function convertUnicodeDbSlug($original = null) public function convertUnicodeDbSlug($original = null)
{ {
$name = $original ? $original : $this->name; $name = $original ? $original : $this->name;

View file

@ -28,7 +28,6 @@
<div class="box box-default"> <div class="box box-default">
<div class="box-body"> <div class="box-body">
<!-- Name --> <!-- Name -->
<div class="form-group {{ $errors->has('name') ? ' has-error' : '' }}"> <div class="form-group {{ $errors->has('name') ? ' has-error' : '' }}">
<label for="name" class="col-md-4 control-label"> <label for="name" class="col-md-4 control-label">
@ -71,7 +70,7 @@
{{ trans('admin/custom_fields/general.field_format') }} {{ trans('admin/custom_fields/general.field_format') }}
</label> </label>
<div class="col-md-6 required"> <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>') !!} {!! $errors->first('format', '<span class="alert-msg"><i class="fa fa-times"></i> :message</span>') !!}
</div> </div>
</div> </div>
@ -82,10 +81,12 @@
{{ trans('admin/custom_fields/general.field_custom_format') }} {{ trans('admin/custom_fields/general.field_custom_format') }}
</label> </label>
<div class="col-md-6 required"> <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> <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>') !!} {!! $errors->first('custom_format', '<span class="alert-msg"><i class="fa fa-times"></i> :message</span>') !!}
</div> </div>
</div> </div>
@ -143,6 +144,7 @@
// If the custom_regex is ever NOT the last element in the format // If the custom_regex is ever NOT the last element in the format
// listbox, we will need to refactor this. // listbox, we will need to refactor this.
if ($('#custom_format').val()!='') { if ($('#custom_format').val()!='') {
// console.log('value is ' + $('#custom_format').val());
$('.format').prop('selectedIndex', $('.format')[0].options.length - 1); $('.format').prop('selectedIndex', $('.format')[0].options.length - 1);
} }