|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Customers; |
|
4
|
|
|
|
|
5
|
|
|
use App\Traits\FileTrait; |
|
6
|
|
|
|
|
7
|
|
|
use App\Models\Customer; |
|
8
|
|
|
use App\Models\FileUploads; |
|
9
|
|
|
use App\Models\CustomerFile; |
|
10
|
|
|
use App\Models\CustomerFileType; |
|
11
|
|
|
|
|
12
|
|
|
use App\Http\Controllers\Controller; |
|
13
|
|
|
use App\Http\Requests\Customers\CustomerFileRequest; |
|
14
|
|
|
|
|
15
|
|
|
use Illuminate\Support\Facades\Log; |
|
16
|
|
|
use Illuminate\Support\Facades\Auth; |
|
17
|
|
|
|
|
18
|
|
|
class CustomerFilesController extends Controller |
|
19
|
|
|
{ |
|
20
|
|
|
use FileTrait; |
|
21
|
|
|
|
|
22
|
|
|
protected $disk; |
|
23
|
|
|
|
|
24
|
|
|
public function __construct() |
|
25
|
|
|
{ |
|
26
|
|
|
$this->disk = 'customers'; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Upload a new file |
|
31
|
|
|
*/ |
|
32
|
|
|
public function store(CustomerFileRequest $request) |
|
33
|
|
|
{ |
|
34
|
|
|
$cust = Customer::findOrFail($request->cust_id); |
|
35
|
|
|
$cust_id = $cust->cust_id; |
|
36
|
|
|
|
|
37
|
|
|
// If the equipment is shared, it must be assigned to the parent site |
|
38
|
|
|
if(filter_var($request->shared, FILTER_VALIDATE_BOOL) && $cust->parent_id > 0) |
|
39
|
|
|
{ |
|
40
|
|
|
$cust_id = $cust->parent_id; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
// Process the chunk of file being uploaded |
|
44
|
|
|
$status = $this->getChunk($request, $this->disk, $cust_id); |
|
45
|
|
|
|
|
46
|
|
|
// If the file upload is completed, save to database |
|
47
|
|
|
if($status['done'] === 100) |
|
48
|
|
|
{ |
|
49
|
|
|
$newFile = FileUploads::create([ |
|
50
|
|
|
'disk' => $this->disk, |
|
51
|
|
|
'folder' => $cust_id, |
|
52
|
|
|
'file_name' => $status['filename'], |
|
53
|
|
|
'public' => false, |
|
54
|
|
|
]); |
|
55
|
|
|
|
|
56
|
|
|
CustomerFile::create([ |
|
57
|
|
|
'file_id' => $newFile->file_id, |
|
58
|
|
|
'file_type_id' => CustomerFileType::where('description', $request->type)->first()->file_type_id, |
|
59
|
|
|
'cust_id' => $cust_id, |
|
60
|
|
|
'user_id' => $request->user()->user_id, |
|
61
|
|
|
'shared' => filter_var($request->shared, FILTER_VALIDATE_BOOL), |
|
62
|
|
|
'name' => $request->name, |
|
63
|
|
|
]); |
|
64
|
|
|
|
|
65
|
|
|
Log::channel('cust')->info('New file '.$request->name.' has been uploaded for Customer '.$cust->name.' by '.$request->user()->username); |
|
66
|
|
|
return response()->noContent(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
// If upload is still in progress, send current status of upload |
|
70
|
|
|
return response($status); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Ajax call to get the files for a specific customer |
|
75
|
|
|
*/ |
|
76
|
|
|
public function show($id) |
|
77
|
|
|
{ |
|
78
|
|
|
$cust = Customer::findOrFail($id); |
|
79
|
|
|
|
|
80
|
|
|
return CustomerFile::where('cust_id', $id) |
|
81
|
|
|
->when($cust->parent_id, function($q) use ($cust) |
|
82
|
|
|
{ |
|
83
|
|
|
$q->orWhere('cust_id', $cust->parent_id)->where('shared', true); |
|
84
|
|
|
}) |
|
85
|
|
|
->with('FileUpload') |
|
86
|
|
|
->get(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/* |
|
90
|
|
|
* Update the basic information for an uploaded file |
|
91
|
|
|
*/ |
|
92
|
|
|
public function update(CustomerFileRequest $request, $id) |
|
93
|
|
|
{ |
|
94
|
|
|
$cust = Customer::findOrFail($request->cust_id); |
|
95
|
|
|
$cust_id = $cust->cust_id; |
|
96
|
|
|
|
|
97
|
|
|
// If the equipment is shared, it must be assigned to the parent site |
|
98
|
|
|
if($request->shared && $cust->parent_id > 0) |
|
99
|
|
|
{ |
|
100
|
|
|
$cust_id = $cust->parent_id; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
CustomerFile::find($id)->update([ |
|
104
|
|
|
'file_type_id' => CustomerFileType::where('description', $request->type)->first()->file_type_id, |
|
105
|
|
|
'cust_id' => $cust_id, |
|
106
|
|
|
'shared' => $request->shared, |
|
107
|
|
|
'name' => $request->name, |
|
108
|
|
|
]); |
|
109
|
|
|
|
|
110
|
|
|
Log::channel('cust')->info('Customer File ID '.$id.' has been updated by '.Auth::user()->username); |
|
|
|
|
|
|
111
|
|
|
return redirect()->back()->with(['message' => 'File Information Updated', 'type' => 'success']); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Delete a customer File |
|
116
|
|
|
*/ |
|
117
|
|
|
public function destroy($id) |
|
118
|
|
|
{ |
|
119
|
|
|
$this->authorize('delete', CustomerFile::class); |
|
120
|
|
|
|
|
121
|
|
|
CustomerFile::find($id)->delete(); |
|
122
|
|
|
Log::channel('cust')->notice('Customer FIle ID '.$id.' has been deleted by '.Auth::user()->username); |
|
|
|
|
|
|
123
|
|
|
return response()->noContent(); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/* |
|
127
|
|
|
* Restore a file that was deleted |
|
128
|
|
|
*/ |
|
129
|
|
|
public function restore($id) |
|
130
|
|
|
{ |
|
131
|
|
|
$this->authorize('restore', CustomerFile::class); |
|
132
|
|
|
$file = CustomerFile::withTrashed()->where('cust_file_id', $id)->first(); |
|
133
|
|
|
$file->restore(); |
|
134
|
|
|
|
|
135
|
|
|
Log::channel('cust')->info('Customer File ID '.$id.' has been restored for Customer ID '.$file->cust_id.' by '.Auth::user()->username); |
|
|
|
|
|
|
136
|
|
|
|
|
137
|
|
|
return redirect()->back()->with(['message' => 'Customer File restored', 'type' => 'success']); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/* |
|
141
|
|
|
* Permanently delete a file |
|
142
|
|
|
*/ |
|
143
|
|
|
public function forceDelete($id) |
|
144
|
|
|
{ |
|
145
|
|
|
$this->authorize('forceDelete', CustomerFile::class); |
|
146
|
|
|
|
|
147
|
|
|
$file = CustomerFile::withTrashed()->where('cust_file_id', $id)->first(); |
|
148
|
|
|
$fileID = $file->file_id; |
|
149
|
|
|
|
|
150
|
|
|
Log::channel('cust')->alert('Customer File ID '.$id.' has been permanently deleted by '.Auth::user()->username); |
|
|
|
|
|
|
151
|
|
|
$file->forceDelete(); |
|
152
|
|
|
$this->deleteFile($fileID); |
|
153
|
|
|
|
|
154
|
|
|
return redirect()->back()->with(['message' => 'File permanently deleted', 'type' => 'danger']); |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|