anonymous widget

This commit is contained in:
denisdulici 2020-01-16 14:20:08 +03:00
parent 8e6e5049e7
commit d129d04ffd
3 changed files with 31 additions and 12 deletions

View File

@ -16,7 +16,7 @@ class Widget extends Model
* *
* @var array * @var array
*/ */
protected $fillable = ['company_id', 'dashboard_id', 'class', 'name', 'settings', 'sort']; protected $fillable = ['company_id', 'dashboard_id', 'class', 'name', 'sort', 'settings'];
/** /**
* The attributes that should be casted to native types. * The attributes that should be casted to native types.
@ -32,8 +32,8 @@ class Widget extends Model
return $this->belongsTo('App\Models\Common\Dashboard'); return $this->belongsTo('App\Models\Common\Dashboard');
} }
public function user() public function users()
{ {
return $this->hasOneThrough('App\Models\Auth\User', 'App\Models\Common\Dashboard'); return $this->hasManyThrough('App\Models\Auth\User', 'App\Models\Common\Dashboard');
} }
} }

View File

@ -48,16 +48,35 @@ class Widgets
public static function getClassInstance($model) public static function getClassInstance($model)
{ {
if (is_string($model)) { if (is_string($model)) {
$model = Widget::where('class', $model)->first(); $class_name = $model;
if (!class_exists($class_name)) {
return false;
}
$model = Widget::where('dashboard_id', session('dashboard_id'))->where('class', $class_name)->first();
if (!$model instanceof Widget) {
$class = (new $class_name());
$model = new Widget();
$model->id = 0;
$model->company_id = session('company_id');
$model->dashboard_id = session('dashboard_id');
$model->class = $class_name;
$model->name = $class->getDefaultName();
$model->sort = 99;
$model->settings = $class->getDefaultSettings();
}
} else {
if ((!$model instanceof Widget) || !class_exists($model->class)) {
return false;
}
$class_name = $model->class;
} }
if ((!$model instanceof Widget) || !class_exists($model->class)) { return new $class_name($model);
return false;
}
$class = $model->class;
return new $class($model);
} }
public static function show($model, ...$arguments) public static function show($model, ...$arguments)

View File

@ -46,8 +46,8 @@ class Dashboards extends Seeder
'dashboard_id' => $dashboard->id, 'dashboard_id' => $dashboard->id,
'class' => $class, 'class' => $class,
'name' => $name, 'name' => $name,
'settings' => (new $class())->getDefaultSettings(),
'sort' => $sort, 'sort' => $sort,
'settings' => (new $class())->getDefaultSettings(),
]); ]);
$sort++; $sort++;