akaunting/app/Jobs/Common/DeleteDashboard.php

70 lines
1.4 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\Traits\Users;
class DeleteDashboard extends Job
{
use Users;
protected $dashboard;
/**
* Create a new job instance.
*
* @param $dashboard
*/
public function __construct($dashboard)
{
$this->dashboard = $dashboard;
}
/**
* Execute the job.
*
* @return boolean
*/
public function handle()
{
$this->authorize();
2020-06-26 13:40:19 +03:00
\DB::transaction(function () {
$this->deleteRelationships($this->dashboard, ['widgets']);
2020-01-07 17:15:00 +03:00
$this->dashboard->users()->detach();
2020-06-26 13:40:19 +03:00
$this->dashboard->delete();
});
2020-01-07 17:15:00 +03:00
return true;
}
/**
* Determine if this action is applicable.
*
* @return void
*/
public function authorize()
{
2020-01-11 22:57:19 +03:00
// Can't delete last dashboard for any shared user
foreach ($this->dashboard->users as $user) {
if ($user->dashboards()->enabled()->count() > 1) {
continue;
}
2020-01-07 17:15:00 +03:00
$message = trans('dashboards.error.delete_last');
throw new \Exception($message);
}
// Check if user can access dashboard
2021-04-16 00:59:43 +03:00
if ($this->isNotUserDashboard($this->dashboard->id)) {
2020-01-07 17:15:00 +03:00
$message = trans('dashboards.error.not_user_dashboard');
throw new \Exception($message);
}
}
}