@@ -16,12 +16,17 @@ class CustomField extends Model
1616 public static $ PredefinedFormats =[
1717 "ANY " => "" ,
1818 "ALPHA " => "alpha " ,
19+ "ALPHA-DASH " => "alpha_dash " ,
20+ "NUMERIC " => "numeric " ,
21+ "ALPHA-NUMERIC " => "alpha_num " ,
1922 "EMAIL " => "email " ,
2023 "DATE " => "date " ,
2124 "URL " => "url " ,
22- "NUMERIC " => "numeric " ,
23- "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}$/ " ,
2425 "IP " => "ip " ,
26+ "IPV4 " => "ipv4 " ,
27+ "IPV6 " => "ipv6 " ,
28+ "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}$/ " ,
29+ "BOOLEAN " => "boolean " ,
2530 ];
2631
2732 public $ rules = [
@@ -30,36 +35,64 @@ class CustomField extends Model
3035
3136 // This is confusing, since it's actually the custom fields table that
3237 // we're usually modifying, but since we alter the assets table, we have to
33- // say that here
38+ // say that here, otherwise the new fields get added onto the custom fields
39+ // table instead of the assets table.
3440 public static $ table_name = "assets " ;
3541
42+
43+ /**
44+ * Convert the custom field's name property to a db-safe string.
45+ *
46+ * We could probably have used str_slug() here but not sure what it would
47+ * do with previously existing values. - @snipe
48+ *
49+ * @author [A. Gianotto] [<[email protected] >] 50+ * @since [v3.4]
51+ * @return String
52+ */
3653 public static function name_to_db_name ($ name )
3754 {
3855 return "_snipeit_ " . preg_replace ("/[^a-zA-Z0-9]/ " , "_ " , strtolower ($ name ));
3956 }
4057
58+ /**
59+ * Set some boot methods for creating and updating.
60+ *
61+ * There is never ever a time when we wouldn't want to be updating those asset
62+ * column names and the values of the db column name in the custom fields table
63+ * if they have changed, so we handle that here so that we don't have to remember
64+ * to do it in the controllers.
65+ *
66+ * @author [A. Gianotto] [<[email protected] >] 67+ * @since [v3.4]
68+ * @return Boolean
69+ */
4170 public static function boot ()
4271 {
4372 self ::created (function ($ custom_field ) {
4473
45- // column exists - nothing to do here
74+ // Column already exists on the assets table - nothing to do here.
75+ // This *shouldn't* happen in the wild.
4676 if (Schema::hasColumn (CustomField::$ table_name , $ custom_field ->convertUnicodeDbSlug ())) {
4777 return false ;
4878 }
4979
80+ // Update the column name in the assets table
5081 Schema::table (CustomField::$ table_name , function ($ table ) use ($ custom_field ) {
5182 $ table ->text ($ custom_field ->convertUnicodeDbSlug ())->nullable ();
5283 });
5384
85+ // Update the db_column property in the custom fields table
5486 $ custom_field ->db_column = $ custom_field ->convertUnicodeDbSlug ();
5587 $ custom_field ->save ();
5688 });
5789
5890
5991 self ::updating (function ($ custom_field ) {
6092
61- // Column already exists. Nothing to update .
93+ // Column already exists on the assets table - nothing to do here .
6294 if ($ custom_field ->isDirty ("name " )) {
95+
6396 if (Schema::hasColumn (CustomField::$ table_name , $ custom_field ->convertUnicodeDbSlug ())) {
6497 return true ;
6598 }
@@ -113,11 +146,22 @@ public function db_column_name()
113146 return $ this ->db_column ;
114147 }
115148
116- // mutators for 'format' attribute
149+ /**
150+ * Mutator for the 'format' attribute.
151+ *
152+ * This is used by the dropdown to store the laravel-specific
153+ * validator strings in the database but still return the
154+ * user-friendly text in the dropdowns, and in the custom fields display.
155+ *
156+ * @author [A. Gianotto] [<[email protected] >] 157+ * @since [v3.4]
158+ * @return Array
159+ */
117160 public function getFormatAttribute ($ value )
118161 {
119162 foreach (self ::$ PredefinedFormats as $ name => $ pattern ) {
120- if ($ pattern ===$ value ) {
163+ \Log::debug ($ name .'=> ' .$ pattern );
164+ if ($ pattern === $ value ) {
121165 return $ name ;
122166 }
123167 }
@@ -168,6 +212,13 @@ public function formatFieldValuesAsArray()
168212 return $ result ;
169213 }
170214
215+ /**
216+ * Check whether the field is encrypted
217+ *
218+ * @author [A. Gianotto] [<[email protected] >] 219+ * @since [v3.4]
220+ * @return Boolean
221+ */
171222 public function isFieldDecryptable ($ string )
172223 {
173224 if (($ this ->field_encrypted =='1 ' ) && ($ string !='' )) {
@@ -177,6 +228,14 @@ public function isFieldDecryptable($string)
177228 }
178229
179230
231+ /**
232+ * Convert non-UTF-8 or weirdly encoded text into something that
233+ * won't break the database.
234+ *
235+ * @author [A. Gianotto] [<[email protected] >] 236+ * @since [v3.4]
237+ * @return Boolean
238+ */
180239 public function convertUnicodeDbSlug ($ original = null )
181240 {
182241 $ name = $ original ? $ original : $ this ->name ;
0 commit comments