1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Uccello\Core\Models; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
6
|
|
|
use Illuminate\Support\Facades\Cache; |
7
|
|
|
use Cviebrock\EloquentSluggable\Sluggable; |
|
|
|
|
8
|
|
|
use Gzero\EloquentTree\Model\Tree; |
|
|
|
|
9
|
|
|
use Spatie\Searchable\Searchable; |
|
|
|
|
10
|
|
|
use Spatie\Searchable\SearchResult; |
|
|
|
|
11
|
|
|
use Uccello\Core\Support\Traits\UccelloModule; |
12
|
|
|
|
13
|
|
|
class Domain extends Tree implements Searchable |
14
|
|
|
{ |
15
|
|
|
use SoftDeletes; |
16
|
|
|
use Sluggable; |
17
|
|
|
use UccelloModule; |
18
|
|
|
|
19
|
|
|
protected $tablePrefix; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The accessors to append to the model's array form. |
23
|
|
|
* |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected $appends = [ |
27
|
|
|
'recordLabel', |
28
|
|
|
'uuid', |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The table associated with the model. |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $table = 'domains'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The attributes that should be mutated to dates. |
40
|
|
|
* |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
protected $dates = [ 'deleted_at' ]; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* The attributes that should be casted to native types. |
47
|
|
|
* |
48
|
|
|
* @var array |
49
|
|
|
*/ |
50
|
|
|
protected $casts = [ |
51
|
|
|
'data' => 'object', |
52
|
|
|
]; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* The attributes that are mass assignable. |
56
|
|
|
* |
57
|
|
|
* @var array |
58
|
|
|
*/ |
59
|
|
|
protected $fillable = [ |
60
|
|
|
'name', |
61
|
|
|
'description', |
62
|
|
|
'parent_id', |
63
|
|
|
]; |
64
|
|
|
|
65
|
|
|
public static function boot() |
66
|
|
|
{ |
67
|
|
|
parent::boot(); |
68
|
|
|
|
69
|
|
|
// Linck to parent record |
70
|
|
|
static::created(function ($model) { |
71
|
|
|
static::linkToParentRecord($model); |
72
|
|
|
}); |
73
|
|
|
|
74
|
|
|
// static::updatedParent(function ($model) { |
75
|
|
|
// static::linkToParentRecord($model); |
76
|
|
|
// }); |
77
|
|
|
|
78
|
|
|
static::updated(function ($model) { |
79
|
|
|
static::linkToParentRecord($model); |
80
|
|
|
}); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public static function linkToParentRecord($model) |
84
|
|
|
{ |
85
|
|
|
// Set parent record |
86
|
|
|
$parentRecord = Domain::find(request('parent')); |
87
|
|
|
|
88
|
|
|
if (!is_null($parentRecord)) { |
89
|
|
|
with($model)->setChildOf($parentRecord); |
90
|
|
|
} |
91
|
|
|
// Remove parent domain |
92
|
|
|
else { |
93
|
|
|
with($model)->setAsRoot(); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Check if node is root |
99
|
|
|
* This function check foreign key field |
100
|
|
|
* |
101
|
|
|
* @return bool |
102
|
|
|
*/ |
103
|
|
|
public function isRoot() |
104
|
|
|
{ |
105
|
|
|
// return (empty($this->{$this->getTreeColumn('parent')})) ? true : false; |
106
|
|
|
return $this->{$this->getTreeColumn('path')} === $this->getKey() . '/' |
107
|
|
|
&& $this->{$this->getTreeColumn('level')} === 0; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Return the sluggable configuration array for this model. |
112
|
|
|
* |
113
|
|
|
* @return array |
114
|
|
|
*/ |
115
|
|
|
public function sluggable() |
116
|
|
|
{ |
117
|
|
|
return [ |
118
|
|
|
'slug' => [ |
119
|
|
|
'source' => 'name', |
120
|
|
|
'onUpdate' => true, |
121
|
|
|
'includeTrashed' => false |
122
|
|
|
] |
123
|
|
|
]; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public $searchableType = 'domain'; |
127
|
|
|
|
128
|
|
|
public $searchableColumns = [ |
129
|
|
|
'name' |
130
|
|
|
]; |
131
|
|
|
|
132
|
|
|
public function getSearchResult(): SearchResult |
133
|
|
|
{ |
134
|
|
|
return new SearchResult( |
135
|
|
|
$this, |
136
|
|
|
$this->recordLabel |
137
|
|
|
); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function __construct(array $attributes = [ ]) |
141
|
|
|
{ |
142
|
|
|
parent::__construct($attributes); |
143
|
|
|
|
144
|
|
|
// Init table prefix |
145
|
|
|
$this->initTablePrefix(); |
146
|
|
|
|
147
|
|
|
// Init table name |
148
|
|
|
$this->initTableName(); |
149
|
|
|
|
150
|
|
|
$this->addTreeEvents(); // Adding tree events |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function getTablePrefix() |
154
|
|
|
{ |
155
|
|
|
return $this->tablePrefix; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
protected function initTablePrefix() |
159
|
|
|
{ |
160
|
|
|
$this->tablePrefix = env('UCCELLO_TABLE_PREFIX', 'uccello_'); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
protected function initTableName() |
164
|
|
|
{ |
165
|
|
|
if ($this->table) |
166
|
|
|
{ |
167
|
|
|
$this->table = $this->tablePrefix.$this->table; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function privileges() |
172
|
|
|
{ |
173
|
|
|
return $this->hasMany(Privilege::class); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public function users() |
177
|
|
|
{ |
178
|
|
|
return $this->hasMany(User::class); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
public function roles() |
182
|
|
|
{ |
183
|
|
|
return $this->hasMany(Role::class); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
public function profiles() |
187
|
|
|
{ |
188
|
|
|
return $this->hasMany(Profile::class); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function modules() |
192
|
|
|
{ |
193
|
|
|
return $this->belongsToMany(Module::class, $this->tablePrefix.'domains_modules'); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public function menus() |
197
|
|
|
{ |
198
|
|
|
return $this->hasMany(Menu::class); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Returns record label |
203
|
|
|
* |
204
|
|
|
* @return string |
205
|
|
|
*/ |
206
|
|
|
public function getRecordLabelAttribute() : string |
207
|
|
|
{ |
208
|
|
|
return $this->name; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Returns all admin modules activated in the domain |
213
|
|
|
* |
214
|
|
|
* @return array |
215
|
|
|
*/ |
216
|
|
|
protected function getAdminModulesAttribute() : array |
217
|
|
|
{ |
218
|
|
|
$modules = [ ]; |
219
|
|
|
|
220
|
|
|
foreach ($this->modules()->get() as $module) { |
221
|
|
|
if ($module->isAdminModule()) { |
222
|
|
|
$modules[ ] = $module; |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
return $modules; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Returns all not admin modules activated in the domain |
231
|
|
|
* |
232
|
|
|
* @return array |
233
|
|
|
*/ |
234
|
|
|
protected function getNotAdminModulesAttribute() : array |
235
|
|
|
{ |
236
|
|
|
return Cache::rememberForever('not_admin_modules', function () { |
237
|
|
|
$modules = [ ]; |
238
|
|
|
|
239
|
|
|
foreach ($this->modules()->get() as $module) { |
240
|
|
|
if (!$module->isAdminModule()) { |
241
|
|
|
$modules[ ] = $module; |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
return $modules; |
246
|
|
|
}); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Return main menu |
251
|
|
|
* Priority: |
252
|
|
|
* 1. User menu |
253
|
|
|
* 2. Domain menu |
254
|
|
|
* 3. Default menu |
255
|
|
|
* |
256
|
|
|
* @return \Uccello\Core\Models\Menu|null |
257
|
|
|
*/ |
258
|
|
|
public function getMainMenuAttribute() |
259
|
|
|
{ |
260
|
|
|
$userMenu = auth()->user()->menus()->where('type', 'main')->where('domain_id', $this->id)->first(); |
261
|
|
|
$domainMenu = $this->menus()->where('type', 'main')->whereNull('user_id')->first(); |
262
|
|
|
$defaultMenu = Menu::where('type', 'main')->whereNull('domain_id')->whereNull('user_id')->first(); |
263
|
|
|
|
264
|
|
|
if (!is_null($userMenu)) { |
265
|
|
|
return $userMenu; |
266
|
|
|
} elseif (!is_null($domainMenu)) { |
267
|
|
|
return $domainMenu; |
268
|
|
|
} elseif (!is_null($defaultMenu)) { |
269
|
|
|
return $defaultMenu; |
270
|
|
|
} else { |
271
|
|
|
return null; |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Return admin menu |
277
|
|
|
* Priority: |
278
|
|
|
* 1. User menu |
279
|
|
|
* 2. Domain menu |
280
|
|
|
* 3. Default menu |
281
|
|
|
* |
282
|
|
|
* @return \Uccello\Core\Models\Menu|null |
283
|
|
|
*/ |
284
|
|
|
public function getAdminMenuAttribute() |
285
|
|
|
{ |
286
|
|
|
$userMenu = auth()->user()->menus()->where('type', 'admin')->where('domain_id', $this->id)->first(); |
287
|
|
|
$domainMenu = $this->menus()->where('type', 'admin')->whereNull('user_id')->first(); |
288
|
|
|
$defaultMenu = Menu::where('type', 'admin')->whereNull('domain_id')->whereNull('user_id')->first(); |
289
|
|
|
|
290
|
|
|
if (!is_null($userMenu)) { |
291
|
|
|
return $userMenu; |
292
|
|
|
} elseif (!is_null($domainMenu)) { |
293
|
|
|
return $domainMenu; |
294
|
|
|
} elseif (!is_null($defaultMenu)) { |
295
|
|
|
return $defaultMenu; |
296
|
|
|
} else { |
297
|
|
|
return null; |
298
|
|
|
} |
299
|
|
|
} |
300
|
|
|
} |
301
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths