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