akaunting/app/Jobs/Common/CreateDashboard.php

79 lines
1.7 KiB
PHP
Raw Normal View History

2020-01-07 17:15:00 +03:00
<?php
namespace App\Jobs\Common;
use App\Abstracts\Job;
use App\Models\Common\Dashboard;
2020-05-21 00:32:47 +03:00
use App\Models\Common\Widget;
use App\Utilities\Widgets;
2020-01-07 17:15:00 +03:00
class CreateDashboard extends Job
{
protected $request;
/**
* Create a new job instance.
*
* @param $request
*/
public function __construct($request)
{
$this->request = $this->getRequestInstance($request);
}
/**
* Execute the job.
*
* @return Item
*/
public function handle()
{
$this->request['enabled'] = $this->request['enabled'] ?? 1;
2020-05-21 00:32:47 +03:00
$this->dashboard = Dashboard::create($this->request->only(['company_id', 'name', 'enabled']));
2020-01-07 17:15:00 +03:00
$this->attachToUser();
2020-05-21 00:32:47 +03:00
if ($this->request->has('with_widgets')) {
$this->createWidgets();
}
2020-01-07 17:15:00 +03:00
return $this->dashboard;
}
protected function attachToUser()
{
if ($this->request->has('users')) {
$user = $this->request->get('users');
} else {
$user = user();
}
if (empty($user)) {
return;
}
$this->dashboard->users()->attach($user);
}
2020-05-21 00:32:47 +03:00
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++;
}
}
2020-01-07 17:15:00 +03:00
}