1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use Validator; |
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
use App\DataTables\DevicesDataTable; |
8
|
|
|
use App\Device; |
9
|
|
|
use App\Site; |
10
|
|
|
use App\Location; |
11
|
|
|
|
12
|
|
|
class DeviceController extends Controller |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Create a new controller instance. |
16
|
|
|
* |
17
|
|
|
*/ |
18
|
|
|
public function __construct() |
19
|
|
|
{ |
20
|
|
|
$this->middleware('auth'); |
21
|
|
|
// TODO: Setup logging |
22
|
|
|
// $this->middleware('log')->only('index'); |
|
|
|
|
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Display index page and process dataTable ajax request. |
27
|
|
|
* |
28
|
|
|
* @param \App\DataTables\DevicesDataTable $dataTable |
29
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\View\View |
30
|
|
|
*/ |
31
|
|
|
public function index(DevicesDataTable $dataTable) |
32
|
|
|
{ |
33
|
|
|
return $dataTable->render('device.index'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Show create device page. |
38
|
|
|
* |
39
|
|
|
* @return \BladeView|bool|\Illuminate\Contracts\View\Factory|\Illuminate\View\View |
40
|
|
|
*/ |
41
|
|
|
public function create() |
42
|
|
|
{ |
43
|
|
|
return view('device.create'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Show the given device. |
48
|
|
|
* |
49
|
|
|
* @param Request $request |
50
|
|
|
* @param string $id |
51
|
|
|
* @return \BladeView|bool|\Illuminate\Contracts\View\Factory|\Illuminate\View\View |
52
|
|
|
*/ |
53
|
|
|
public function show(Request $request, $id) |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
$device = Device::findOrFail($id); |
56
|
|
|
|
57
|
|
|
return view('device.show', ['device' => $device]); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* View the edit device page. |
62
|
|
|
* |
63
|
|
|
* @param Request $request |
64
|
|
|
* @param string $id |
65
|
|
|
* @return \BladeView|bool|\Illuminate\Contracts\View\Factory|\Illuminate\View\View |
66
|
|
|
*/ |
67
|
|
|
public function edit(Request $request, $id) |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
$device = Device::findOrFail($id); |
70
|
|
|
$location = Location::where('id', $device->location_id)->first(); |
71
|
|
|
if ($location) |
72
|
|
|
$site = Site::where('id', $location->site_id)->first(); |
73
|
|
|
else |
74
|
|
|
$site = null; |
75
|
|
|
|
76
|
|
|
return view('device.edit', ['device' => $device, 'location' => $location, 'site' => $site]); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Update the given device. |
81
|
|
|
* |
82
|
|
|
* @param Request $request |
83
|
|
|
* @param string $id |
84
|
|
|
* @return Response |
85
|
|
|
*/ |
86
|
|
|
public function update(Request $request, $id) |
87
|
|
|
{ |
88
|
|
|
// TODO: Since HTML forms can't make PUT, PATCH, or DELETE requests, you will need |
89
|
|
|
// to add a hidden _method field to spoof these HTTP verbs. The |
90
|
|
|
// method_field helper can create this field for you: |
91
|
|
|
// {{ method_field('PUT') }} |
|
|
|
|
92
|
|
|
|
93
|
|
|
$device = Device::findOrFail($id); |
94
|
|
|
$oldLocationID = $device->location_id; |
95
|
|
|
$location = null; |
|
|
|
|
96
|
|
|
$site = null; |
|
|
|
|
97
|
|
|
|
98
|
|
|
$validator = Validator::make($request->all(), [ |
99
|
|
|
'name' => 'required|string|max:255', |
100
|
|
|
'site' => 'string|max:255', |
101
|
|
|
'location' => 'string|max:255', |
102
|
|
|
]); |
103
|
|
|
|
104
|
|
|
if ($validator->fails()) { |
105
|
|
|
return redirect('device/'.$id.'/edit')->withErrors($validator)->withInput(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
//Check if the site entered by the user is already created |
109
|
|
|
$siteExist = Site::where('name', $request->input('site'))->first(); |
110
|
|
|
if (!$siteExist) |
111
|
|
|
{ |
112
|
|
|
//Create a new site |
113
|
|
|
$site = new Site; |
114
|
|
|
$site->name = $request->input('site'); |
|
|
|
|
115
|
|
|
$site->save(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
//Check if the location entered by the user is already created and connected to the same site |
119
|
|
|
$site = Site::where('name', $request->input('site'))->first(); |
120
|
|
|
$locationExist = Location::where('name', $request->input('location'))->first(); |
121
|
|
|
if (!$locationExist || !$siteExist) |
122
|
|
|
{ |
123
|
|
|
//Create a new location |
124
|
|
|
$location = new Location; |
125
|
|
|
$location->name = $request->input('location'); |
|
|
|
|
126
|
|
|
$location->site_id = $site->id; |
|
|
|
|
127
|
|
|
$location->save(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
//Update the devices name and location_id |
131
|
|
|
$location = Location::where('name', $request->input('location')) |
132
|
|
|
->where('site_id', $site->id)->first(); |
133
|
|
|
$device->location_id = $location->id; |
134
|
|
|
$device->name = $request->input('name'); |
135
|
|
|
$device->save(); |
136
|
|
|
|
137
|
|
|
//If the old site isn't connected to a device then remove it |
138
|
|
|
$this->removeUnusedSite($oldLocationID); |
139
|
|
|
|
140
|
|
|
return redirect('device'); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Deletes a device. |
145
|
|
|
* |
146
|
|
|
* @param Request $request |
147
|
|
|
* @param string $id |
148
|
|
|
* @return Response |
149
|
|
|
*/ |
150
|
|
|
public function destroy(Request $request, $id) |
|
|
|
|
151
|
|
|
{ |
152
|
|
|
$device = Device::findOrFail($id); |
153
|
|
|
|
154
|
|
|
if ($device->trashed()) |
155
|
|
|
{ |
156
|
|
|
// if the user was already deleted then permananetly delete it |
157
|
|
|
Device::destroy($id); |
158
|
|
|
} |
159
|
|
|
else |
160
|
|
|
{ |
161
|
|
|
//Remove the location from the device |
162
|
|
|
$oldLocation_id = $device->location_id; |
163
|
|
|
$device->location_id = null; |
164
|
|
|
$device->save(); |
165
|
|
|
//Remove unused location and site if applicable |
166
|
|
|
$this->removeUnusedSite($oldLocation_id); |
167
|
|
|
|
168
|
|
|
// soft delete the user the first time |
169
|
|
|
$device->delete(); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
return redirect('device'); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Confirms deletion of a device. |
177
|
|
|
* |
178
|
|
|
* @param Request $request |
179
|
|
|
* @param string $id |
180
|
|
|
* @return Response |
181
|
|
|
*/ |
182
|
|
|
public function remove(Request $request, $id) |
|
|
|
|
183
|
|
|
{ |
184
|
|
|
$device = Device::findOrFail($id); |
185
|
|
|
|
186
|
|
|
return view('device.remove', ['device' => $device]); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* If a site is not connected to a device then delete the site |
191
|
|
|
* |
192
|
|
|
* @param int $oldLocationID |
193
|
|
|
*/ |
194
|
|
|
private function removeUnusedSite($oldLocationID) |
195
|
|
|
{ |
196
|
|
|
//Cleanup left over sites and locations |
197
|
|
|
$deviceExist = Device::where('location_id', $oldLocationID)->first(); |
198
|
|
|
if (!$deviceExist && $oldLocationID != null) |
199
|
|
|
{ |
200
|
|
|
$oldLocation = Location::where('id', $oldLocationID)->firstOrFail()->site_id; |
201
|
|
|
Site::where('id', $oldLocation)->delete(); |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.