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