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