|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Api\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use App\Models\Dashboard; |
|
6
|
|
|
use App\Models\UsersWidgets; |
|
7
|
|
|
use Dingo\Api\Routing\Helpers; |
|
8
|
|
|
use Illuminate\Http\Request; |
|
9
|
|
|
use Validator; |
|
10
|
|
|
|
|
11
|
|
|
class DashboardController extends Controller |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
use Helpers; |
|
15
|
|
|
|
|
16
|
|
|
public function __construct() { |
|
17
|
|
|
|
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Display a listing of all authorized devices |
|
22
|
|
|
* |
|
23
|
|
|
* @return \Illuminate\Http\Response |
|
24
|
|
|
*/ |
|
25
|
|
|
public function index(Request $request) |
|
26
|
|
|
{ |
|
27
|
|
|
$dashboards = Dashboard::allAvailable($request->user())->get(); |
|
|
|
|
|
|
28
|
|
|
return $dashboards; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Show the form for creating a new resource. |
|
33
|
|
|
* |
|
34
|
|
|
* @return \Illuminate\Http\Response|null |
|
35
|
|
|
*/ |
|
36
|
|
|
public function create(Request $request) |
|
37
|
|
|
{ |
|
38
|
|
|
// |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Store a newly created resource in storage. |
|
43
|
|
|
* |
|
44
|
|
|
* @param \Illuminate\Http\Request $request |
|
45
|
|
|
* @return \Illuminate\Http\Response |
|
46
|
|
|
*/ |
|
47
|
|
|
public function store(Request $request) |
|
48
|
|
|
{ |
|
49
|
|
|
$validation = Validator::make($request->all(), [ |
|
50
|
|
|
'name' => 'required|max:255', |
|
51
|
|
|
'access' => 'required', |
|
52
|
|
|
]); |
|
53
|
|
|
if ($validation->passes()) |
|
54
|
|
|
{ |
|
55
|
|
|
$dashboard = new Dashboard; |
|
56
|
|
|
$dashboard->dashboard_name = $request->name; |
|
57
|
|
|
$dashboard->access = $request->access; |
|
58
|
|
|
if ($request->user()->dashboards()->save($dashboard)) |
|
59
|
|
|
{ |
|
60
|
|
|
if (is_numeric($request->copy_from)) |
|
61
|
|
|
{ |
|
62
|
|
|
$duplicate_widgets = Dashboard::find($request->copy_from)->widgets()->get(); |
|
63
|
|
|
foreach ($duplicate_widgets as $tmp_widget) |
|
64
|
|
|
{ |
|
65
|
|
|
/** @var UsersWidgets $tmp_widget */ |
|
66
|
|
|
$new_widget = $tmp_widget->replicate(); |
|
67
|
|
|
$new_widget->user_id = $request->user()->user_id; |
|
68
|
|
|
$new_widget->dashboard_id = $dashboard->dashboard_id; |
|
69
|
|
|
unset($new_widget->user_widget_id); |
|
70
|
|
|
$new_widget->save(); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
return $this->response->array(array('statusText' => 'OK', 'dashboard_id' => $dashboard->dashboard_id)); |
|
74
|
|
|
} |
|
75
|
|
|
else { |
|
76
|
|
|
return $this->response->errorInternal(); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
else { |
|
80
|
|
|
$errors = $validation->errors(); |
|
81
|
|
|
return response()->json($errors, 422); |
|
|
|
|
|
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Display the specified resource. |
|
87
|
|
|
* |
|
88
|
|
|
* @param int $id |
|
89
|
|
|
* @return \Illuminate\Http\Response |
|
90
|
|
|
*/ |
|
91
|
|
|
public function show(Request $request, $id) |
|
92
|
|
|
{ |
|
93
|
|
|
$dashboard = Dashboard::find($id); |
|
94
|
|
|
$widgets = $dashboard->widgets()->get(); |
|
95
|
|
|
|
|
96
|
|
|
return array('dashboard' => $dashboard, 'widgets' => $widgets); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Show the form for editing the specified resource. |
|
101
|
|
|
* |
|
102
|
|
|
* @param int $id |
|
103
|
|
|
* @return \Illuminate\Http\Response |
|
104
|
|
|
*/ |
|
105
|
|
|
public function edit($id) |
|
|
|
|
|
|
106
|
|
|
{ |
|
107
|
|
|
// |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Update the specified resource in storage. |
|
112
|
|
|
* |
|
113
|
|
|
* @param \Illuminate\Http\Request $request |
|
114
|
|
|
* @param int $id |
|
115
|
|
|
* @return \Illuminate\Http\Response |
|
116
|
|
|
*/ |
|
117
|
|
|
public function update(Request $request, $id) |
|
118
|
|
|
{ |
|
119
|
|
|
$validation = Validator::make($request->all(), [ |
|
120
|
|
|
'name' => 'required|max:255', |
|
121
|
|
|
'access' => 'required', |
|
122
|
|
|
]); |
|
123
|
|
|
if ($validation->passes()) |
|
124
|
|
|
{ |
|
125
|
|
|
$dashboard = Dashboard::find($id); |
|
126
|
|
|
$dashboard->dashboard_name = $request->name; |
|
127
|
|
|
$dashboard->access = $request->access; |
|
128
|
|
View Code Duplication |
if ($dashboard->save()) |
|
|
|
|
|
|
129
|
|
|
{ |
|
130
|
|
|
return $this->response->array(array('statusText' => 'OK')); |
|
131
|
|
|
} |
|
132
|
|
|
else { |
|
133
|
|
|
return $this->response->errorInternal(); |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
else { |
|
137
|
|
|
$errors = $validation->errors(); |
|
138
|
|
|
return response()->json($errors, 422); |
|
|
|
|
|
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Remove the specified resource from storage. |
|
144
|
|
|
* |
|
145
|
|
|
* @param int $id |
|
146
|
|
|
* @return \Illuminate\Http\Response |
|
147
|
|
|
*/ |
|
148
|
|
|
public function destroy(Request $request, $id) |
|
149
|
|
|
{ |
|
150
|
|
|
if (Dashboard::where('user_id', $request->user()->user_id)->where('dashboard_id', $id)->delete()) |
|
151
|
|
|
{ |
|
152
|
|
View Code Duplication |
if (UsersWidgets::where('dashboard_id', $id)->delete() >= 0) |
|
|
|
|
|
|
153
|
|
|
{ |
|
154
|
|
|
return $this->response->array(array('statusText' => 'OK')); |
|
155
|
|
|
} |
|
156
|
|
|
else { |
|
157
|
|
|
return $this->response->errorInternal(); |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
else { |
|
161
|
|
|
return $this->response->errorInternal(); |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
public function clear($id) |
|
166
|
|
|
{ |
|
167
|
|
|
if (Dashboard::find($id)->widgets()->delete() >= 0) |
|
168
|
|
|
{ |
|
169
|
|
|
return $this->response->array(array('statusText' => 'OK')); |
|
170
|
|
|
} |
|
171
|
|
|
else { |
|
172
|
|
|
return $this->response->errorInternal(); |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
} |
|
177
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: