|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Auth; |
|
6
|
|
|
use Illuminate\Http\Request; |
|
7
|
|
|
use Validator; |
|
8
|
|
|
use App\Device; |
|
9
|
|
|
use App\Site; |
|
10
|
|
|
use App\Location; |
|
11
|
|
|
use Illuminate\Support\HtmlString; |
|
12
|
|
|
|
|
13
|
|
|
class DashboardController extends Controller |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Create a new controller instance. |
|
17
|
|
|
* |
|
18
|
|
|
*/ |
|
19
|
|
|
public function __construct() |
|
20
|
|
|
{ |
|
21
|
|
|
$this->middleware('auth'); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Show the application dashboard. |
|
26
|
|
|
* |
|
27
|
|
|
* @param Request $request |
|
28
|
|
|
* @return \Illuminate\Http\Response||\Illuminate\Http\JsonResponse |
|
|
|
|
|
|
29
|
|
|
*/ |
|
30
|
|
|
public function index(Request $request) |
|
31
|
|
|
{ |
|
32
|
|
|
$request->validate([ |
|
33
|
|
|
'site_id' => 'sometimes|required|integer|digits_between:1,10', |
|
34
|
|
|
'location_id' => 'sometimes|required|integer|digits_between:1,10', |
|
35
|
|
|
'device_id' => 'sometimes|required|integer|digits_between:1,10', |
|
36
|
|
|
'page' => 'sometimes|required|integer|digits_between:1,10', |
|
37
|
|
|
]); |
|
38
|
|
|
|
|
39
|
|
|
if (count($request->all()) == 0) |
|
40
|
|
|
{ |
|
41
|
|
|
//Get the device, location, and site ids based on the user's preferred device |
|
42
|
|
|
//TODO get the user's preferred device |
|
43
|
|
|
$device = Device::find(3); |
|
44
|
|
|
|
|
45
|
|
|
$location = $device->location ?? null; |
|
46
|
|
|
|
|
47
|
|
|
//If the location exists then set the pagination page to be where the device is located |
|
48
|
|
|
if ($location != null) |
|
49
|
|
|
$request->merge(['page' => $device->dashPageNum(4)]); |
|
50
|
|
|
$site_id = $location->site_id ?? 0; |
|
51
|
|
|
$location_id = $location->id ?? 0; |
|
52
|
|
|
$device_id = $device->id ?? 0; |
|
53
|
|
|
} |
|
54
|
|
|
else |
|
55
|
|
|
{ |
|
56
|
|
|
//Get the active site, location, and device ids |
|
57
|
|
|
$site_id = $request->site_id ?? 0; |
|
58
|
|
|
$location_id = $request->location_id ?? 0; |
|
59
|
|
|
$device_id = $request->device_id ?? 0; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
//Get all sites with the selected site first |
|
63
|
|
|
$sites = Site::select('id', 'name') |
|
64
|
|
|
->orderByRaw("id = ? DESC", $site_id) |
|
65
|
|
|
->orderBy('name', 'ASC') |
|
66
|
|
|
->get(); |
|
67
|
|
|
//Get all locations for the selected site with the selected location first |
|
68
|
|
|
$locations = Location::select('id', 'name', 'site_id') |
|
69
|
|
|
->where('site_id', '=', $sites[0]->id ?? 0) |
|
70
|
|
|
->orderByRaw("id = ? DESC", $location_id) |
|
71
|
|
|
->orderBy('name', 'ASC') |
|
72
|
|
|
->get(); |
|
73
|
|
|
//Get all devices for the selected location |
|
74
|
|
|
$devices = Device::publicDashData() |
|
75
|
|
|
->where('location_id', '=', $locations[0]->id ?? 0) |
|
76
|
|
|
->orderBy('name', 'ASC') |
|
77
|
|
|
->paginate(4); |
|
78
|
|
|
|
|
79
|
|
|
//Get the active device |
|
80
|
|
|
$active_device = $devices->where('id', $device_id)->first(); |
|
81
|
|
|
//Set the active device to the first device in $devices if it is not empty and the original active device wasn't found |
|
82
|
|
|
if (!$devices->isEmpty() && $active_device == null) |
|
83
|
|
|
$active_device = $devices[0]; |
|
84
|
|
|
|
|
85
|
|
|
//Store the active site, location, and device in a collection |
|
86
|
|
|
$active_data = collect(['device' => $active_device, 'location' => $locations[0] ?? null, 'site' => $sites[0] ?? null]); |
|
87
|
|
|
|
|
88
|
|
|
//Use the device_list.blade.php to generate the device table html |
|
89
|
|
|
$html_device_table = view('dashboard.device_list', ['devices' => $devices, 'active_data' => $active_data])->render(); |
|
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
if (\Request::ajax()) |
|
92
|
|
|
return response()->json([ 'active_data' => $active_data, 'devices' => $devices, 'locations' => $locations, |
|
93
|
|
|
'sites' => $sites, 'html_device_table' => $html_device_table ]); |
|
94
|
|
|
else |
|
95
|
|
|
return view('dashboard.index', [ 'active_data' => $active_data, 'devices' => $devices, |
|
96
|
|
|
'locations' => $locations, 'sites' => $sites ]); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Show the development layouts for dashboard. |
|
101
|
|
|
* |
|
102
|
|
|
* @return \Illuminate\Http\Response |
|
103
|
|
|
*/ |
|
104
|
|
|
public function dev_layout() |
|
105
|
|
|
{ |
|
106
|
|
|
return view('dashboard.dev_layout'); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.