Akaunting/app/Jobs/Common/CreateDashboard.php

145 lines
3.9 KiB
PHP
Raw Normal View History

2020-01-07 14:15:00 +00:00
<?php
namespace App\Jobs\Common;
use App\Abstracts\Job;
2021-09-06 08:53:57 +00:00
use App\Interfaces\Job\HasOwner;
2021-09-07 07:33:34 +00:00
use App\Interfaces\Job\HasSource;
2021-09-06 08:53:57 +00:00
use App\Interfaces\Job\ShouldCreate;
2021-09-06 11:46:42 +00:00
use App\Jobs\Common\CreateWidget;
2020-12-25 14:00:13 +00:00
use App\Models\Auth\User;
use App\Models\Common\Company;
2020-01-07 14:15:00 +00:00
use App\Models\Common\Dashboard;
2020-05-20 21:32:47 +00:00
use App\Models\Common\Widget;
use App\Utilities\Widgets;
2020-12-25 14:00:13 +00:00
use Illuminate\Support\Arr;
2020-01-07 14:15:00 +00:00
2021-09-07 07:33:34 +00:00
class CreateDashboard extends Job implements HasOwner, HasSource, ShouldCreate
2020-01-07 14:15:00 +00:00
{
2021-09-06 08:53:57 +00:00
public function handle(): Dashboard
2020-01-07 14:15:00 +00:00
{
$this->request['enabled'] = $this->request['enabled'] ?? 1;
2020-06-26 10:40:19 +00:00
\DB::transaction(function () {
2020-12-25 14:00:13 +00:00
$users = $this->getUsers();
if (empty($users)) {
2022-04-26 08:46:36 +00:00
$this->model = Dashboard::make();
2020-12-25 14:00:13 +00:00
return;
}
2021-09-07 07:58:39 +00:00
$this->model = Dashboard::create($this->request->only([
'company_id', 'name', 'enabled', 'created_from', 'created_by'
]));
2020-01-07 14:15:00 +00:00
2021-09-06 08:53:57 +00:00
$this->model->users()->attach($users);
2020-01-07 14:15:00 +00:00
2020-12-25 14:00:13 +00:00
$this->checkAndCreateWidgets();
2020-06-26 10:40:19 +00:00
});
2020-05-20 21:32:47 +00:00
2021-09-06 08:53:57 +00:00
return $this->model;
2020-01-07 14:15:00 +00:00
}
2021-09-06 08:53:57 +00:00
protected function getUsers(): array
2020-01-07 14:15:00 +00:00
{
2020-12-25 14:00:13 +00: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 14:00:13 +00: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 14:39:51 +00:00
$list[] = $user->id;
2020-12-25 14:00:13 +00:00
}
2020-01-07 14:15:00 +00:00
} else {
$user = user();
2020-12-25 14:00:13 +00:00
if ($this->shouldCreateDashboardFor($user)) {
2020-12-25 14:39:51 +00:00
$list[] = $user->id;
2020-12-25 14:00:13 +00:00
}
2020-01-07 14:15:00 +00:00
}
2020-12-25 14:00:13 +00:00
return $list;
}
2021-09-06 08:53:57 +00:00
protected function shouldCreateDashboardFor($user): bool
2020-12-25 14:00:13 +00:00
{
2020-01-07 14:15:00 +00:00
if (empty($user)) {
2020-12-25 14:00:13 +00:00
return false;
2020-01-07 14:15:00 +00:00
}
2020-12-25 14:00:13 +00: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 14:15:00 +00:00
}
2020-05-20 21:32:47 +00:00
2021-09-06 08:53:57 +00:00
protected function checkAndCreateWidgets(): void
2020-05-20 21:32:47 +00:00
{
$sort = 1;
2020-12-25 14:00:13 +00:00
if ($this->request->has('default_widgets')) {
2022-06-01 07:15:55 +00: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 14:00:13 +00:00
$this->createWidgets($widgets, $sort);
}
if ($this->request->has('custom_widgets')) {
$widgets = $this->request->get('custom_widgets');
$this->createWidgets($widgets, $sort);
}
}
2021-09-06 08:53:57 +00:00
protected function createWidgets($widgets, &$sort): void
2020-12-25 14:00:13 +00:00
{
2020-05-20 21:32:47 +00:00
foreach ($widgets as $class => $name) {
2020-12-25 14:00:13 +00:00
// It's just an array of classes
if (is_numeric($class)) {
$class = $name;
$name = (new $class())->getDefaultName();
}
2021-09-06 11:46:42 +00: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(),
]));
}
2020-05-20 21:32:47 +00:00
$sort++;
}
}
2020-01-07 14:15:00 +00:00
}