From d129d04ffd584b2f7df9152aa11d157f523de4f3 Mon Sep 17 00:00:00 2001 From: denisdulici Date: Thu, 16 Jan 2020 14:20:08 +0300 Subject: [PATCH] anonymous widget --- app/Models/Common/Widget.php | 6 +++--- app/Utilities/Widgets.php | 35 +++++++++++++++++++++++++++-------- database/seeds/Dashboards.php | 2 +- 3 files changed, 31 insertions(+), 12 deletions(-) diff --git a/app/Models/Common/Widget.php b/app/Models/Common/Widget.php index 79a8912c1..d81e20b26 100644 --- a/app/Models/Common/Widget.php +++ b/app/Models/Common/Widget.php @@ -16,7 +16,7 @@ class Widget extends Model * * @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. @@ -32,8 +32,8 @@ class Widget extends Model 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'); } } diff --git a/app/Utilities/Widgets.php b/app/Utilities/Widgets.php index b368e5d19..415520809 100644 --- a/app/Utilities/Widgets.php +++ b/app/Utilities/Widgets.php @@ -48,16 +48,35 @@ class Widgets public static function getClassInstance($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 false; - } - - $class = $model->class; - - return new $class($model); + return new $class_name($model); } public static function show($model, ...$arguments) diff --git a/database/seeds/Dashboards.php b/database/seeds/Dashboards.php index 29e78a490..9f14dbc62 100644 --- a/database/seeds/Dashboards.php +++ b/database/seeds/Dashboards.php @@ -46,8 +46,8 @@ class Dashboards extends Seeder 'dashboard_id' => $dashboard->id, 'class' => $class, 'name' => $name, - 'settings' => (new $class())->getDefaultSettings(), 'sort' => $sort, + 'settings' => (new $class())->getDefaultSettings(), ]); $sort++;