akaunting/app/Jobs/Common/CreateDashboard.php

147 lines
4.0 KiB
PHP
Raw Normal View History

2020-01-07 17:15:00 +03:00
<?php
namespace App\Jobs\Common;
use App\Abstracts\Job;
2021-09-06 11:53:57 +03:00
use App\Interfaces\Job\HasOwner;
2021-09-07 10:33:34 +03:00
use App\Interfaces\Job\HasSource;
2021-09-06 11:53:57 +03:00
use App\Interfaces\Job\ShouldCreate;
2021-09-06 14:46:42 +03:00
use App\Jobs\Common\CreateWidget;
2020-12-25 17:00:13 +03:00
use App\Models\Auth\User;
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
2021-09-07 10:33:34 +03:00
class CreateDashboard extends Job implements HasOwner, HasSource, ShouldCreate
2020-01-07 17:15:00 +03:00
{
2021-09-06 11:53:57 +03:00
public function handle(): Dashboard
2020-01-07 17:15:00 +03:00
{
$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)) {
2022-04-26 11:46:36 +03:00
$this->model = Dashboard::make();
2020-12-25 17:00:13 +03:00
return;
}
2021-09-07 10:58:39 +03:00
$this->model = Dashboard::create($this->request->only([
'company_id', 'name', 'enabled', 'created_from', 'created_by'
]));
2020-01-07 17:15:00 +03:00
2021-09-06 11:53:57 +03:00
$this->model->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
2021-09-06 11:53:57 +03:00
return $this->model;
2020-01-07 17:15:00 +03:00
}
2021-09-06 11:53:57 +03:00
protected function getUsers(): array
2020-01-07 17:15:00 +03:00
{
2020-12-25 17:00:13 +03:00
$list = [];
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;
}
2021-09-06 11:53:57 +03:00
protected function shouldCreateDashboardFor($user): bool
2020-12-25 17:00:13 +03:00
{
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
2021-09-06 11:53:57 +03:00
protected function checkAndCreateWidgets(): void
2020-05-21 00:32:47 +03:00
{
$sort = 1;
2020-12-25 17:00:13 +03:00
if ($this->request->has('default_widgets')) {
2022-06-01 10:15:55 +03:00
$default_widgets = $this->request->get('default_widgets');
if (! is_array($default_widgets) && ($default_widgets == 'core')) {
Widgets::optimizeCoreWidgets();
}
$widgets = Widgets::getClasses($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);
}
}
2021-09-06 11:53:57 +03:00
protected function createWidgets($widgets, &$sort): void
2020-12-25 17:00:13 +03:00
{
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-09-06 14:46:42 +03:00
$widget = Widget::companyId($this->model->company_id)
->where('dashboard_id', $this->model->id)
->where('class', $class)
->first();
if (! $widget) {
$this->dispatch(new CreateWidget([
'company_id' => $this->model->company_id,
'dashboard_id' => $this->model->id,
'class' => $class,
'name' => $name,
'sort' => $sort,
'settings' => (new $class())->getDefaultSettings(),
'created_from' => $this->model->created_from,
'created_by' => $this->model->created_by,
2021-09-06 14:46:42 +03:00
]));
}
2020-05-21 00:32:47 +03:00
$sort++;
}
}
2020-01-07 17:15:00 +03:00
}