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
|
|
|
public function __construct() |
23
|
|
|
{ |
24
|
|
|
$this->middleware('auth'); |
25
|
|
|
} |
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
|
|
|
public function store(Request $request) |
42
|
|
|
{ |
43
|
|
|
// Validate the form |
44
|
|
|
$request->validate([ |
45
|
|
|
'custID' => 'required', |
46
|
|
|
'name' => 'required', |
47
|
|
|
'type' => 'required' |
48
|
|
|
]); |
49
|
|
|
|
50
|
|
|
$receiver = new FileReceiver('file', $request, HandlerFactory::classFromRequest($request)); |
51
|
|
|
|
52
|
|
|
// Verify that the upload is valid and being processed |
53
|
|
|
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
|
|
|
$save = $receiver->receive(); |
61
|
|
|
|
62
|
|
|
// See if the upload has finished |
63
|
|
|
if($save->isFinished()) |
64
|
|
|
{ |
65
|
|
|
// Set file locationi and clean filename for duplicates |
66
|
|
|
$filePath = config('filesystems.paths.customers').DIRECTORY_SEPARATOR.$request->custID; |
67
|
|
|
$fileName = Files::cleanFileName($filePath, $request->file->getClientOriginalName()); |
68
|
|
|
|
69
|
|
|
// Store the file |
70
|
|
|
$request->file->storeAs($filePath, $fileName); |
71
|
|
|
|
72
|
|
|
// Put the file in the database |
73
|
|
|
$file = Files::create( |
74
|
|
|
[ |
75
|
|
|
'file_name' => $fileName, |
76
|
|
|
'file_link' => $filePath.DIRECTORY_SEPARATOR |
77
|
|
|
]); |
78
|
|
|
|
79
|
|
|
// Get information for system files table |
80
|
|
|
$fileID = $file->file_id; |
81
|
|
|
|
82
|
|
|
// Input the file into the customer files table |
83
|
|
|
CustomerFiles::create([ |
84
|
|
|
'file_id' => $fileID, |
85
|
|
|
'file_type_id' => $request->type, |
86
|
|
|
'cust_id' => $request->custID, |
87
|
|
|
'user_id' => Auth::user()->user_id, |
|
|
|
|
88
|
|
|
'name' => $request->name |
89
|
|
|
]); |
90
|
|
|
|
91
|
|
|
Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id); |
|
|
|
|
92
|
|
|
Log::debug('Submitted Data - ', $request->toArray()); |
93
|
|
|
Log::info('File added for Customer ID-'.$request->custID.' by User ID-'.Auth::user()->user_id.'. New File ID-'.$fileID); |
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
// Get the current progress |
97
|
|
|
$handler = $save->handler(); |
98
|
|
|
|
99
|
|
|
Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id); |
|
|
|
|
100
|
|
|
Log::debug('File being uploaded. Percentage done - '.$handler->getPercentageDone()); |
101
|
|
|
return response()->json([ |
|
|
|
|
102
|
|
|
'done' => $handler->getPercentageDone(), |
103
|
|
|
'status' => true |
104
|
|
|
]); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
// Get the files for the customer |
108
|
|
|
public function show($id) |
109
|
|
|
{ |
110
|
|
|
$files = CustomerFiles::where('cust_id', $id) |
111
|
|
|
->select('name', 'customer_file_types.description as type', |
112
|
|
|
'first_name', 'last_name', 'customer_files.updated_at as added_on', |
113
|
|
|
'files.file_id', 'files.file_name', 'cust_file_id') |
114
|
|
|
->LeftJoin('customer_file_types', 'customer_files.file_type_id', '=', 'customer_file_types.file_type_id') |
115
|
|
|
->LeftJoin('users', 'customer_files.user_id', '=', 'users.user_id') |
116
|
|
|
->Join('files', 'customer_files.file_id', '=', 'files.file_id') |
117
|
|
|
->orderBy('customer_files.file_type_id', 'asc') |
118
|
|
|
->get(); |
119
|
|
|
|
120
|
|
|
return response()->json($files); |
|
|
|
|
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
// Remove a customer file |
124
|
|
View Code Duplication |
public function destroy($id) |
|
|
|
|
125
|
|
|
{ |
126
|
|
|
// Remove the file from SystemFiles table |
127
|
|
|
$data = CustomerFiles::find($id); |
128
|
|
|
$fileID = $data->file_id; |
129
|
|
|
$data->delete(); |
130
|
|
|
|
131
|
|
|
Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id); |
|
|
|
|
132
|
|
|
Log::info('File Deleted For Customer ID-'.$data->custID.' by User ID-'.Auth::user()->user_id.'. File ID-'.$id); |
|
|
|
|
133
|
|
|
|
134
|
|
|
// Delete from system if no longer in use |
135
|
|
|
Files::deleteFile($fileID); |
136
|
|
|
|
137
|
|
|
return response()->json(['success' => true]); |
|
|
|
|
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: