|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Systems; |
|
4
|
|
|
|
|
5
|
|
|
use App\Files; |
|
6
|
|
|
use App\SystemTypes; |
|
7
|
|
|
use App\SystemFiles; |
|
8
|
|
|
use App\SystemFileTypes; |
|
9
|
|
|
use Illuminate\Http\Request; |
|
10
|
|
|
use Illuminate\Support\Facades\Log; |
|
11
|
|
|
use App\Http\Controllers\Controller; |
|
12
|
|
|
use Illuminate\Support\Facades\Auth; |
|
13
|
|
|
|
|
14
|
|
|
class SystemFilesController extends Controller |
|
15
|
|
|
{ |
|
16
|
|
|
public function __construct() |
|
17
|
|
|
{ |
|
18
|
|
|
$this->middleware('auth'); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
// Store a new system file |
|
22
|
|
|
public function store(Request $request) |
|
23
|
|
|
{ |
|
24
|
|
|
// Validate the form |
|
25
|
|
|
$request->validate([ |
|
26
|
|
|
'name' => 'required', |
|
27
|
|
|
'sysID' => 'required', |
|
28
|
|
|
'type' => 'required', |
|
29
|
|
|
'file' => 'required' |
|
30
|
|
|
]); |
|
31
|
|
|
|
|
32
|
|
|
// Get the system information |
|
33
|
|
|
$sys = SystemTypes::where('sys_id', $request->sysID) |
|
34
|
|
|
->join('system_categories', 'system_types.cat_id', '=', 'system_categories.cat_id') |
|
35
|
|
|
->select('system_types.*', 'system_categories.name AS cat_name') |
|
36
|
|
|
->first(); |
|
37
|
|
|
|
|
38
|
|
|
// Set the file location and clean the file name for storage |
|
39
|
|
|
$filePath = config('filesystems.paths.systems') |
|
40
|
|
|
.DIRECTORY_SEPARATOR.strtolower($sys->cat_name) |
|
41
|
|
|
.DIRECTORY_SEPARATOR.$sys->folder_location; |
|
42
|
|
|
|
|
43
|
|
|
$fileName = Files::cleanFileName($filePath, $request->file->getClientOriginalName()); |
|
44
|
|
|
|
|
45
|
|
|
// Store the file and place it in the database |
|
46
|
|
|
$request->file->storeAs($filePath, $fileName); |
|
47
|
|
|
$file = Files::create([ |
|
48
|
|
|
'file_name' => $fileName, |
|
49
|
|
|
'file_link' => $filePath.DIRECTORY_SEPARATOR |
|
50
|
|
|
]); |
|
51
|
|
|
|
|
52
|
|
|
// Get the information for the system files table |
|
53
|
|
|
$fileID = $file->file_id; |
|
54
|
|
|
$typeID = SystemFileTypes::where('description', $request->type)->first()->type_id; |
|
55
|
|
|
|
|
56
|
|
|
// Attach file to system type |
|
57
|
|
|
SystemFiles::create([ |
|
58
|
|
|
'sys_id' => $request->sysID, |
|
59
|
|
|
'type_id' => $typeID, |
|
60
|
|
|
'file_id' => $fileID, |
|
61
|
|
|
'name' => $request->name, |
|
62
|
|
|
'description' => $request->description, |
|
63
|
|
|
'user_id' => Auth::user()->user_id |
|
64
|
|
|
]); |
|
65
|
|
|
|
|
66
|
|
|
Log::info('File ID-'.$fileID.' Added For System ID-'.$request->sysID.' By User ID-'.Auth::user()->user_id); |
|
67
|
|
|
|
|
68
|
|
|
return response()->json(['success' => true]); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
// Return JSON array of the system files |
|
72
|
|
|
public function show($id) |
|
73
|
|
|
{ |
|
74
|
|
|
$fileTypes = SystemFileTypes::all(); |
|
75
|
|
|
$files = SystemFiles::where('sys_id', $id) |
|
76
|
|
|
->join('files', 'system_files.file_id', '=', 'files.file_id') |
|
77
|
|
|
->join('users', 'system_files.user_id', '=', 'users.user_id') |
|
78
|
|
|
->get(); |
|
79
|
|
|
|
|
80
|
|
|
$i = 0; |
|
81
|
|
|
$fileArr = []; |
|
82
|
|
|
foreach($fileTypes as $type) |
|
83
|
|
|
{ |
|
84
|
|
|
$fileArr[$i] = [ |
|
85
|
|
|
'description' => $type->description, |
|
86
|
|
|
'data' => false |
|
87
|
|
|
]; |
|
88
|
|
|
|
|
89
|
|
|
foreach($files as $file) |
|
90
|
|
|
{ |
|
91
|
|
|
if($file->type_id == $type->type_id) |
|
92
|
|
|
{ |
|
93
|
|
|
$fileArr[$i]['data'][] = [ |
|
94
|
|
|
'id' => $file->sys_file_id, |
|
95
|
|
|
'url' => route('download', [$file->file_id, $file->file_name]), |
|
96
|
|
|
'name' => $file->name, |
|
97
|
|
|
'description' => $file->description, |
|
98
|
|
|
'user' => $file->first_name.' '.$file->last_name, |
|
99
|
|
|
'added' => date('M d, Y', strtotime($file->created_at)) |
|
100
|
|
|
]; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
$i++; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return response()->json($fileArr); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
// Update the file information |
|
111
|
|
|
public function update(Request $request, $id) |
|
112
|
|
|
{ |
|
113
|
|
|
SystemFiles::find($id)->update([ |
|
114
|
|
|
'name' => $request->name, |
|
115
|
|
|
'description' => $request->desc |
|
116
|
|
|
]); |
|
117
|
|
|
|
|
118
|
|
|
Log::info('File Information on System File ID-'.$id.' Updated by User ID-'.Auth::user()->user_id); |
|
119
|
|
|
|
|
120
|
|
|
return response()->json(['success' => true]); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
// Replace the file with a new one |
|
124
|
|
|
public function replace(Request $request) |
|
125
|
|
|
{ |
|
126
|
|
|
// Validate the form |
|
127
|
|
|
$request->validate(['file' => 'required']); |
|
128
|
|
|
|
|
129
|
|
|
// Get the original file information |
|
130
|
|
|
$origData = SystemFiles::find($request->fileID)->with('files')->first(); |
|
131
|
|
|
$filePath = $origData->files->file_link; |
|
|
|
|
|
|
132
|
|
|
// Clean the filename |
|
133
|
|
|
$fileName = Files::cleanFileName($filePath, $request->file->getClientOriginalName()); |
|
134
|
|
|
|
|
135
|
|
|
// Store the file |
|
136
|
|
|
$request->file->storeAs($filePath, $fileName); |
|
137
|
|
|
|
|
138
|
|
|
// Put the file in the database |
|
139
|
|
|
$file = Files::create( |
|
140
|
|
|
[ |
|
141
|
|
|
'file_name' => $fileName, |
|
142
|
|
|
'file_link' => $filePath.DIRECTORY_SEPARATOR |
|
143
|
|
|
]); |
|
144
|
|
|
|
|
145
|
|
|
// Get information for system files table |
|
146
|
|
|
$fileID = $file->file_id; |
|
147
|
|
|
|
|
148
|
|
|
SystemFiles::find($request->fileID)->update([ |
|
149
|
|
|
'file_id' => $fileID |
|
150
|
|
|
]); |
|
151
|
|
|
|
|
152
|
|
|
Log::info('System File ID-'.$fileID.' Replaced by User ID-'.Auth::user()->user_id); |
|
153
|
|
|
|
|
154
|
|
|
return response()->json(['success' => true]); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
// Delete the system file |
|
158
|
|
|
public function destroy($id) |
|
159
|
|
|
{ |
|
160
|
|
|
// Remove the file from SystemFiles table |
|
161
|
|
|
$data = SystemFiles::find($id); |
|
162
|
|
|
$fileID = $data->file_id; |
|
163
|
|
|
|
|
164
|
|
|
Log::info('System File ID-'.$fileID.' Deleted For System ID-'.$data->sys_id.' by User ID-'.Auth::user()->user_id); |
|
165
|
|
|
|
|
166
|
|
|
$data->delete(); |
|
167
|
|
|
|
|
168
|
|
|
// Delete from system if no longer in use |
|
169
|
|
|
Files::deleteFile($fileID); |
|
170
|
|
|
|
|
171
|
|
|
return response()->json(['success' => true]); |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|