create a dashboard if none available
This commit is contained in:
parent
ef7ea48755
commit
b911158c26
@ -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]);
|
||||
}
|
||||
|
||||
$dashboards = user()->dashboards()->enabled()->get();
|
||||
|
||||
if (!$dashboard_id) {
|
||||
$dashboard_id = $dashboards->pluck('id')->first();
|
||||
}
|
||||
|
||||
// Dashboard
|
||||
if (!empty($dashboard_id)) {
|
||||
$dashboard = Dashboard::find($dashboard_id);
|
||||
}
|
||||
|
||||
if (empty($dashboard)) {
|
||||
$dashboard = $this->dispatch(new CreateDashboard([
|
||||
'company_id' => session('company_id'),
|
||||
'name' => trans_choice('general.dashboards', 1),
|
||||
'with_widgets' => true,
|
||||
]));
|
||||
}
|
||||
|
||||
// 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'));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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,
|
||||
]));
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user