/
Dashboard.php
171 lines (146 loc) · 4.42 KB
/
Dashboard.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
namespace App\Models\Common;
use App\Abstracts\Model;
use Bkwld\Cloner\Cloneable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Support\Str;
class Dashboard extends Model
{
use Cloneable, HasFactory;
protected $table = 'dashboards';
/**
* Attributes that should be mass-assignable.
*
* @var array
*/
protected $fillable = ['company_id', 'name', 'enabled', 'created_from', 'created_by'];
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'enabled' => 'boolean',
];
/**
* Sortable columns.
*
* @var array
*/
public $sortable = ['name', 'enabled'];
public function users()
{
return $this->belongsToMany('App\Models\Auth\User', 'App\Models\Auth\UserDashboard');
}
public function widgets()
{
return $this->hasMany('App\Models\Common\Widget')->orderBy('sort', 'asc');
}
/**
* Scope to only include dashboards of a given user id.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param int $user_id
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeUserId($query, $user_id)
{
return $query->whereHas('users', function ($query) use ($user_id) {
$query->where('user_id', $user_id);
});
}
/**
* Scope to only include dashboards of a given alias.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param string $alias
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeAlias($query, $alias)
{
$class = ($alias == 'core') ? 'App\\\\' : 'Modules\\\\' . Str::studly($alias) . '\\\\';
return $query->whereHas('widgets', function ($query) use ($class) {
// Must have widgets of module
$query->where('class', 'like', $class . '%');
})->whereDoesntHave('widgets', function ($query) use ($class) {
// Must not have widgets from other modules
$query->where('class', 'not like', $class . '%');
});
}
/**
* Get the alias based on class.
*
* @return string
*/
public function getAliasAttribute()
{
$alias = '';
foreach ($this->widgets as $widget) {
if (Str::startsWith($widget->class, 'App\\')) {
$tmp_alias = 'core';
} else {
$arr = explode('\\', $widget->class);
$tmp_alias = Str::kebab($arr[1]);
}
// First time set
if ($alias == '') {
$alias = $tmp_alias;
}
// Must not have widgets from different modules
if ($alias != $tmp_alias) {
$alias = '';
break;
}
}
return $alias;
}
/**
* Get the line actions.
*
* @return array
*/
public function getLineActionsAttribute()
{
$actions = [];
if ($this->enabled) {
$actions[] = [
'title' => trans('general.switch'),
'icon' => 'settings_ethernet',
'url' => route('dashboards.switch', $this->id),
'permission' => 'read-common-dashboards',
'attributes' => [
'id' => 'index-line-actions-switch-dashboard-' . $this->id,
],
];
}
$actions[] = [
'title' => trans('general.edit'),
'icon' => 'edit',
'url' => route('dashboards.edit', $this->id),
'permission' => 'update-common-dashboards',
'attributes' => [
'id' => 'index-line-actions-edit-dashboard-' . $this->id,
],
];
$actions[] = [
'type' => 'delete',
'icon' => 'delete',
'route' => 'dashboards.destroy',
'permission' => 'delete-common-dashboards',
'attributes' => [
'id' => 'index-line-actions-delete-dashboard-' . $this->id,
],
'model' => $this,
];
return $actions;
}
/**
* Create a new factory instance for the model.
*
* @return \Illuminate\Database\Eloquent\Factories\Factory
*/
protected static function newFactory()
{
return \Database\Factories\Dashboard::new();
}
}