Search Results

Go to the documentation of this file.
1 <?php
2 namespace App\Models;
3 
5 
6 class CustomField extends Model
7 {
8  public $guarded=["id"];
9 
13  public static $PredefinedFormats=[
14  "ANY" => "",
15  "ALPHA" => "alpha",
16  "EMAIL" => "email",
17  "DATE" => "date",
18  "URL" => "url",
19  "NUMERIC" => "numeric",
20  "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}$/",
21  "IP" => "ip"
22  ];
23 
24  public $rules=[
25  "name" => "required|unique:custom_fields"
26  ];
27 
28  public static $table_name="assets";
29 
30  public static function name_to_db_name($name)
31  {
32  return "_snipeit_".preg_replace("/[^a-zA-Z0-9]/", "_", strtolower($name));
33  }
34 
35  public static function boot()
36  {
37  self::creating(function ($custom_field) {
38 
39  if (in_array($custom_field->db_column_name(), \Schema::getColumnListing(\DB::getTablePrefix().CustomField::$table_name))) {
40  //field already exists when making a new custom field; fail.
41  return false;
42  }
43 
44  \Schema::table(\DB::getTablePrefix().\App\Models\CustomField::$table_name, function ($table) use ($custom_field) {
45  $table->text($custom_field->db_column_name())->nullable();
46  });
47 
48  });
49 
50  self::updating(function ($custom_field) {
51  if ($custom_field->isDirty("name")) {
52  if (in_array($custom_field->db_column_name(), \Schema::getColumnListing(CustomField::$table_name))) {
53  //field already exists when renaming a custom field
54  return false;
55  }
56  return \DB::statement("UPDATE ".CustomField::$table_name." RENAME ".self::name_to_db_name($custom_field->get_original("name"))." TO ".$custom_field->db_column_name());
57  }
58  return true;
59  });
60 
61  self::deleting(function ($custom_field) {
62  return \DB::statement("ALTER TABLE ".CustomField::$table_name." DROP COLUMN ".$custom_field->db_column_name());
63  });
64  }
65 
66  public function fieldset()
67  {
68  return $this->belongsToMany('\App\Models\CustomFieldset'); //?!?!?!?!?!?
69  }
70 
71  public function user()
72  {
73  return $this->belongsTo('\App\Models\User');
74  }
75 
76  //public function
77 
78  //need helper to go from regex->English
79  //need helper to go from English->regex
80 
81  //need helper for save() stuff - basically to alter table for the fields in question
82 
83  public function check_format($value)
84  {
85  return preg_match('/^'.$this->attributes['format'].'$/', $value)===1;
86  }
87 
88  public function db_column_name()
89  {
90  return self::name_to_db_name($this->name);
91  }
92 
93  //mutators for 'format' attribute
94  public function getFormatAttribute($value)
95  {
96  foreach (self::$PredefinedFormats as $name => $pattern) {
97  if ($pattern===$value) {
98  return $name;
99  }
100  }
101  return $value;
102  }
103 
104  public function setFormatAttribute($value)
105  {
106  if (isset(self::$PredefinedFormats[$value])) {
107  $this->attributes['format']=self::$PredefinedFormats[$value];
108  } else {
109  $this->attributes['format']=$value;
110  }
111  }
112 }
static name_to_db_name($name)
Definition: CustomField.php:30