|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use App\DataTables\LocationDataTable; |
|
6
|
|
|
use App\Location; |
|
7
|
|
|
use App\Site; |
|
8
|
|
|
use App\Http\Requests\EditLocation; |
|
9
|
|
|
|
|
10
|
|
|
class LocationController extends Controller |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Create a new controller instance. |
|
14
|
|
|
*/ |
|
15
|
|
|
public function __construct() |
|
16
|
|
|
{ |
|
17
|
|
|
$this->middleware('auth'); |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Display a listing of the resource. |
|
22
|
|
|
* |
|
23
|
|
|
* @param LocationDataTable $dataTable |
|
24
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\View\View |
|
25
|
|
|
*/ |
|
26
|
|
|
public function index(LocationDataTable $dataTable) |
|
27
|
|
|
{ |
|
28
|
|
|
return $dataTable->render('location.index'); |
|
|
|
|
|
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Show the form for creating a new resource. |
|
33
|
|
|
* |
|
34
|
|
|
* @return \Illuminate\Http\Response |
|
35
|
|
|
*/ |
|
36
|
|
|
public function create() |
|
37
|
|
|
{ |
|
38
|
|
|
$sites = Site::orderBy('name', 'ASC')->get(); |
|
39
|
|
|
return view('location.create', [ 'sites' => $sites ]); |
|
|
|
|
|
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Store a newly created resource in storage. |
|
44
|
|
|
* |
|
45
|
|
|
* @param EditLocation $request |
|
46
|
|
|
* @return \Illuminate\Http\RedirectResponse |
|
47
|
|
|
*/ |
|
48
|
|
View Code Duplication |
public function store(EditLocation $request) |
|
49
|
|
|
{ |
|
50
|
|
|
//Get the site id of the old or newly created site |
|
51
|
|
|
if (!empty($request->input('new_site_name'))) |
|
52
|
|
|
{ |
|
53
|
|
|
//Create a new site |
|
54
|
|
|
$site = Site::create([ 'name' => $request->input('new_site_name') ]); |
|
55
|
|
|
$site_id = $site->id; |
|
56
|
|
|
} else { |
|
57
|
|
|
$site_id = $request->input('site'); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$location = Location::create([ 'name' => $request->input('name'), 'site_id' => $site_id ]); |
|
61
|
|
|
|
|
62
|
|
|
return redirect()->route('location.show', $location->id) |
|
|
|
|
|
|
63
|
|
|
->with('success', 'Location created successfully'); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Display the specified resource. |
|
68
|
|
|
* |
|
69
|
|
|
* @param int $id |
|
70
|
|
|
* @return \Illuminate\Http\Response |
|
71
|
|
|
*/ |
|
72
|
|
|
public function show($id) |
|
73
|
|
|
{ |
|
74
|
|
|
$location = Location::findOrFail($id); |
|
75
|
|
|
$devices = $location->devices()->orderBy('name', 'ASC')->paginate(15); |
|
76
|
|
|
|
|
77
|
|
|
return view('location.show', [ 'location' => $location, 'devices' => $devices ]); |
|
|
|
|
|
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Show the form for editing the specified resource. |
|
82
|
|
|
* |
|
83
|
|
|
* @param int $id |
|
84
|
|
|
* @return \Illuminate\Http\Response |
|
85
|
|
|
*/ |
|
86
|
|
|
public function edit($id) |
|
87
|
|
|
{ |
|
88
|
|
|
$location = Location::findOrFail($id); |
|
89
|
|
|
$sites = Site::orderByRaw("id = ? DESC", $location->site_id)->orderBy('name', 'ASC')->get(); |
|
90
|
|
|
|
|
91
|
|
|
return view('location.edit', [ 'location' => $location, 'sites' => $sites ]); |
|
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Update the specified resource in storage. |
|
96
|
|
|
* |
|
97
|
|
|
* @param EditLocation $request |
|
98
|
|
|
* @param int $id |
|
99
|
|
|
* @return \Illuminate\Http\RedirectResponse |
|
100
|
|
|
*/ |
|
101
|
|
View Code Duplication |
public function update(EditLocation $request, $id) |
|
102
|
|
|
{ |
|
103
|
|
|
//Get the site id of the old or newly created site |
|
104
|
|
|
if (!empty($request->input('new_site_name'))) |
|
105
|
|
|
{ |
|
106
|
|
|
//Create a new site |
|
107
|
|
|
$site = Site::create([ 'name' => $request->input('new_site_name') ]); |
|
108
|
|
|
$site_id = $site->id; |
|
109
|
|
|
} else { |
|
110
|
|
|
$site_id = $request->input('site'); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
//Update the location with the supplied name and the site |
|
114
|
|
|
Location::findOrFail($id)->update([ 'name' => $request->input('name'), 'site_id' => $site_id ]); |
|
115
|
|
|
|
|
116
|
|
|
return redirect()->route('location.show', $id) |
|
|
|
|
|
|
117
|
|
|
->with('success', 'Location updated successfully'); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Remove the specified resource from storage. |
|
122
|
|
|
* |
|
123
|
|
|
* @param int $id |
|
124
|
|
|
* @return \Illuminate\Http\RedirectResponse |
|
125
|
|
|
*/ |
|
126
|
|
|
public function destroy($id) |
|
127
|
|
|
{ |
|
128
|
|
|
Location::findOrFail($id)->delete(); |
|
129
|
|
|
return redirect()->route('location.index') |
|
130
|
|
|
->with('success', 'Location deleted successfully'); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|