1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Customers; |
4
|
|
|
|
5
|
|
|
use App\Files; |
6
|
|
|
use App\Customers; |
7
|
|
|
use App\CustomerFiles; |
8
|
|
|
use App\CustomerFileTypes; |
9
|
|
|
use Illuminate\Http\Request; |
10
|
|
|
use Illuminate\Http\UploadedFile; |
11
|
|
|
use Illuminate\Support\Facades\Log; |
12
|
|
|
use App\Http\Controllers\Controller; |
13
|
|
|
use Illuminate\Support\Facades\Auth; |
14
|
|
|
use Illuminate\Support\Facades\Route; |
15
|
|
|
use Pion\Laravel\ChunkUpload\Receiver\FileReceiver; |
16
|
|
|
use Pion\Laravel\ChunkUpload\Handler\HandlerFactory; |
17
|
|
|
use Pion\Laravel\ChunkUpload\Handler\AbstractHandler; |
18
|
|
|
use Pion\Laravel\ChunkUpload\Exceptions\UploadMissingFileException; |
19
|
|
|
|
20
|
|
|
class CustomerFilesController extends Controller |
21
|
|
|
{ |
22
|
12 |
|
public function __construct() |
23
|
|
|
{ |
24
|
12 |
|
$this->middleware('auth'); |
25
|
12 |
|
} |
26
|
|
|
|
27
|
|
|
// Get the types of files that can be attached to a customer |
28
|
|
|
// public function getFileTypes() |
29
|
|
|
// { |
30
|
|
|
// $fileTypes = CustomerFileTypes::all(); |
31
|
|
|
// $fTypes = []; |
32
|
|
|
// foreach($fileTypes as $type) |
33
|
|
|
// { |
34
|
|
|
// $fTypes[$type->file_type_id] = $type->description; |
35
|
|
|
// } |
36
|
|
|
|
37
|
|
|
// return response()->json($fileTypes); |
38
|
|
|
// } |
39
|
|
|
|
40
|
|
|
// Store a new customer file |
41
|
2 |
|
public function store(Request $request) |
42
|
|
|
{ |
43
|
|
|
// Validate the form |
44
|
2 |
|
$request->validate([ |
45
|
2 |
|
'cust_id' => 'required', |
46
|
|
|
'name' => 'required', |
47
|
|
|
'type' => 'required' |
48
|
|
|
]); |
49
|
|
|
|
50
|
2 |
|
$receiver = new FileReceiver('file', $request, HandlerFactory::classFromRequest($request)); |
51
|
|
|
|
52
|
|
|
// Verify that the upload is valid and being processed |
53
|
2 |
|
if($receiver->isUploaded() === false) |
54
|
|
|
{ |
55
|
|
|
Log::error('Upload File Missing - '.$request->toArray()); |
|
|
|
|
56
|
|
|
throw new UploadMissingFileException(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
// Receive and process the file |
60
|
2 |
|
$save = $receiver->receive(); |
61
|
|
|
|
62
|
|
|
// See if the upload has finished |
63
|
2 |
|
if($save->isFinished()) |
64
|
|
|
{ |
65
|
2 |
|
$file = $save->getFile(); |
66
|
|
|
// Set file locationi and clean filename for duplicates |
67
|
2 |
|
$filePath = config('filesystems.paths.customers').DIRECTORY_SEPARATOR.$request->cust_id; |
68
|
2 |
|
$fileName = Files::cleanFileName($filePath, $file->getClientOriginalName()); |
69
|
|
|
|
70
|
|
|
// Store the file |
71
|
2 |
|
$file->storeAs($filePath, $fileName); |
72
|
|
|
|
73
|
|
|
// Put the file in the database |
74
|
2 |
|
$file = Files::create( |
75
|
|
|
[ |
76
|
2 |
|
'file_name' => $fileName, |
77
|
2 |
|
'file_link' => $filePath.DIRECTORY_SEPARATOR |
78
|
|
|
]); |
79
|
|
|
|
80
|
|
|
// Get information for system files table |
81
|
2 |
|
$fileID = $file->file_id; |
82
|
|
|
|
83
|
|
|
// Input the file into the customer files table |
84
|
2 |
|
CustomerFiles::create([ |
85
|
2 |
|
'file_id' => $fileID, |
86
|
2 |
|
'file_type_id' => $request->type, |
87
|
2 |
|
'cust_id' => $request->cust_id, |
88
|
2 |
|
'user_id' => Auth::user()->user_id, |
89
|
2 |
|
'name' => $request->name |
90
|
|
|
]); |
91
|
|
|
|
92
|
2 |
|
Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id); |
93
|
2 |
|
Log::debug('Submitted Data - ', $request->toArray()); |
94
|
2 |
|
Log::info('File added for Customer ID-'.$request->custID.' by User ID-'.Auth::user()->user_id.'. New File ID-'.$fileID); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
// Get the current progress |
98
|
2 |
|
$handler = $save->handler(); |
99
|
|
|
|
100
|
2 |
|
Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id); |
101
|
2 |
|
Log::debug('File being uploaded. Percentage done - '.$handler->getPercentageDone()); |
102
|
2 |
|
return response()->json([ |
103
|
2 |
|
'done' => $handler->getPercentageDone(), |
104
|
|
|
'status' => true |
105
|
|
|
]); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
// Get the files for the customer |
109
|
2 |
|
public function show($id) |
110
|
|
|
{ |
111
|
2 |
|
$files = CustomerFiles::where('cust_id', $id) |
112
|
2 |
|
->with('Files') |
113
|
2 |
|
->with('CustomerFileTypes') |
114
|
2 |
|
->with('User') |
115
|
2 |
|
->get(); |
116
|
|
|
|
117
|
2 |
|
return $files; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
// Update the test information of the file, but not the file itself |
121
|
|
|
public function update(Request $request, $id) |
122
|
|
|
{ |
123
|
|
|
$request->validate([ |
124
|
|
|
'cust_id' => 'required', |
125
|
|
|
'name' => 'required', |
126
|
|
|
'type' => 'required' |
127
|
|
|
]); |
128
|
|
|
|
129
|
|
|
CustomerFiles::find($id)->update([ |
130
|
|
|
'name' => $request->name, |
131
|
|
|
'file_type_id' => $request->type |
132
|
|
|
]); |
133
|
|
|
|
134
|
|
|
|
135
|
|
|
return response()->json(['success' => true]); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
// Remove a customer file |
139
|
2 |
|
public function destroy($id) |
140
|
|
|
{ |
141
|
|
|
// Remove the file from SystemFiles table |
142
|
2 |
|
$data = CustomerFiles::find($id); |
143
|
2 |
|
$fileID = $data->file_id; |
144
|
2 |
|
$data->delete(); |
145
|
|
|
|
146
|
2 |
|
Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id); |
147
|
2 |
|
Log::info('File Deleted For Customer ID-'.$data->custID.' by User ID-'.Auth::user()->user_id.'. File ID-'.$id); |
148
|
|
|
|
149
|
|
|
// Delete from system if no longer in use |
150
|
2 |
|
Files::deleteFile($fileID); |
151
|
|
|
|
152
|
2 |
|
return response()->json(['success' => true]); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|