From b911158c264f7df8acede57bcd27ac2907b6b71c Mon Sep 17 00:00:00 2001 From: denisdulici Date: Thu, 21 May 2020 00:32:47 +0300 Subject: [PATCH] create a dashboard if none available --- app/Http/Controllers/Common/Dashboards.php | 31 +++++++++----------- app/Jobs/Common/CreateDashboard.php | 28 +++++++++++++++++- database/seeds/Dashboards.php | 34 +++++----------------- 3 files changed, 48 insertions(+), 45 deletions(-) diff --git a/app/Http/Controllers/Common/Dashboards.php b/app/Http/Controllers/Common/Dashboards.php index 7fb15a2b6..c3f245197 100644 --- a/app/Http/Controllers/Common/Dashboards.php +++ b/app/Http/Controllers/Common/Dashboards.php @@ -47,38 +47,33 @@ class Dashboards extends Controller * * @return Response */ - public function show(Dashboard $dashboard) + public function show($dashboard_id = null) { - $dashboard_id = session('dashboard_id', 0); + $dashboard_id = $dashboard_id ?? session('dashboard_id'); - if (!empty($dashboard->id)) { - $dashboard_id = $dashboard->id; + if (empty($dashboard_id)) { + $dashboard_id = user()->dashboards()->enabled()->pluck('id')->first(); } - // Change Dashboard - if (request()->get('dashboard_id', 0)) { - $dashboard_id = request()->get('dashboard_id'); - - session(['dashboard_id' => $dashboard_id]); + if (!empty($dashboard_id)) { + $dashboard = Dashboard::find($dashboard_id); } - $dashboards = user()->dashboards()->enabled()->get(); - - if (!$dashboard_id) { - $dashboard_id = $dashboards->pluck('id')->first(); + if (empty($dashboard)) { + $dashboard = $this->dispatch(new CreateDashboard([ + 'company_id' => session('company_id'), + 'name' => trans_choice('general.dashboards', 1), + 'with_widgets' => true, + ])); } - // Dashboard - $dashboard = Dashboard::find($dashboard_id); - - // Widgets $widgets = Widget::where('dashboard_id', $dashboard->id)->orderBy('sort', 'asc')->get()->filter(function ($widget) { return Widgets::canRead($widget->class); }); $financial_start = $this->getFinancialStart()->format('Y-m-d'); - return view('common.dashboards.show', compact('dashboards', 'dashboard', 'widgets', 'financial_start')); + return view('common.dashboards.show', compact('dashboard', 'widgets', 'financial_start')); } /** diff --git a/app/Jobs/Common/CreateDashboard.php b/app/Jobs/Common/CreateDashboard.php index dd6b9eb01..a610d508b 100644 --- a/app/Jobs/Common/CreateDashboard.php +++ b/app/Jobs/Common/CreateDashboard.php @@ -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++; + } + } } diff --git a/database/seeds/Dashboards.php b/database/seeds/Dashboards.php index 9f14dbc62..4a238f607 100644 --- a/database/seeds/Dashboards.php +++ b/database/seeds/Dashboards.php @@ -3,14 +3,14 @@ namespace Database\Seeds; use App\Abstracts\Model; -use App\Models\Auth\User; -use App\Models\Common\Widget; -use App\Models\Common\Dashboard; -use App\Utilities\Widgets; +use App\Jobs\Common\CreateDashboard; +use App\Traits\Jobs; use Illuminate\Database\Seeder; class Dashboards extends Seeder { + use Jobs; + /** * Run the database seeds. * @@ -30,29 +30,11 @@ class Dashboards extends Seeder $user_id = $this->command->argument('user'); $company_id = $this->command->argument('company'); - $dashboard = Dashboard::create([ + $this->dispatch(new CreateDashboard([ 'company_id' => $company_id, 'name' => trans_choice('general.dashboards', 1), - 'enabled' => 1, - ]); - - $widgets = Widgets::getClasses(false); - - $sort = 1; - - foreach ($widgets as $class => $name) { - Widget::create([ - 'company_id' => $company_id, - 'dashboard_id' => $dashboard->id, - 'class' => $class, - 'name' => $name, - 'sort' => $sort, - 'settings' => (new $class())->getDefaultSettings(), - ]); - - $sort++; - } - - User::find($user_id)->dashboards()->attach($dashboard->id); + 'with_widgets' => true, + 'users' => $user_id, + ])); } }