1 | <?php namespace Modules\Dashboard\Http\Controllers\Admin; |
||
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) |
||
101 | |||
102 | /** |
||
103 | * Require necessary assets |
||
104 | */ |
||
105 | private function requireAssets() |
||
117 | } |
||
118 |