1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LWS\ExportActions\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
use SoapBox\Formatter\Formatter; |
8
|
|
|
use Illuminate\Routing\Controller; |
9
|
|
|
use Illuminate\Support\Facades\Storage; |
10
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet; |
|
|
|
|
11
|
|
|
use PhpOffice\PhpSpreadsheet\Writer\Xlsx; |
|
|
|
|
12
|
|
|
use LWS\ExportActions\Jobs\WritePdf; |
13
|
|
|
use Illuminate\Support\Facades\DB; |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
class Exporter extends Controller |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
public function download(Request $request) |
20
|
|
|
{ |
21
|
|
|
if(DB::table('job_update')->where('id', $request->id)->exists()) { |
22
|
|
|
$file_download_progress = DB::table('job_update')->where('id',$request->id)->first(); |
23
|
|
|
//dd($file_download_progress->file_name); |
24
|
|
|
return response()->download($file_download_progress->file_name); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
else { |
28
|
|
|
return response()->json(['message' => 'Something Went Wrong'],500); |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function progress() |
33
|
|
|
{ |
34
|
|
|
$data = DB::table('job_update')->get()->toJson(); |
35
|
|
|
return response($data,200); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
public function export(Request $requestData) |
40
|
|
|
{ |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
$client = new Client(); |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
$request = new \GuzzleHttp\Psr7\Request('GET', $requestData->url); |
47
|
|
|
$promise = $client->sendAsync($request)->then(function ($responseData) { |
48
|
|
|
|
49
|
|
|
$formatter = Formatter::make($responseData->getBody(), Formatter::JSON); |
50
|
|
|
$csv = $formatter->toCsv(); |
51
|
|
|
|
52
|
|
|
Storage::disk('local')->put('csvfile.csv',$csv); |
53
|
|
|
|
54
|
|
|
}); |
55
|
|
|
|
56
|
|
|
$promise->wait(); |
57
|
|
|
|
58
|
|
|
$file_name = storage_path().'/app/csvfile.csv'; |
59
|
|
|
|
60
|
|
|
switch(strtolower($requestData->format)) { |
61
|
|
|
case 'csv': return $this->csv($file_name);break; |
|
|
|
|
62
|
|
|
case 'excel': return $this->excel($file_name);break; |
|
|
|
|
63
|
|
|
case 'pdf' : return $this->pdf($file_name);break; |
64
|
|
|
} //switch |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
|
68
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
public function csv($file_name) |
73
|
|
|
{ |
74
|
|
|
$headers = [ |
75
|
|
|
'Content-Type' => 'text/csv' |
76
|
|
|
]; |
77
|
|
|
|
78
|
|
|
return response()->download($file_name,"export.csv",$headers); |
79
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
|
83
|
|
|
public function excel($file_name) |
84
|
|
|
{ |
85
|
|
|
$spreadsheet = new Spreadsheet(); |
|
|
|
|
86
|
|
|
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Csv(); |
|
|
|
|
87
|
|
|
|
88
|
|
|
/* Set CSV parsing options */ |
89
|
|
|
$reader->setDelimiter(','); |
90
|
|
|
$reader->setEnclosure('"'); |
91
|
|
|
$reader->setSheetIndex(0); |
92
|
|
|
|
93
|
|
|
/* Load a CSV file and save as a XLS */ |
94
|
|
|
$spreadsheet = $reader->load($file_name); |
95
|
|
|
$writer = new Xlsx($spreadsheet); |
96
|
|
|
|
97
|
|
|
$writer->save('export.xlsx'); |
98
|
|
|
|
99
|
|
|
$spreadsheet->disconnectWorksheets(); |
100
|
|
|
unset($spreadsheet); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function pdf($file_name) |
104
|
|
|
{ |
105
|
|
|
|
106
|
|
|
$csv = array_map('str_getcsv',file($file_name)); |
|
|
|
|
107
|
|
|
|
108
|
|
|
$thead = array_shift($csv); |
109
|
|
|
|
110
|
|
|
$lastKey = array_search(end($thead), $thead); |
|
|
|
|
111
|
|
|
|
112
|
|
|
|
113
|
|
|
|
114
|
|
|
$html = ' |
115
|
|
|
<html> |
116
|
|
|
</head> |
117
|
|
|
<style> |
118
|
|
|
td, th { |
119
|
|
|
border: 1px solid #dddddd; |
120
|
|
|
text-align: left; |
121
|
|
|
padding: 8px; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
th { |
125
|
|
|
|
126
|
|
|
font-family : bold; |
127
|
|
|
|
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
</style> |
131
|
|
|
</head> |
132
|
|
|
'; |
133
|
|
|
|
134
|
|
|
$html .= '<body><table><thead><tr>'; |
135
|
|
|
|
136
|
|
|
|
137
|
|
|
|
138
|
|
|
|
139
|
|
|
for($i = 0 ; $i<count($thead); $i++) { |
|
|
|
|
140
|
|
|
$html.= "<th bgcolor='#ccc'>$thead[$i]</th>"; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
|
144
|
|
|
if($i>=count($thead)) $html.= "</tr></thead>"; |
145
|
|
|
|
146
|
|
|
foreach($csv as $tbody) { |
147
|
|
|
$html .= "<tr>"; |
148
|
|
|
foreach ($tbody as $td ) { |
149
|
|
|
$html .= "<td>$td</td>"; |
150
|
|
|
} |
151
|
|
|
$html .= "</tr>"; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
|
155
|
|
|
$html .= "</table></body>"; |
156
|
|
|
|
157
|
|
|
$id = DB::table('job_update')->insertGetId([ |
158
|
|
|
"name" => "PDF", |
159
|
|
|
"status" => 'processing', |
160
|
|
|
"file_name" => '0' |
161
|
|
|
]); |
162
|
|
|
WritePdf::dispatch($html,$id); |
163
|
|
|
return response()->json(["message"=>"Preparing File For Download..."], 200); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
} |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths