2020-01-07 17:15:00 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Jobs\Common;
|
|
|
|
|
|
|
|
use App\Abstracts\Job;
|
2020-12-25 17:00:13 +03:00
|
|
|
use App\Models\Auth\User;
|
2021-03-30 00:12:51 +03:00
|
|
|
use App\Models\Common\Company;
|
2020-01-07 17:15:00 +03:00
|
|
|
use App\Models\Common\Dashboard;
|
2020-05-21 00:32:47 +03:00
|
|
|
use App\Models\Common\Widget;
|
|
|
|
use App\Utilities\Widgets;
|
2020-12-25 17:00:13 +03:00
|
|
|
use Illuminate\Support\Arr;
|
2020-01-07 17:15:00 +03:00
|
|
|
|
|
|
|
class CreateDashboard extends Job
|
|
|
|
{
|
2020-06-26 13:40:19 +03:00
|
|
|
protected $dashboard;
|
|
|
|
|
2020-01-07 17:15:00 +03:00
|
|
|
protected $request;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new job instance.
|
|
|
|
*
|
|
|
|
* @param $request
|
|
|
|
*/
|
|
|
|
public function __construct($request)
|
|
|
|
{
|
|
|
|
$this->request = $this->getRequestInstance($request);
|
2021-06-17 10:59:07 +03:00
|
|
|
$this->request->merge(['created_by' => user_id()]);
|
2020-01-07 17:15:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the job.
|
|
|
|
*
|
|
|
|
* @return Item
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
|
|
|
$this->request['enabled'] = $this->request['enabled'] ?? 1;
|
|
|
|
|
2020-06-26 13:40:19 +03:00
|
|
|
\DB::transaction(function () {
|
2020-12-25 17:00:13 +03:00
|
|
|
$users = $this->getUsers();
|
|
|
|
|
|
|
|
if (empty($users)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-26 13:40:19 +03:00
|
|
|
$this->dashboard = Dashboard::create($this->request->only(['company_id', 'name', 'enabled']));
|
2020-01-07 17:15:00 +03:00
|
|
|
|
2020-12-25 17:00:13 +03:00
|
|
|
$this->dashboard->users()->attach($users);
|
2020-01-07 17:15:00 +03:00
|
|
|
|
2020-12-25 17:00:13 +03:00
|
|
|
$this->checkAndCreateWidgets();
|
2020-06-26 13:40:19 +03:00
|
|
|
});
|
2020-05-21 00:32:47 +03:00
|
|
|
|
2020-01-07 17:15:00 +03:00
|
|
|
return $this->dashboard;
|
|
|
|
}
|
|
|
|
|
2020-12-25 17:00:13 +03:00
|
|
|
protected function getUsers()
|
2020-01-07 17:15:00 +03:00
|
|
|
{
|
2020-12-25 17:00:13 +03:00
|
|
|
$list = [];
|
|
|
|
|
2021-03-30 00:12:51 +03:00
|
|
|
if ($this->request->has('all_users')) {
|
|
|
|
Company::find($this->request->get('company_id'))->users()->each(function ($user) use (&$list) {
|
|
|
|
if (!$this->shouldCreateDashboardFor($user)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$list[] = $user->id;
|
|
|
|
});
|
|
|
|
} elseif ($this->request->has('users')) {
|
2020-12-25 17:00:13 +03:00
|
|
|
$user_ids = Arr::wrap($this->request->get('users'));
|
|
|
|
|
|
|
|
foreach($user_ids as $user_id) {
|
|
|
|
$user = User::find($user_id);
|
|
|
|
|
|
|
|
if (!$this->shouldCreateDashboardFor($user)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-12-25 17:39:51 +03:00
|
|
|
$list[] = $user->id;
|
2020-12-25 17:00:13 +03:00
|
|
|
}
|
2020-01-07 17:15:00 +03:00
|
|
|
} else {
|
|
|
|
$user = user();
|
2020-12-25 17:00:13 +03:00
|
|
|
|
|
|
|
if ($this->shouldCreateDashboardFor($user)) {
|
2020-12-25 17:39:51 +03:00
|
|
|
$list[] = $user->id;
|
2020-12-25 17:00:13 +03:00
|
|
|
}
|
2020-01-07 17:15:00 +03:00
|
|
|
}
|
|
|
|
|
2020-12-25 17:00:13 +03:00
|
|
|
return $list;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function shouldCreateDashboardFor($user)
|
|
|
|
{
|
2020-01-07 17:15:00 +03:00
|
|
|
if (empty($user)) {
|
2020-12-25 17:00:13 +03:00
|
|
|
return false;
|
2020-01-07 17:15:00 +03:00
|
|
|
}
|
|
|
|
|
2020-12-25 17:00:13 +03:00
|
|
|
// Don't create dashboard if user can't access admin panel (i.e. customer with login)
|
|
|
|
if ($user->cannot('read-admin-panel')) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2020-01-07 17:15:00 +03:00
|
|
|
}
|
2020-05-21 00:32:47 +03:00
|
|
|
|
2020-12-25 17:00:13 +03:00
|
|
|
protected function checkAndCreateWidgets()
|
2020-05-21 00:32:47 +03:00
|
|
|
{
|
|
|
|
$sort = 1;
|
|
|
|
|
2020-12-25 17:00:13 +03:00
|
|
|
if ($this->request->has('default_widgets')) {
|
2021-03-30 00:12:51 +03:00
|
|
|
$widgets = Widgets::getClasses($this->request->get('default_widgets'), false);
|
2020-12-25 17:00:13 +03:00
|
|
|
|
|
|
|
$this->createWidgets($widgets, $sort);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->request->has('custom_widgets')) {
|
|
|
|
$widgets = $this->request->get('custom_widgets');
|
|
|
|
|
|
|
|
$this->createWidgets($widgets, $sort);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function createWidgets($widgets, &$sort)
|
|
|
|
{
|
2020-05-21 00:32:47 +03:00
|
|
|
foreach ($widgets as $class => $name) {
|
2020-12-25 17:00:13 +03:00
|
|
|
// It's just an array of classes
|
|
|
|
if (is_numeric($class)) {
|
|
|
|
$class = $name;
|
|
|
|
$name = (new $class())->getDefaultName();
|
|
|
|
}
|
|
|
|
|
2021-03-30 00:12:51 +03:00
|
|
|
Widget::firstOrCreate([
|
2020-05-21 00:32:47 +03:00
|
|
|
'company_id' => $this->dashboard->company_id,
|
|
|
|
'dashboard_id' => $this->dashboard->id,
|
|
|
|
'class' => $class,
|
2021-03-30 00:12:51 +03:00
|
|
|
], [
|
2020-05-21 00:32:47 +03:00
|
|
|
'name' => $name,
|
|
|
|
'sort' => $sort,
|
|
|
|
'settings' => (new $class())->getDefaultSettings(),
|
|
|
|
]);
|
|
|
|
|
|
|
|
$sort++;
|
|
|
|
}
|
|
|
|
}
|
2020-01-07 17:15:00 +03:00
|
|
|
}
|