diff --git a/app/Helpers/Helper.php b/app/Helpers/Helper.php index b8ef8ccda6..59f7e24a66 100644 --- a/app/Helpers/Helper.php +++ b/app/Helpers/Helper.php @@ -225,8 +225,9 @@ class Helper */ public static function predefined_formats() { - $keys = array_keys(CustomField::$PredefinedFormats); + $keys = array_keys(CustomField::PREDEFINED_FORMATS); $stuff = array_combine($keys, $keys); + return $stuff; } diff --git a/app/Http/Controllers/CustomFieldsController.php b/app/Http/Controllers/CustomFieldsController.php index e169414276..8d15b22cb5 100644 --- a/app/Http/Controllers/CustomFieldsController.php +++ b/app/Http/Controllers/CustomFieldsController.php @@ -1,12 +1,13 @@ get(); $fields = CustomField::with("fieldset")->get(); + return view("custom_fields.index")->with("custom_fieldsets", $fieldsets)->with("custom_fields", $fields); } @@ -53,7 +55,9 @@ class CustomFieldsController extends Controller { $this->authorize('create', CustomField::class); - return view("custom_fields.fields.edit")->with('field', new CustomField()); + return view("custom_fields.fields.edit",[ + 'predefinedFormats' => Helper::predefined_formats() + ])->with('field', new CustomField()); } @@ -158,7 +162,16 @@ class CustomFieldsController extends Controller $this->authorize('update', $field); - return view("custom_fields.fields.edit")->with('field', $field); + $customFormat = ''; + if((stripos($field->format, 'regex') === 0) && ($field->format !== CustomField::PREDEFINED_FORMATS['MAC'])) { + $customFormat = $field->format; + } + + return view("custom_fields.fields.edit",[ + 'field' => $field, + 'customFormat' => $customFormat, + 'predefinedFormats' => Helper::predefined_formats() + ]); } @@ -176,17 +189,17 @@ class CustomFieldsController extends Controller public function update(CustomFieldRequest $request, $id) { $field = CustomField::find($id); - + $this->authorize('update', $field); - $field->name = e($request->get("name")); - $field->element = e($request->get("element")); - $field->field_values = e($request->get("field_values")); - $field->user_id = Auth::id(); - $field->help_text = $request->get("help_text"); + $field->name = e($request->get("name")); + $field->element = e($request->get("element")); + $field->field_values = e($request->get("field_values")); + $field->user_id = Auth::id(); + $field->help_text = $request->get("help_text"); $field->show_in_email = $request->get("show_in_email", 0); - if (!in_array(Input::get('format'), array_keys(CustomField::$PredefinedFormats))) { + if ($request->get('format') == 'CUSTOM REGEX') { $field->format = e($request->get("custom_format")); } else { $field->format = e($request->get("format")); diff --git a/app/Models/CustomField.php b/app/Models/CustomField.php index 0d2565db16..eb3da01715 100644 --- a/app/Models/CustomField.php +++ b/app/Models/CustomField.php @@ -15,27 +15,32 @@ class CustomField extends Model use ValidatingTrait, UniqueUndeletedTrait; + /** + * Custom field predfined formats + * + * @var array + */ + const PREDEFINED_FORMATS = [ + 'ANY' => '', + 'CUSTOM REGEX' => '', + 'ALPHA' => 'alpha', + 'ALPHA-DASH' => 'alpha_dash', + 'NUMERIC' => 'numeric', + 'ALPHA-NUMERIC' => 'alpha_num', + 'EMAIL' => 'email', + 'DATE' => 'date', + 'URL' => 'url', + '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 $guarded = [ "id" ]; - public static $PredefinedFormats = [ - "ANY" => "", - "CUSTOM REGEX" => "", - "ALPHA" => "alpha", - "ALPHA-DASH" => "alpha_dash", - "NUMERIC" => "numeric", - "ALPHA-NUMERIC" => "alpha_num", - "EMAIL" => "email", - "DATE" => "date", - "URL" => "url", - "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", - ]; - /** * Validation rules. * At least empty array must be provided if using ValidatingTrait. @@ -248,11 +253,12 @@ class CustomField extends Model */ public function getFormatAttribute($value) { - foreach (self::$PredefinedFormats as $name => $pattern) { + foreach (self::PREDEFINED_FORMATS as $name => $pattern) { if ($pattern === $value || $name === $value) { return $name; } } + return $value; } @@ -265,8 +271,8 @@ class CustomField extends Model */ public function setFormatAttribute($value) { - if (isset(self::$PredefinedFormats[$value])) { - $this->attributes['format']=self::$PredefinedFormats[$value]; + if (isset(self::PREDEFINED_FORMATS[$value])) { + $this->attributes['format']=self::PREDEFINED_FORMATS[$value]; } else { $this->attributes['format']=$value; } @@ -354,9 +360,28 @@ class CustomField extends Model Rule::in(['text', 'listbox']) ], 'format' => [ - Rule::in(array_merge(array_keys(CustomField::$PredefinedFormats), CustomField::$PredefinedFormats)) + Rule::in(array_merge(array_keys(CustomField::PREDEFINED_FORMATS), CustomField::PREDEFINED_FORMATS)) ], 'field_encrypted' => "nullable|boolean" ]; } + + /** + * Check to see if there is a custom regex format type + * @see https://github.com/snipe/snipe-it/issues/5896 + * + * @author Wes Hulette + * + * @since 5.0.0 + * + * @return string + */ + public function getFormatType() + { + if(stripos($this->format,'regex') === 0 && ($this->format !== self::PREDEFINED_FORMATS['MAC'])) { + return 'CUSTOM REGEX'; + } + + return $this->format; + } } diff --git a/database/migrations/2015_09_22_003413_migrate_mac_address.php b/database/migrations/2015_09_22_003413_migrate_mac_address.php index ff31042f9a..6345773ca1 100644 --- a/database/migrations/2015_09_22_003413_migrate_mac_address.php +++ b/database/migrations/2015_09_22_003413_migrate_mac_address.php @@ -21,7 +21,7 @@ class MigrateMacAddress extends Migration { } $macid=DB::table('custom_fields')->insertGetId([ 'name' => "MAC Address", - 'format' => \App\Models\CustomField::$PredefinedFormats['MAC'], + 'format' => \App\Models\CustomField::PREDEFINED_FORMATS['MAC'], 'element'=>'text']); if(!$macid) { throw new Exception("Can't save MAC Custom field: $macid"); diff --git a/resources/views/custom_fields/fields/edit.blade.php b/resources/views/custom_fields/fields/edit.blade.php index fb109c7cc0..f426a000f9 100644 --- a/resources/views/custom_fields/fields/edit.blade.php +++ b/resources/views/custom_fields/fields/edit.blade.php @@ -1,4 +1,9 @@ +@php + use App\Models\CustomField; +@endphp + @extends('layouts/default') + {{-- Page title --}} @section('title') {{ trans('admin/custom_fields/general.custom_fields') }} @@ -70,19 +75,18 @@ {{ trans('admin/custom_fields/general.field_format') }}
- {{ Form::select("format",\App\Helpers\Helper::predefined_formats(), $field->format, array('class'=>'format select2 form-control')) }} + {{ Form::select("format",$predefinedFormats, $field->getFormatType(), + array('class'=>'format select2 form-control')) }} {!! $errors->first('format', ' :message') !!}
- @stop @section('moar_scripts')