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

@ -47,38 +47,33 @@ class Dashboards extends Controller
* *
* @return Response * @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)) { if (empty($dashboard_id)) {
$dashboard_id = $dashboard->id; $dashboard_id = user()->dashboards()->enabled()->pluck('id')->first();
} }
// Change Dashboard if (!empty($dashboard_id)) {
if (request()->get('dashboard_id', 0)) { $dashboard = Dashboard::find($dashboard_id);
$dashboard_id = request()->get('dashboard_id');
session(['dashboard_id' => $dashboard_id]);
} }
$dashboards = user()->dashboards()->enabled()->get(); if (empty($dashboard)) {
$dashboard = $this->dispatch(new CreateDashboard([
if (!$dashboard_id) { 'company_id' => session('company_id'),
$dashboard_id = $dashboards->pluck('id')->first(); '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) { $widgets = Widget::where('dashboard_id', $dashboard->id)->orderBy('sort', 'asc')->get()->filter(function ($widget) {
return Widgets::canRead($widget->class); return Widgets::canRead($widget->class);
}); });
$financial_start = $this->getFinancialStart()->format('Y-m-d'); $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'));
} }
/** /**

View File

@ -4,6 +4,8 @@ namespace App\Jobs\Common;
use App\Abstracts\Job; use App\Abstracts\Job;
use App\Models\Common\Dashboard; use App\Models\Common\Dashboard;
use App\Models\Common\Widget;
use App\Utilities\Widgets;
class CreateDashboard extends Job class CreateDashboard extends Job
{ {
@ -28,10 +30,14 @@ class CreateDashboard extends Job
{ {
$this->request['enabled'] = $this->request['enabled'] ?? 1; $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(); $this->attachToUser();
if ($this->request->has('with_widgets')) {
$this->createWidgets();
}
return $this->dashboard; return $this->dashboard;
} }
@ -49,4 +55,24 @@ class CreateDashboard extends Job
$this->dashboard->users()->attach($user); $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++;
}
}
} }

View File

@ -3,14 +3,14 @@
namespace Database\Seeds; namespace Database\Seeds;
use App\Abstracts\Model; use App\Abstracts\Model;
use App\Models\Auth\User; use App\Jobs\Common\CreateDashboard;
use App\Models\Common\Widget; use App\Traits\Jobs;
use App\Models\Common\Dashboard;
use App\Utilities\Widgets;
use Illuminate\Database\Seeder; use Illuminate\Database\Seeder;
class Dashboards extends Seeder class Dashboards extends Seeder
{ {
use Jobs;
/** /**
* Run the database seeds. * Run the database seeds.
* *
@ -30,29 +30,11 @@ class Dashboards extends Seeder
$user_id = $this->command->argument('user'); $user_id = $this->command->argument('user');
$company_id = $this->command->argument('company'); $company_id = $this->command->argument('company');
$dashboard = Dashboard::create([ $this->dispatch(new CreateDashboard([
'company_id' => $company_id, 'company_id' => $company_id,
'name' => trans_choice('general.dashboards', 1), 'name' => trans_choice('general.dashboards', 1),
'enabled' => 1, 'with_widgets' => true,
]); 'users' => $user_id,
]));
$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);
} }
} }