DashboardController::reset()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 12
rs 9.4286
cc 2
eloc 6
nc 2
nop 0
1
<?php namespace Modules\Dashboard\Http\Controllers\Admin;
2
3
use Illuminate\Http\Request;
4
use Illuminate\Support\Facades\Response;
5
use Modules\Core\Contracts\Authentication;
6
use Modules\Core\Http\Controllers\Admin\AdminBaseController;
7
use Modules\Dashboard\Repositories\WidgetRepository;
8
use Pingpong\Modules\Repository;
9
10
class DashboardController extends AdminBaseController
11
{
12
    /**
13
     * @var WidgetRepository
14
     */
15
    private $widget;
16
    /**
17
     * @var Authentication
18
     */
19
    private $auth;
20
21
    /**
22
     * @param Repository $modules
23
     * @param WidgetRepository $widget
24
     * @param Authentication $auth
25
     */
26
    public function __construct(Repository $modules, WidgetRepository $widget, Authentication $auth)
27
    {
28
        parent::__construct();
29
        $this->bootWidgets($modules);
30
        $this->widget = $widget;
31
        $this->auth = $auth;
32
    }
33
34
    /**
35
     * Display the dashboard with its widgets
36
     * @return \Illuminate\View\View
37
     */
38
    public function index()
39
    {
40
        $this->requireAssets();
41
42
        $widget = $this->widget->findForUser($this->auth->check()->id);
43
44
        $customWidgets = json_encode(null);
45
        if ($widget) {
46
            $customWidgets = $widget->widgets;
47
        }
48
49
        return view('dashboard::admin.dashboard', compact('customWidgets'));
50
    }
51
52
    /**
53
     * Save the current state of the widgets
54
     * @param Request $request
55
     * @return mixed
56
     */
57
    public function save(Request $request)
58
    {
59
        $widgets = $request->get('grid');
60
61
        if (empty($widgets)) {
62
            return Response::json([false]);
63
        }
64
65
        $this->widget->updateOrCreateForUser($widgets, $this->auth->check()->id);
66
67
        return Response::json([true]);
68
    }
69
70
    /**
71
     * Reset the grid for the current user
72
     */
73
    public function reset()
74
    {
75
        $widget = $this->widget->findForUser($this->auth->check()->id);
76
77
        if (!$widget) {
78
            return redirect()->route('dashboard.index')->with('warning', trans('dashboard::dashboard.reset not needed'));
79
        }
80
81
        $this->widget->destroy($widget);
82
83
        return redirect()->route('dashboard.index')->with('success', trans('dashboard::dashboard.dashboard reset'));
84
    }
85
86
    /**
87
     * Boot widgets for all enabled modules
88
     * @param Repository $modules
89
     */
90
    private function bootWidgets(Repository $modules)
91
    {
92
        foreach ($modules->enabled() as $module) {
93
            if (! $module->widgets) {
94
                continue;
95
            }
96
            foreach ($module->widgets as $widgetClass) {
97
                app($widgetClass)->boot();
98
            }
99
        }
100
    }
101
102
    /**
103
     * Require necessary assets
104
     */
105
    private function requireAssets()
106
    {
107
        $this->assetPipeline->requireJs('lodash.js');
108
        $this->assetPipeline->requireJs('jquery-ui-core.js');
109
        $this->assetPipeline->requireJs('jquery-ui-widget.js');
110
        $this->assetPipeline->requireJs('jquery-ui-mouse.js');
111
        $this->assetPipeline->requireJs('jquery-ui-draggable.js');
112
        $this->assetPipeline->requireJs('jquery-ui-resizable.js');
113
        $this->assetPipeline->requireJs('gridstack.js');
114
        $this->assetPipeline->requireJs('chart.js');
115
        $this->assetPipeline->requireCss('gridstack.css')->before('asgard.css');
116
    }
117
}
118