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 Module extends Model |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The table associated with the model. |
12
|
|
|
* |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $table = 'modules'; |
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
|
|
|
'name', |
33
|
|
|
'icon', |
34
|
|
|
'model_class', |
35
|
|
|
'data' |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
protected function initTablePrefix() |
39
|
|
|
{ |
40
|
|
|
$this->tablePrefix = env('UCCELLO_TABLE_PREFIX', 'uccello_'); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function permissions() |
44
|
|
|
{ |
45
|
|
|
return $this->hasMany(Permission::class); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function domains() |
49
|
|
|
{ |
50
|
|
|
return $this->belongsToMany(Domain::class, $this->tablePrefix.'domains_modules'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function tabs() |
54
|
|
|
{ |
55
|
|
|
return $this->hasMany(Tab::class)->orderBy('sequence'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function blocks() |
59
|
|
|
{ |
60
|
|
|
return $this->hasMany(Block::class)->orderBy('sequence'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function fields() |
64
|
|
|
{ |
65
|
|
|
return $this->hasMany(Field::class); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function filters() |
69
|
|
|
{ |
70
|
|
|
return $this->hasMany(Filter::class); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function relatedlists() |
74
|
|
|
{ |
75
|
|
|
return $this->hasMany(Relatedlist::class, 'module_id')->orderBy('sequence'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function links() |
79
|
|
|
{ |
80
|
|
|
return $this->hasMany(Link::class, 'module_id')->orderBy('sequence'); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function detailLinks() |
84
|
|
|
{ |
85
|
|
|
return $this->hasMany(Link::class, 'module_id')->where('type', 'detail')->orderBy('sequence'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function detailActionLinks() |
89
|
|
|
{ |
90
|
|
|
return $this->hasMany(Link::class, 'module_id')->where('type', 'detail.action')->orderBy('sequence'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function widgets() |
94
|
|
|
{ |
95
|
|
|
return $this->belongsToMany(Widget::class, $this->tablePrefix.'modules_widgets')->withPivot('data'); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Returns module package name |
100
|
|
|
* |
101
|
|
|
* @return string|null |
102
|
|
|
*/ |
103
|
|
|
public function getPackageAttribute() : ?string |
104
|
|
|
{ |
105
|
|
|
$package = ''; // For modules created directory in the host application |
106
|
|
|
|
107
|
|
|
// Get only package name if defined (Format: vendor/package) |
108
|
|
|
if (isset($this->data->package)) |
|
|
|
|
109
|
|
|
{ |
110
|
|
|
$packageData = explode('/', $this->data->package); |
111
|
|
|
$package = array_pop($packageData); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $package; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Return all module links to display in the menu |
119
|
|
|
* |
120
|
|
|
* @return array |
121
|
|
|
*/ |
122
|
|
|
public function getMenuLinksAttribute() : array |
123
|
|
|
{ |
124
|
|
|
$menuLinks = [ ]; |
125
|
|
|
|
126
|
|
|
//TODO: Adds capability needed |
127
|
|
|
|
128
|
|
|
if (isset($this->data->menu)) { |
|
|
|
|
129
|
|
|
// One route |
130
|
|
|
if (is_string($this->data->menu)) { |
131
|
|
|
$link = new \StdClass; |
132
|
|
|
$link->label = $this->name; |
|
|
|
|
133
|
|
|
$link->route = $this->data->menu; |
134
|
|
|
$link->icon = $this->icon; |
135
|
|
|
$menuLinks[ ] = $link; |
136
|
|
|
} |
137
|
|
|
// Several routes |
138
|
|
|
elseif (is_array($this->data->menu)) { |
139
|
|
|
foreach ($this->data->menu as $link) { |
140
|
|
|
if (empty($link->icon)) { |
141
|
|
|
$link->icon = $this->icon; |
142
|
|
|
} |
143
|
|
|
$menuLinks[ ] = $link; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
// No route wanted |
147
|
|
|
elseif ($this->data->menu === false) { |
148
|
|
|
// Nothing to do |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
// No route defined, add it automaticaly |
152
|
|
|
else { |
153
|
|
|
$link = new \StdClass; |
154
|
|
|
$link->label = $this->name; |
155
|
|
|
$link->route = 'uccello.list'; |
156
|
|
|
$link->icon = $this->icon; |
157
|
|
|
$menuLinks[ ] = $link; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return $menuLinks; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Returns module's default route |
165
|
|
|
* |
166
|
|
|
* @return string|null |
167
|
|
|
*/ |
168
|
|
|
public function getDefaultRouteAttribute() : ?string |
169
|
|
|
{ |
170
|
|
|
$defaultRoute = 'uccello.list'; |
171
|
|
|
|
172
|
|
|
if (isset($this->data->menu)) { |
|
|
|
|
173
|
|
|
// One route |
174
|
|
|
if (is_string($this->data->menu)) { |
175
|
|
|
$defaultRoute = $this->data->menu; |
176
|
|
|
} |
177
|
|
|
// Several routes |
178
|
|
|
elseif (is_array($this->data->menu)) { |
179
|
|
|
if (!empty($this->data->menu[0]->route)) { |
180
|
|
|
$defaultRoute = $this->data->menu[0]->route; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
return $defaultRoute; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Searches in the module a field by name. |
190
|
|
|
* |
191
|
|
|
* @param string $name |
192
|
|
|
* @return Field|null |
193
|
|
|
*/ |
194
|
|
|
public function getField($name) : ?Field |
195
|
|
|
{ |
196
|
|
|
return $this->fields()->where('name', $name)->first(); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Checks if the module is active on a domain. |
201
|
|
|
* |
202
|
|
|
* @param Domain $domain |
203
|
|
|
* @return boolean |
204
|
|
|
*/ |
205
|
|
|
public function isActiveOnDomain(Domain $domain) : bool |
206
|
|
|
{ |
207
|
|
|
// We don't use cache else it can be very huge with a lot of domains!!! |
208
|
|
|
return $this->domains()->where('domain_id', $domain->id)->count() > 0; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Checks if the module is for administration. |
213
|
|
|
* |
214
|
|
|
* @return boolean |
215
|
|
|
*/ |
216
|
|
|
public function isAdminModule() : bool |
217
|
|
|
{ |
218
|
|
|
return $this->data->admin ?? false; |
|
|
|
|
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Check if the module is mandatory. |
223
|
|
|
* |
224
|
|
|
* @return boolean |
225
|
|
|
*/ |
226
|
|
|
public function isMandatory() : bool |
227
|
|
|
{ |
228
|
|
|
return $this->data->mandatory ?? false; |
|
|
|
|
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|
Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.