2019-11-16 10:21:14 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models\Common;
|
|
|
|
|
|
|
|
use App\Abstracts\Model;
|
|
|
|
use Bkwld\Cloner\Cloneable;
|
2020-10-16 23:35:26 +03:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
2021-03-30 00:12:51 +03:00
|
|
|
use Illuminate\Support\Str;
|
2019-11-16 10:21:14 +03:00
|
|
|
|
|
|
|
class Dashboard extends Model
|
|
|
|
{
|
2020-10-16 23:35:26 +03:00
|
|
|
use Cloneable, HasFactory;
|
2019-11-16 10:21:14 +03:00
|
|
|
|
|
|
|
protected $table = 'dashboards';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attributes that should be mass-assignable.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2021-09-07 10:33:34 +03:00
|
|
|
protected $fillable = ['company_id', 'name', 'enabled', 'created_from', 'created_by'];
|
2019-11-16 10:21:14 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sortable columns.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $sortable = ['name', 'enabled'];
|
|
|
|
|
2020-01-07 17:15:00 +03:00
|
|
|
public function users()
|
2019-11-16 10:21:14 +03:00
|
|
|
{
|
2022-03-02 12:19:46 +03:00
|
|
|
return $this->belongsToMany('App\Models\Auth\User', 'App\Models\Auth\UserDashboard');
|
2019-11-16 10:21:14 +03:00
|
|
|
}
|
|
|
|
|
2019-12-31 11:27:49 +03:00
|
|
|
public function widgets()
|
2019-11-16 10:21:14 +03:00
|
|
|
{
|
2019-12-31 11:27:49 +03:00
|
|
|
return $this->hasMany('App\Models\Common\Widget')->orderBy('sort', 'asc');
|
2019-11-16 10:21:14 +03:00
|
|
|
}
|
2020-10-16 23:35:26 +03:00
|
|
|
|
2021-01-29 23:56:25 +03:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-03-30 00:12:51 +03:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2022-06-01 10:15:55 +03:00
|
|
|
/**
|
|
|
|
* 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',
|
2022-09-06 13:54:56 +03:00
|
|
|
'attributes' => [
|
|
|
|
'id' => 'index-line-actions-switch-dashboard-' . $this->id,
|
|
|
|
],
|
2022-06-01 10:15:55 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
$actions[] = [
|
|
|
|
'title' => trans('general.edit'),
|
|
|
|
'icon' => 'edit',
|
|
|
|
'url' => route('dashboards.edit', $this->id),
|
|
|
|
'permission' => 'update-common-dashboards',
|
2022-09-06 13:54:56 +03:00
|
|
|
'attributes' => [
|
|
|
|
'id' => 'index-line-actions-edit-dashboard-' . $this->id,
|
|
|
|
],
|
2022-06-01 10:15:55 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
$actions[] = [
|
|
|
|
'type' => 'delete',
|
|
|
|
'icon' => 'delete',
|
|
|
|
'route' => 'dashboards.destroy',
|
|
|
|
'permission' => 'delete-common-dashboards',
|
2022-09-06 13:54:56 +03:00
|
|
|
'attributes' => [
|
|
|
|
'id' => 'index-line-actions-delete-dashboard-' . $this->id,
|
|
|
|
],
|
2022-06-01 10:15:55 +03:00
|
|
|
'model' => $this,
|
|
|
|
];
|
|
|
|
|
|
|
|
return $actions;
|
|
|
|
}
|
|
|
|
|
2020-10-16 23:35:26 +03:00
|
|
|
/**
|
|
|
|
* Create a new factory instance for the model.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Factories\Factory
|
|
|
|
*/
|
|
|
|
protected static function newFactory()
|
|
|
|
{
|
|
|
|
return \Database\Factories\Dashboard::new();
|
|
|
|
}
|
2019-11-16 10:21:14 +03:00
|
|
|
}
|