2020-01-07 17:15:00 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Jobs\Common;
|
|
|
|
|
|
|
|
use App\Abstracts\Job;
|
|
|
|
use App\Models\Common\Dashboard;
|
|
|
|
use App\Traits\Users;
|
|
|
|
|
|
|
|
class UpdateDashboard extends Job
|
|
|
|
{
|
|
|
|
use Users;
|
|
|
|
|
|
|
|
protected $dashboard;
|
|
|
|
|
|
|
|
protected $request;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new job instance.
|
|
|
|
*
|
|
|
|
* @param $dashboard
|
|
|
|
* @param $request
|
|
|
|
*/
|
|
|
|
public function __construct($dashboard, $request)
|
|
|
|
{
|
|
|
|
$this->dashboard = $dashboard;
|
|
|
|
$this->request = $this->getRequestInstance($request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the job.
|
|
|
|
*
|
|
|
|
* @return Dashboard
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
|
|
|
$this->authorize();
|
|
|
|
|
2020-06-26 13:40:19 +03:00
|
|
|
\DB::transaction(function () {
|
|
|
|
$this->dashboard->update($this->request->all());
|
2020-01-07 17:15:00 +03:00
|
|
|
|
2020-06-26 13:40:19 +03:00
|
|
|
if ($this->request->has('users')) {
|
|
|
|
$this->dashboard->users()->sync($this->request->get('users'));
|
|
|
|
}
|
|
|
|
});
|
2020-01-07 17:15:00 +03:00
|
|
|
|
|
|
|
return $this->dashboard;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if this action is applicable.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function authorize()
|
|
|
|
{
|
2020-01-11 22:57:19 +03:00
|
|
|
// Can't disable last dashboard for any shared user
|
|
|
|
if ($this->request->has('enabled') && !$this->request->get('enabled')) {
|
|
|
|
foreach ($this->dashboard->users as $user) {
|
|
|
|
if ($user->dashboards()->enabled()->count() > 1) {
|
|
|
|
continue;
|
|
|
|
}
|
2020-01-07 17:15:00 +03:00
|
|
|
|
2020-01-11 22:57:19 +03:00
|
|
|
$message = trans('dashboards.error.disable_last');
|
2020-01-07 17:15:00 +03:00
|
|
|
|
2020-01-11 22:57:19 +03:00
|
|
|
throw new \Exception($message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->request->has('users')) {
|
|
|
|
$user = user();
|
|
|
|
|
|
|
|
if (!in_array($user->id, (array) $this->request->get('users')) && ($user->dashboards()->enabled()->count() == 1)) {
|
|
|
|
$message = trans('dashboards.error.delete_last');
|
|
|
|
|
|
|
|
throw new \Exception($message);
|
|
|
|
}
|
2020-01-07 17:15:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if user can access dashboard
|
|
|
|
if (!$this->isUserDashboard($this->dashboard->id)) {
|
|
|
|
$message = trans('dashboards.error.not_user_dashboard');
|
|
|
|
|
|
|
|
throw new \Exception($message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|