1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Uccello\Core\Models; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Cache; |
6
|
|
|
use Uccello\Core\Database\Eloquent\Model; |
7
|
|
|
|
8
|
|
|
class Field extends Model |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The table associated with the model. |
12
|
|
|
* |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $table = 'fields'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The attributes that should be casted to native types. |
19
|
|
|
* |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
protected $casts = [ |
23
|
|
|
'data' => 'object', |
24
|
|
|
]; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The attributes that are mass assignable. |
28
|
|
|
* |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected $fillable = [ |
32
|
|
|
'module_id', |
33
|
|
|
'block_id', |
34
|
|
|
'uitype_id', |
35
|
|
|
'displaytype_id', |
36
|
|
|
'name', |
37
|
|
|
'sequence', |
38
|
|
|
'data', |
39
|
|
|
]; |
40
|
|
|
|
41
|
|
|
protected function initTablePrefix() |
42
|
|
|
{ |
43
|
|
|
$this->tablePrefix = env('UCCELLO_TABLE_PREFIX', 'uccello_'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function block() |
47
|
|
|
{ |
48
|
|
|
return $this->belongsTo(Block::class); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function uitype() |
52
|
|
|
{ |
53
|
|
|
return $this->belongsTo(Uitype::class); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function displaytype() |
57
|
|
|
{ |
58
|
|
|
return $this->belongsTo(Displaytype::class); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function module() |
62
|
|
|
{ |
63
|
|
|
return $this->belongsTo(Module::class); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Returns overrided label if defined, else default one. |
68
|
|
|
* Default: field.fieldName |
69
|
|
|
* |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
|
|
public function getLabelAttribute() : string |
73
|
|
|
{ |
74
|
|
|
return $this->data->label ?? 'field.'.$this->name; |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Returns overrided database column name if defined, else default one. |
79
|
|
|
* The related uitype defines default column name. |
80
|
|
|
* |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
public function getColumnAttribute() : string |
84
|
|
|
{ |
85
|
|
|
return Cache::rememberForever('field_'.$this->id.'_column', function() { |
86
|
|
|
if ($this->data->column ?? false) { |
|
|
|
|
87
|
|
|
$column = $this->data->column; |
88
|
|
|
} else { |
89
|
|
|
$uitypeClass = $this->uitype->class; |
|
|
|
|
90
|
|
|
$uitype = new $uitypeClass(); |
91
|
|
|
$column = $uitype->getDefaultDatabaseColumn($this); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $column; |
95
|
|
|
}); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Returns default icon if defined, else checks if an icon is related to the uitype. |
100
|
|
|
* |
101
|
|
|
* @return string|null |
102
|
|
|
*/ |
103
|
|
|
public function getIconAttribute() : ?string |
104
|
|
|
{ |
105
|
|
|
return Cache::rememberForever('field_'.$this->id.'_icon', function() { |
106
|
|
|
if ($this->data->icon ?? false) { |
|
|
|
|
107
|
|
|
$icon = $this->data->icon; |
108
|
|
|
|
109
|
|
|
} else { |
110
|
|
|
$uitypeClass = $this->uitype->class; |
|
|
|
|
111
|
|
|
$uitype = new $uitypeClass(); |
112
|
|
|
$icon = $uitype->getDefaultIcon(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $icon; |
116
|
|
|
}); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function getRequiredAttribute(): bool |
120
|
|
|
{ |
121
|
|
|
$rules = explode('|', $this->data->rules ?? ''); |
|
|
|
|
122
|
|
|
|
123
|
|
|
return in_array('required', $rules); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Checks if the field can be displayed in List view. |
128
|
|
|
* |
129
|
|
|
* @return boolean |
130
|
|
|
*/ |
131
|
|
|
public function isListable() : bool |
132
|
|
|
{ |
133
|
|
|
return Cache::rememberForever('displaytype_'.$this->displaytype_id.'_is_listable', function() { |
|
|
|
|
134
|
|
|
return $this->displaytype->isListable(); |
135
|
|
|
}); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Checks if the field can be displayed in Detail view. |
140
|
|
|
* |
141
|
|
|
* @return boolean |
142
|
|
|
*/ |
143
|
|
|
public function isDetailable() : bool |
144
|
|
|
{ |
145
|
|
|
return Cache::rememberForever('displaytype_'.$this->displaytype_id.'_is_detailable', function() { |
|
|
|
|
146
|
|
|
return $this->displaytype->isDetailable(); |
147
|
|
|
}); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Checks if the field can be displayed in Edit view (create mode). |
152
|
|
|
* |
153
|
|
|
* @return boolean |
154
|
|
|
*/ |
155
|
|
|
public function isCreateable() : bool |
156
|
|
|
{ |
157
|
|
|
return Cache::rememberForever('displaytype_'.$this->displaytype_id.'_is_createable', function() { |
|
|
|
|
158
|
|
|
return $this->displaytype->isCreateable(); |
159
|
|
|
}); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Checks if the field can be displayed in Edit view (edit mode). |
164
|
|
|
* |
165
|
|
|
* @return boolean |
166
|
|
|
*/ |
167
|
|
|
public function isEditable() : bool |
168
|
|
|
{ |
169
|
|
|
return Cache::rememberForever('displaytype_'.$this->displaytype_id.'_is_editable', function() { |
|
|
|
|
170
|
|
|
return $this->displaytype->isEditable(); |
171
|
|
|
}); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Checks if the field can be displayed is hidden. |
176
|
|
|
* |
177
|
|
|
* @return boolean |
178
|
|
|
*/ |
179
|
|
|
public function isHidden() : bool |
180
|
|
|
{ |
181
|
|
|
return Cache::rememberForever('displaytype_'.$this->displaytype_id.'_is_hidden', function() { |
|
|
|
|
182
|
|
|
return $this->displaytype->id === displaytype('hidden')->id; |
183
|
|
|
}); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.