improved create dashboard job
This commit is contained in:
		@@ -62,7 +62,7 @@ class Dashboards extends Controller
 | 
			
		||||
            $dashboard = $this->dispatch(new CreateDashboard([
 | 
			
		||||
                'company_id' => session('company_id'),
 | 
			
		||||
                'name' => trans_choice('general.dashboards', 1),
 | 
			
		||||
                'with_widgets' => true,
 | 
			
		||||
                'default_widgets' => true,
 | 
			
		||||
            ]));
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -3,9 +3,11 @@
 | 
			
		||||
namespace App\Jobs\Common;
 | 
			
		||||
 | 
			
		||||
use App\Abstracts\Job;
 | 
			
		||||
use App\Models\Auth\User;
 | 
			
		||||
use App\Models\Common\Dashboard;
 | 
			
		||||
use App\Models\Common\Widget;
 | 
			
		||||
use App\Utilities\Widgets;
 | 
			
		||||
use Illuminate\Support\Arr;
 | 
			
		||||
 | 
			
		||||
class CreateDashboard extends Job
 | 
			
		||||
{
 | 
			
		||||
@@ -33,40 +35,89 @@ class CreateDashboard extends Job
 | 
			
		||||
        $this->request['enabled'] = $this->request['enabled'] ?? 1;
 | 
			
		||||
 | 
			
		||||
        \DB::transaction(function () {
 | 
			
		||||
            $users = $this->getUsers();
 | 
			
		||||
 | 
			
		||||
            if (empty($users)) {
 | 
			
		||||
                return;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            $this->dashboard = Dashboard::create($this->request->only(['company_id', 'name', 'enabled']));
 | 
			
		||||
 | 
			
		||||
            $this->attachToUser();
 | 
			
		||||
            $this->dashboard->users()->attach($users);
 | 
			
		||||
 | 
			
		||||
            if ($this->request->has('with_widgets')) {
 | 
			
		||||
                $this->createWidgets();
 | 
			
		||||
            }
 | 
			
		||||
            $this->checkAndCreateWidgets();
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        return $this->dashboard;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected function attachToUser()
 | 
			
		||||
    protected function getUsers()
 | 
			
		||||
    {
 | 
			
		||||
        $list = [];
 | 
			
		||||
 | 
			
		||||
        if ($this->request->has('users')) {
 | 
			
		||||
            $user = $this->request->get('users');
 | 
			
		||||
            $user_ids = Arr::wrap($this->request->get('users'));
 | 
			
		||||
 | 
			
		||||
            foreach($user_ids as $user_id) {
 | 
			
		||||
                $user = User::find($user_id);
 | 
			
		||||
 | 
			
		||||
                if (!$this->shouldCreateDashboardFor($user)) {
 | 
			
		||||
                    continue;
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                $list[] = $user;
 | 
			
		||||
            }
 | 
			
		||||
        } else {
 | 
			
		||||
            $user = user();
 | 
			
		||||
 | 
			
		||||
            if ($this->shouldCreateDashboardFor($user)) {
 | 
			
		||||
                $list[] = $user;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (empty($user)) {
 | 
			
		||||
            return;
 | 
			
		||||
        return $list;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
        $this->dashboard->users()->attach($user);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected function createWidgets()
 | 
			
		||||
    protected function shouldCreateDashboardFor($user)
 | 
			
		||||
    {
 | 
			
		||||
        $widgets = Widgets::getClasses(false);
 | 
			
		||||
        if (empty($user)) {
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // 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;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected function checkAndCreateWidgets()
 | 
			
		||||
    {
 | 
			
		||||
        $sort = 1;
 | 
			
		||||
 | 
			
		||||
        if ($this->request->has('default_widgets')) {
 | 
			
		||||
            $widgets = Widgets::getClasses(false);
 | 
			
		||||
 | 
			
		||||
            $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)
 | 
			
		||||
    {
 | 
			
		||||
        foreach ($widgets as $class => $name) {
 | 
			
		||||
            // It's just an array of classes
 | 
			
		||||
            if (is_numeric($class)) {
 | 
			
		||||
                $class = $name;
 | 
			
		||||
                $name = (new $class())->getDefaultName();
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            Widget::create([
 | 
			
		||||
                'company_id' => $this->dashboard->company_id,
 | 
			
		||||
                'dashboard_id' => $this->dashboard->id,
 | 
			
		||||
 
 | 
			
		||||
@@ -33,7 +33,7 @@ class Dashboards extends Seeder
 | 
			
		||||
        $this->dispatch(new CreateDashboard([
 | 
			
		||||
            'company_id' => $company_id,
 | 
			
		||||
            'name' => trans_choice('general.dashboards', 1),
 | 
			
		||||
            'with_widgets' => true,
 | 
			
		||||
            'default_widgets' => true,
 | 
			
		||||
            'users' => $user_id,
 | 
			
		||||
        ]));
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user