create a dashboard if none available

This commit is contained in:
denisdulici
2020-05-21 00:32:47 +03:00
parent ef7ea48755
commit b911158c26
3 changed files with 48 additions and 45 deletions

View File

@ -4,6 +4,8 @@ namespace App\Jobs\Common;
use App\Abstracts\Job;
use App\Models\Common\Dashboard;
use App\Models\Common\Widget;
use App\Utilities\Widgets;
class CreateDashboard extends Job
{
@ -28,10 +30,14 @@ class CreateDashboard extends Job
{
$this->request['enabled'] = $this->request['enabled'] ?? 1;
$this->dashboard = Dashboard::create($this->request->all());
$this->dashboard = Dashboard::create($this->request->only(['company_id', 'name', 'enabled']));
$this->attachToUser();
if ($this->request->has('with_widgets')) {
$this->createWidgets();
}
return $this->dashboard;
}
@ -49,4 +55,24 @@ class CreateDashboard extends Job
$this->dashboard->users()->attach($user);
}
protected function createWidgets()
{
$widgets = Widgets::getClasses(false);
$sort = 1;
foreach ($widgets as $class => $name) {
Widget::create([
'company_id' => $this->dashboard->company_id,
'dashboard_id' => $this->dashboard->id,
'class' => $class,
'name' => $name,
'sort' => $sort,
'settings' => (new $class())->getDefaultSettings(),
]);
$sort++;
}
}
}