1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Installer; |
4
|
|
|
|
5
|
|
|
use App\SystemCategories; |
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
use Illuminate\Validation\Rule; |
8
|
|
|
use Illuminate\Support\Facades\Log; |
9
|
|
|
use Illuminate\Support\Facades\Auth; |
10
|
|
|
use App\Http\Controllers\Controller; |
11
|
|
|
use Illuminate\Support\Facades\Route; |
12
|
|
|
|
13
|
|
|
class CategoriesController extends Controller |
14
|
|
|
{ |
15
|
30 |
|
public function __construct() |
16
|
|
|
{ |
17
|
30 |
|
$this->middleware('auth'); |
18
|
|
|
$this->middleware(function ($request, $next) { |
19
|
24 |
|
$this->authorize('hasAccess', 'Manage Equipment'); |
20
|
18 |
|
return $next($request); |
21
|
30 |
|
}); |
22
|
|
|
|
23
|
30 |
|
} |
24
|
|
|
// View the list of categories |
25
|
4 |
|
public function index() |
26
|
|
|
{ |
27
|
4 |
|
$categories = SystemCategories::all(); |
28
|
|
|
|
29
|
4 |
|
Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id); |
30
|
4 |
|
return view('installer.categoryList', [ |
31
|
4 |
|
'cats' => $categories |
32
|
|
|
]); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
// New category form |
36
|
|
|
// public function create() |
37
|
|
|
// { |
38
|
|
|
// Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id); |
39
|
|
|
// return view('installer.newCategory'); |
40
|
|
|
// } |
41
|
|
|
|
42
|
|
|
// Store the new category form |
43
|
8 |
|
public function store(Request $request) |
44
|
|
|
{ |
45
|
8 |
|
$request->validate([ |
46
|
8 |
|
'name' => 'required|string|unique:system_categories|regex:/^[a-zA-Z0-9_ ]*$/' |
47
|
|
|
]); |
48
|
|
|
|
49
|
4 |
|
SystemCategories::create([ |
50
|
4 |
|
'name' => $request->name |
51
|
|
|
]); |
52
|
|
|
|
53
|
4 |
|
Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id); |
54
|
4 |
|
Log::debug('Submitted Data - ', $request->toArray()); |
55
|
4 |
|
Log::notice('New System Category - '.$request->name.' created by User ID-'.Auth::user()->user_id); |
56
|
|
|
|
57
|
4 |
|
return response()->json(['success' => true]); |
58
|
|
|
// return redirect()->back()->with('success', 'Category Successfully Added. <a href="'.route('installer.systems.create').'">Add System</a>'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
// Get a JSON list of the categories |
62
|
|
|
// public function show($id) |
63
|
|
|
// { |
64
|
|
|
// $categories = SystemCategories::all(); |
65
|
|
|
|
66
|
|
|
// Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id); |
67
|
|
|
// return response()->json($categories); |
68
|
|
|
// } |
69
|
|
|
|
70
|
|
|
// Brind up the Edit Category Form |
71
|
|
|
// public function edit($id) |
72
|
|
|
// { |
73
|
|
|
// $cat = SystemCategories::find($id); |
74
|
|
|
|
75
|
|
|
// if(!$cat) |
76
|
|
|
// { |
77
|
|
|
// Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id); |
78
|
|
|
// Log::debug('Invalid Category ID Selected - '.$id); |
79
|
|
|
// return response(404); |
80
|
|
|
// } |
81
|
|
|
|
82
|
|
|
// Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id); |
83
|
|
|
// Log::debug('Edit Data - ', $cat->toArray()); |
84
|
|
|
// return view('installer.editCategory', [ |
85
|
|
|
// 'details' => $cat |
86
|
|
|
// ]); |
87
|
|
|
// } |
88
|
|
|
|
89
|
|
|
// Submit teh Edit Category form |
90
|
|
|
public function update(Request $request, $id) |
91
|
|
|
{ |
92
|
|
|
$request->validate([ |
93
|
|
|
'name' => [ |
94
|
|
|
'required', |
95
|
|
|
'string', |
96
|
|
|
Rule::unique('system_categories')->ignore($id, 'cat_id'), |
97
|
|
|
'regex:/^[a-zA-Z0-9_ ]*$/' |
98
|
|
|
] |
99
|
|
|
]); |
100
|
|
|
|
101
|
|
|
SystemCategories::find($id)->update([ |
102
|
|
|
'name' => $request->name |
103
|
|
|
]); |
104
|
|
|
|
105
|
|
|
Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id); |
106
|
|
|
Log::debug('Submitted Data - ', $request->toArray()); |
107
|
|
|
Log::notice('Category ID-'.$id.' updated by User ID-'.Auth::user()->user_id); |
108
|
|
|
// return redirect()->back()->with('success', 'Category Successfully Modified. <a href="'.route('installer.systems.create').'">Add System</a>'); |
109
|
|
|
return response()->json(['success' => true]); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Remove the specified resource from storage. |
114
|
|
|
* |
115
|
|
|
* @param int $id |
116
|
|
|
* @return \Illuminate\Http\Response |
117
|
|
|
*/ |
118
|
6 |
|
public function destroy($id) |
119
|
|
|
{ |
120
|
|
|
try { |
121
|
6 |
|
SystemCategories::find($id)->delete(); |
122
|
4 |
|
return response()->json(['success' => true, 'reason' => 'Category Successfully Deleted']); |
123
|
|
|
} |
124
|
2 |
|
catch (\Illuminate\Database\QueryException $e) |
125
|
|
|
{ |
126
|
2 |
|
return response()->json(['success' => false, 'reason' => 'Category still in use. You must delete all systems attached to this category first.']); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|