1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Installer; |
4
|
|
|
|
5
|
|
|
use App\SystemTypes; |
6
|
|
|
use App\SystemCategories; |
7
|
|
|
use App\SystemCustDataTypes; |
8
|
|
|
use Illuminate\Http\Request; |
9
|
|
|
use App\SystemCustDataFields; |
10
|
|
|
use Illuminate\Validation\Rule; |
11
|
|
|
use Illuminate\Support\Facades\Log; |
12
|
|
|
use Illuminate\Support\Facades\Auth; |
13
|
|
|
use App\Http\Controllers\Controller; |
14
|
|
|
use Illuminate\Support\Facades\Route; |
15
|
|
|
|
16
|
|
|
class SystemsController extends Controller |
17
|
|
|
{ |
18
|
|
|
public function __construct() |
19
|
|
|
{ |
20
|
|
|
$this->middleware('auth'); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
// List the systems that can be modified |
24
|
|
View Code Duplication |
public function index() |
|
|
|
|
25
|
|
|
{ |
26
|
|
|
$systems = SystemCategories::with('SystemTypes')->get(); |
27
|
|
|
|
28
|
|
|
Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id); |
|
|
|
|
29
|
|
|
Log::debug('Fetched Data - ', $systems->toArray()); |
30
|
|
|
return view('installer.systemsList', [ |
31
|
|
|
'systems' => $systems |
32
|
|
|
]); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
// Open the form to create a new system |
36
|
|
View Code Duplication |
public function create() |
|
|
|
|
37
|
|
|
{ |
38
|
|
|
$categories = SystemCategories::all(); |
39
|
|
|
$dataTypes = SystemCustDataTypes::orderBy('name', 'ASC')->get(); |
40
|
|
|
|
41
|
|
|
$dropDown = []; |
42
|
|
|
foreach($dataTypes as $type) |
43
|
|
|
{ |
44
|
|
|
$dropDown[] = [ |
45
|
|
|
'value' => $type->data_type_id, |
46
|
|
|
'label' => $type->name |
47
|
|
|
]; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id); |
|
|
|
|
51
|
|
|
return view('installer.newSystem', [ |
52
|
|
|
'categories' => $categories, |
53
|
|
|
'dropDown' => $dropDown |
54
|
|
|
]); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// Store the new system type |
58
|
|
|
public function store(Request $request) |
59
|
|
|
{ |
60
|
|
|
$request->validate([ |
61
|
|
|
'category' => 'required|numeric', |
62
|
|
|
'name' => [ |
63
|
|
|
'required', |
64
|
|
|
'string', |
65
|
|
|
Rule::unique('system_types'), |
66
|
|
|
'regex:/^[a-zA-Z0-9_ ]*$/' |
67
|
|
|
], |
68
|
|
|
'dataOptions' => 'required' |
69
|
|
|
|
70
|
|
|
]); |
71
|
|
|
|
72
|
|
|
$sysData = SystemTypes::create([ |
73
|
|
|
'cat_id' => $request->category, |
74
|
|
|
'name' => $request->name, |
75
|
|
|
'parent_id' => null, |
76
|
|
|
'folder_location' => str_replace(' ', '_', $request->name) |
77
|
|
|
]); |
78
|
|
|
$sysID = $sysData->sys_id; |
79
|
|
|
$i = 0; |
80
|
|
|
|
81
|
|
View Code Duplication |
foreach($request->dataOptions as $field) |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
if(!empty($field)) |
84
|
|
|
{ |
85
|
|
|
if(isset($field['value'])) |
86
|
|
|
{ |
87
|
|
|
$id = $field['value']; |
88
|
|
|
} |
89
|
|
|
else |
90
|
|
|
{ |
91
|
|
|
$newField = SystemCustDataTypes::create([ |
92
|
|
|
'name' => $field['label'] |
93
|
|
|
]); |
94
|
|
|
$id = $newField->data_type_id; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
SystemCustDataFields::create([ |
98
|
|
|
'sys_id' => $sysID, |
99
|
|
|
'data_type_id' => $id, |
100
|
|
|
'order' => $i |
101
|
|
|
]); |
102
|
|
|
$i++; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id); |
|
|
|
|
107
|
|
|
Log::debug('Submitted Data - ', $request->toArray()); |
108
|
|
|
Log::notice('New System Created', ['cat_id' => $request->catName, 'sys_name' => $request->name, 'user_id' => Auth::user()->user_id]); |
|
|
|
|
109
|
|
|
$request->session()->flash('success', 'New System Created'); |
|
|
|
|
110
|
|
|
|
111
|
|
|
return response()->json(['success' => true]); |
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
// Get a JSON array of the system to be edited |
115
|
|
|
public function show($id) |
116
|
|
|
{ |
117
|
|
|
$system = SystemTypes::find($id); |
118
|
|
|
$data = SystemCustDataFields::where('sys_id', $id) |
119
|
|
|
->join('system_cust_data_types', 'system_cust_data_fields.data_type_id', '=', 'system_cust_data_types.data_type_id') |
120
|
|
|
->orderBy('order', 'ASC') |
121
|
|
|
->pluck('name'); |
122
|
|
|
|
123
|
|
|
$sysData = [ |
124
|
|
|
'name' => $system->name, |
125
|
|
|
'data' => $data |
126
|
|
|
]; |
127
|
|
|
|
128
|
|
|
Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id); |
|
|
|
|
129
|
|
|
Log::debug('Fetched Data - ', $sysData); |
130
|
|
|
return response()->json($sysData); |
|
|
|
|
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
// Edit an existing system |
134
|
|
View Code Duplication |
public function edit($id) |
|
|
|
|
135
|
|
|
{ |
136
|
|
|
$system = SystemTypes::find($id); |
137
|
|
|
$dataTypes = SystemCustDataTypes::orderBy('name', 'ASC')->get(); |
138
|
|
|
|
139
|
|
|
$dropDown = []; |
140
|
|
|
foreach($dataTypes as $type) |
141
|
|
|
{ |
142
|
|
|
$dropDown[] = [ |
143
|
|
|
'value' => $type->data_type_id, |
144
|
|
|
'label' => $type->name |
145
|
|
|
]; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id); |
|
|
|
|
149
|
|
|
return view('installer.editSystem', [ |
150
|
|
|
'sys_id' => $id, |
151
|
|
|
'name' => $system->name, |
152
|
|
|
'dropDown' => $dropDown |
153
|
|
|
]); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
// Update the system data |
157
|
|
|
public function update(Request $request, $id) |
158
|
|
|
{ |
159
|
|
|
$request->validate([ |
160
|
|
|
'name' => [ |
161
|
|
|
'required', |
162
|
|
|
'string', |
163
|
|
|
Rule::unique('system_types')->ignore($id, 'sys_id'), |
164
|
|
|
'regex:/^[a-zA-Z0-9_ ]*$/' |
165
|
|
|
] |
166
|
|
|
]); |
167
|
|
|
|
168
|
|
|
// Update the system name |
169
|
|
|
SystemTypes::find($id)->update([ |
170
|
|
|
'name' => $request->name |
171
|
|
|
]); |
172
|
|
|
|
173
|
|
|
// Update the order of the existing data fields |
174
|
|
|
$i = 0; |
175
|
|
|
foreach($request->dataOptions as $data) |
176
|
|
|
{ |
177
|
|
|
$dataID = SystemCustDataTypes::where('name', $data)->first(); |
178
|
|
|
|
179
|
|
|
SystemCustDataFields::where('sys_id', $id)->where('data_type_id', $dataID->data_type_id)->update([ |
180
|
|
|
'order' => $i |
181
|
|
|
]); |
182
|
|
|
|
183
|
|
|
$i++; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
// Process any new data fields |
187
|
|
|
if(!empty($request->newDataOptions)) |
188
|
|
|
{ |
189
|
|
View Code Duplication |
foreach($request->newDataOptions as $field) |
|
|
|
|
190
|
|
|
{ |
191
|
|
|
if(!empty($field)) |
192
|
|
|
{ |
193
|
|
|
if(isset($field['value'])) |
194
|
|
|
{ |
195
|
|
|
$dataID = $field['value']; |
196
|
|
|
} |
197
|
|
|
else |
198
|
|
|
{ |
199
|
|
|
$newField = SystemCustDataTypes::create([ |
200
|
|
|
'name' => $field['label'] |
201
|
|
|
]); |
202
|
|
|
$dataID = $newField->data_type_id; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
SystemCustDataFields::create([ |
206
|
|
|
'sys_id' => $id, |
207
|
|
|
'data_type_id' => $dataID, |
208
|
|
|
'order' => $i |
209
|
|
|
]); |
210
|
|
|
$i++; |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id); |
|
|
|
|
216
|
|
|
Log::debug('Submitted Data - ', $request->toArray()); |
217
|
|
|
Log::notice('System Updated', ['sys_name' => $request->name, 'user_id' => Auth::user()->user_id]); |
|
|
|
|
218
|
|
|
$request->session()->flash('success', 'System Updated'); |
|
|
|
|
219
|
|
|
|
220
|
|
|
return response()->json(['success' => true]); |
|
|
|
|
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Remove the specified resource from storage. |
225
|
|
|
* |
226
|
|
|
* @param int $id |
227
|
|
|
* @return \Illuminate\Http\Response |
228
|
|
|
*/ |
229
|
|
|
// public function destroy($id) |
230
|
|
|
// { |
231
|
|
|
// // |
232
|
|
|
// } |
233
|
|
|
} |
234
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.