diff --git a/app/Models/CustomField.php b/app/Models/CustomField.php index 696b0454b5..350b1e8408 100644 --- a/app/Models/CustomField.php +++ b/app/Models/CustomField.php @@ -16,12 +16,17 @@ class CustomField extends Model public 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] [] + * @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] [] + * @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 table Schema::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 Model 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 (Schema::hasColumn(CustomField::$table_name, $custom_field->convertUnicodeDbSlug())) { return true; } @@ -113,11 +146,22 @@ class CustomField extends Model 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] [] + * @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 Model return $result; } + /** + * Check whether the field is encrypted + * + * @author [A. Gianotto] [] + * @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] [] + * @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.php index 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 @@
-
- {{ 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', ' :message') !!}
@@ -82,10 +81,12 @@ {{ trans('admin/custom_fields/general.field_custom_format') }}
- {{ 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}$/')) }}

{!! trans('admin/custom_fields/general.field_custom_format_help') !!}

{!! $errors->first('custom_format', ' :message') !!} +
@@ -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); }