@@ -7,20 +7,20 @@ |
||
7 | 7 | class ImportExportLog |
8 | 8 | { |
9 | 9 | |
10 | - public function logImportError(Import $import, $data, $message) |
|
11 | - { |
|
12 | - // Create new log |
|
13 | - return $import->importLogs()->create([ |
|
14 | - 'data' => $data, |
|
15 | - 'message' => $message, |
|
16 | - ]); |
|
17 | - } |
|
10 | + public function logImportError(Import $import, $data, $message) |
|
11 | + { |
|
12 | + // Create new log |
|
13 | + return $import->importLogs()->create([ |
|
14 | + 'data' => $data, |
|
15 | + 'message' => $message, |
|
16 | + ]); |
|
17 | + } |
|
18 | 18 | |
19 | 19 | |
20 | - public function getLogs($id) |
|
21 | - { |
|
22 | - // Get all logs of a import or export process |
|
23 | - return Import::findOrFail( $id )->importLogs()->get( [ 'data', 'message' ] )->toArray(); |
|
24 | - } |
|
20 | + public function getLogs($id) |
|
21 | + { |
|
22 | + // Get all logs of a import or export process |
|
23 | + return Import::findOrFail( $id )->importLogs()->get( [ 'data', 'message' ] )->toArray(); |
|
24 | + } |
|
25 | 25 | |
26 | 26 | } |
@@ -20,7 +20,7 @@ |
||
20 | 20 | public function getLogs($id) |
21 | 21 | { |
22 | 22 | // Get all logs of a import or export process |
23 | - return Import::findOrFail( $id )->importLogs()->get( [ 'data', 'message' ] )->toArray(); |
|
23 | + return Import::findOrFail($id)->importLogs()->get(['data', 'message'])->toArray(); |
|
24 | 24 | } |
25 | 25 | |
26 | 26 | } |
@@ -9,24 +9,24 @@ discard block |
||
9 | 9 | class Export |
10 | 10 | { |
11 | 11 | |
12 | - public function export(Builder $query, $ext = 'xls') |
|
13 | - { |
|
14 | - // Create new export |
|
15 | - $export = new ModelExport; |
|
12 | + public function export(Builder $query, $ext = 'xls') |
|
13 | + { |
|
14 | + // Create new export |
|
15 | + $export = new ModelExport; |
|
16 | 16 | |
17 | - $export->type = $ext; |
|
18 | - $export->query = $query->getModel(); |
|
17 | + $export->type = $ext; |
|
18 | + $export->query = $query->getModel(); |
|
19 | 19 | |
20 | - $export->save(); |
|
20 | + $export->save(); |
|
21 | 21 | |
22 | - ExportJob::dispatch( $export )->onQueue( 'exporting' ); |
|
22 | + ExportJob::dispatch( $export )->onQueue( 'exporting' ); |
|
23 | 23 | |
24 | - return $export; |
|
25 | - } |
|
24 | + return $export; |
|
25 | + } |
|
26 | 26 | |
27 | 27 | |
28 | 28 | |
29 | - public function showExportStatus($id) |
|
29 | + public function showExportStatus($id) |
|
30 | 30 | { |
31 | 31 | // Map export instance |
32 | 32 | $export = ModelExport::findOrFail( $id ); |
@@ -50,11 +50,11 @@ discard block |
||
50 | 50 | |
51 | 51 | if ( $export->result_rows > 0 ) { |
52 | 52 | |
53 | - $data['progress'] = round( ( $export->row_processed / $export->result_rows ) * 100 ); |
|
53 | + $data['progress'] = round( ( $export->row_processed / $export->result_rows ) * 100 ); |
|
54 | 54 | |
55 | 55 | } else { |
56 | 56 | |
57 | - $data['progress'] = 0; |
|
57 | + $data['progress'] = 0; |
|
58 | 58 | |
59 | 59 | } |
60 | 60 |
@@ -19,7 +19,7 @@ discard block |
||
19 | 19 | |
20 | 20 | $export->save(); |
21 | 21 | |
22 | - ExportJob::dispatch( $export )->onQueue( 'exporting' ); |
|
22 | + ExportJob::dispatch($export)->onQueue('exporting'); |
|
23 | 23 | |
24 | 24 | return $export; |
25 | 25 | } |
@@ -29,9 +29,9 @@ discard block |
||
29 | 29 | public function showExportStatus($id) |
30 | 30 | { |
31 | 31 | // Map export instance |
32 | - $export = ModelExport::findOrFail( $id ); |
|
32 | + $export = ModelExport::findOrFail($id); |
|
33 | 33 | |
34 | - return view( 'importexport::export.progress', compact( 'id' ) ); |
|
34 | + return view('importexport::export.progress', compact('id')); |
|
35 | 35 | } |
36 | 36 | |
37 | 37 | |
@@ -44,13 +44,13 @@ discard block |
||
44 | 44 | public function returnExportProgress($id) |
45 | 45 | { |
46 | 46 | // Map export instance |
47 | - $export = ModelExport::findOrFail( $id ); |
|
47 | + $export = ModelExport::findOrFail($id); |
|
48 | 48 | |
49 | 49 | $data['status'] = 200; |
50 | 50 | |
51 | - if ( $export->result_rows > 0 ) { |
|
51 | + if ($export->result_rows > 0) { |
|
52 | 52 | |
53 | - $data['progress'] = round( ( $export->row_processed / $export->result_rows ) * 100 ); |
|
53 | + $data['progress'] = round(($export->row_processed / $export->result_rows) * 100); |
|
54 | 54 | |
55 | 55 | } else { |
56 | 56 | |
@@ -59,23 +59,23 @@ discard block |
||
59 | 59 | } |
60 | 60 | |
61 | 61 | // If progress completed return successful imported rows count |
62 | - if ( $data['progress'] == 100 ) { |
|
63 | - $data['exported'] = route( 'ladybirdweb.export.download', $export->id ); |
|
62 | + if ($data['progress'] == 100) { |
|
63 | + $data['exported'] = route('ladybirdweb.export.download', $export->id); |
|
64 | 64 | } |
65 | 65 | |
66 | - return response()->json( $data ); |
|
66 | + return response()->json($data); |
|
67 | 67 | } |
68 | 68 | |
69 | 69 | |
70 | 70 | public function downloadExportedFile($id) |
71 | 71 | { |
72 | 72 | // Map export instance |
73 | - $export = ModelExport::findOrFail( $id ); |
|
73 | + $export = ModelExport::findOrFail($id); |
|
74 | 74 | |
75 | - if ( ! file_exists( storage_path( 'app/exports' ) . '/' . $export->file ) || empty( $export->file ) ) { |
|
75 | + if (!file_exists(storage_path('app/exports').'/'.$export->file) || empty($export->file)) { |
|
76 | 76 | abort(404); |
77 | 77 | } |
78 | 78 | |
79 | - return response()->download( storage_path( 'app/exports/' . $export->file ) ); |
|
79 | + return response()->download(storage_path('app/exports/'.$export->file)); |
|
80 | 80 | } |
81 | 81 | } |
@@ -8,10 +8,10 @@ discard block |
||
8 | 8 | class ImportHandler |
9 | 9 | { |
10 | 10 | |
11 | - function process(Import $import, callable $callback) |
|
12 | - { |
|
11 | + function process(Import $import, callable $callback) |
|
12 | + { |
|
13 | 13 | |
14 | - // CSV header row show be excluded |
|
14 | + // CSV header row show be excluded |
|
15 | 15 | $csv_header = true; |
16 | 16 | |
17 | 17 | // Read csv |
@@ -30,21 +30,21 @@ discard block |
||
30 | 30 | |
31 | 31 | } else { |
32 | 32 | |
33 | - // Drop ignore columns |
|
33 | + // Drop ignore columns |
|
34 | 34 | $data = array_combine( $import->model_map, $csv_line ); |
35 | 35 | unset( $data[''] ); |
36 | 36 | |
37 | 37 | // Call user callback with data |
38 | 38 | if ( $callback( $data ) ) { |
39 | 39 | |
40 | - // If successful -> update imported rows |
|
41 | - $import->row_imported = $import->row_imported + 1; |
|
40 | + // If successful -> update imported rows |
|
41 | + $import->row_imported = $import->row_imported + 1; |
|
42 | 42 | |
43 | 43 | } |
44 | 44 | |
45 | - // Update porcessed rows |
|
46 | - $import->row_processed = $processed_row; |
|
47 | - $import->save(); |
|
45 | + // Update porcessed rows |
|
46 | + $import->row_processed = $processed_row; |
|
47 | + $import->save(); |
|
48 | 48 | |
49 | 49 | $processed_row++; |
50 | 50 | } |
@@ -58,5 +58,5 @@ discard block |
||
58 | 58 | $import->completed_at = Carbon::now(); |
59 | 59 | $import->save(); |
60 | 60 | |
61 | - } |
|
61 | + } |
|
62 | 62 | } |
@@ -15,15 +15,15 @@ discard block |
||
15 | 15 | $csv_header = true; |
16 | 16 | |
17 | 17 | // Read csv |
18 | - $file = fopen( storage_path( 'app/' . $import->file ), 'r' ); |
|
18 | + $file = fopen(storage_path('app/'.$import->file), 'r'); |
|
19 | 19 | |
20 | 20 | // Processed csv rows |
21 | 21 | $processed_row = 1; |
22 | 22 | |
23 | 23 | // Go over csv data line by line |
24 | - while ( $csv_line = fgetcsv( $file ) ) { |
|
24 | + while ($csv_line = fgetcsv($file)) { |
|
25 | 25 | |
26 | - if ( $csv_header ) { |
|
26 | + if ($csv_header) { |
|
27 | 27 | |
28 | 28 | // Skip csv header |
29 | 29 | $csv_header = false; |
@@ -31,11 +31,11 @@ discard block |
||
31 | 31 | } else { |
32 | 32 | |
33 | 33 | // Drop ignore columns |
34 | - $data = array_combine( $import->model_map, $csv_line ); |
|
35 | - unset( $data[''] ); |
|
34 | + $data = array_combine($import->model_map, $csv_line); |
|
35 | + unset($data['']); |
|
36 | 36 | |
37 | 37 | // Call user callback with data |
38 | - if ( $callback( $data ) ) { |
|
38 | + if ($callback($data)) { |
|
39 | 39 | |
40 | 40 | // If successful -> update imported rows |
41 | 41 | $import->row_imported = $import->row_imported + 1; |
@@ -52,7 +52,7 @@ discard block |
||
52 | 52 | } |
53 | 53 | |
54 | 54 | // Close csv file |
55 | - fclose( $file ); |
|
55 | + fclose($file); |
|
56 | 56 | |
57 | 57 | // Update import as done |
58 | 58 | $import->completed_at = Carbon::now(); |
@@ -37,7 +37,7 @@ discard block |
||
37 | 37 | public function handle() |
38 | 38 | { |
39 | 39 | // Custom file path |
40 | - $file_path = date( 'Y/m' ); |
|
40 | + $file_path = date('Y/m'); |
|
41 | 41 | |
42 | 42 | // Export instance assign to variable |
43 | 43 | $export = $this->export; |
@@ -47,19 +47,19 @@ discard block |
||
47 | 47 | $export->save(); |
48 | 48 | |
49 | 49 | // Create export file |
50 | - $excel = Excel::create( 'export-' . date( 'dmYhis' ), function($excel) use ($export) { |
|
50 | + $excel = Excel::create('export-'.date('dmYhis'), function($excel) use ($export) { |
|
51 | 51 | |
52 | 52 | // Create new sheet |
53 | - $excel->sheet( 'export', function($sheet) use ($export) { |
|
53 | + $excel->sheet('export', function($sheet) use ($export) { |
|
54 | 54 | |
55 | 55 | // Retrive data in chunk |
56 | - $export->query->chunk( 10, function($data) use ($export, $sheet) { |
|
56 | + $export->query->chunk(10, function($data) use ($export, $sheet) { |
|
57 | 57 | |
58 | 58 | // Process chunk data |
59 | - foreach ( $data as $row ) { |
|
59 | + foreach ($data as $row) { |
|
60 | 60 | |
61 | 61 | // Append row to sheet |
62 | - $sheet->appendRow( $row->toArray() ); |
|
62 | + $sheet->appendRow($row->toArray()); |
|
63 | 63 | |
64 | 64 | } |
65 | 65 | |
@@ -71,10 +71,10 @@ discard block |
||
71 | 71 | |
72 | 72 | }); |
73 | 73 | |
74 | - })->store( $this->export->type, storage_path( 'app/exports/' . $file_path ), true ); |
|
74 | + })->store($this->export->type, storage_path('app/exports/'.$file_path), true); |
|
75 | 75 | |
76 | 76 | // Update export data |
77 | - $export->file = $file_path . '/' . $excel['file']; |
|
77 | + $export->file = $file_path.'/'.$excel['file']; |
|
78 | 78 | $export->completed_at = Carbon::now(); |
79 | 79 | $export->save(); |
80 | 80 |
@@ -3,15 +3,15 @@ |
||
3 | 3 | Route::middleware('web')->group(function() { |
4 | 4 | |
5 | 5 | // Post import form |
6 | -Route::post( config('import.import_upload'), [ 'as' => 'ladybirdweb.import', 'uses' => 'Ladybirdweb\ImportExport\Import@uploadImportFile']); |
|
6 | +Route::post(config('import.import_upload'), ['as' => 'ladybirdweb.import', 'uses' => 'Ladybirdweb\ImportExport\Import@uploadImportFile']); |
|
7 | 7 | |
8 | 8 | // Ajax GET import progress |
9 | -Route::get( config( 'import.import_progress' ), [ 'as' => 'ladybirdweb.import.ajax.progress', 'uses' => 'Ladybirdweb\ImportExport\Import@returnImportProgress']); |
|
9 | +Route::get(config('import.import_progress'), ['as' => 'ladybirdweb.import.ajax.progress', 'uses' => 'Ladybirdweb\ImportExport\Import@returnImportProgress']); |
|
10 | 10 | |
11 | 11 | // Ajax GET export progress |
12 | -Route::get( config( 'export.export_progress' ), [ 'as' => 'ladybirdweb.export.ajax.progress', 'uses' => 'Ladybirdweb\ImportExport\Export@returnExportProgress']); |
|
12 | +Route::get(config('export.export_progress'), ['as' => 'ladybirdweb.export.ajax.progress', 'uses' => 'Ladybirdweb\ImportExport\Export@returnExportProgress']); |
|
13 | 13 | |
14 | 14 | // GET export download |
15 | -Route::get( config( 'export.export_download' ), [ 'as' => 'ladybirdweb.export.download', 'uses' => 'Ladybirdweb\ImportExport\Export@downloadExportedFile']); |
|
15 | +Route::get(config('export.export_download'), ['as' => 'ladybirdweb.export.download', 'uses' => 'Ladybirdweb\ImportExport\Export@downloadExportedFile']); |
|
16 | 16 | |
17 | 17 | }); |
@@ -26,7 +26,7 @@ discard block |
||
26 | 26 | { |
27 | 27 | $route = $this->upload_route; |
28 | 28 | |
29 | - return view( 'importexport::import.import', compact( 'route' ) ); |
|
29 | + return view('importexport::import.import', compact('route')); |
|
30 | 30 | } |
31 | 31 | |
32 | 32 | |
@@ -42,7 +42,7 @@ discard block |
||
42 | 42 | // Store file path and model class to db |
43 | 43 | $import = ModelImport::create([ |
44 | 44 | 'file' => $path, |
45 | - 'file_rows' => count( file( storage_path( 'app/' . $path ) ) ) - 1, |
|
45 | + 'file_rows' => count(file(storage_path('app/'.$path))) - 1, |
|
46 | 46 | 'db_cols' => $columns |
47 | 47 | ]); |
48 | 48 | |
@@ -63,18 +63,18 @@ discard block |
||
63 | 63 | 'file' => 'required|mimes:csv,txt' |
64 | 64 | ]); |
65 | 65 | |
66 | - if ( $validation->fails() ) { |
|
66 | + if ($validation->fails()) { |
|
67 | 67 | // Set validator errors |
68 | 68 | $this->import_errors = $validation->errors(); |
69 | 69 | |
70 | 70 | return ['status' => 'failed']; |
71 | 71 | } |
72 | 72 | |
73 | - return Plupload::receive('file', function ($file) { |
|
73 | + return Plupload::receive('file', function($file) { |
|
74 | 74 | |
75 | 75 | // Upload file to storage/app/imports |
76 | - $path = Storage::putFileAs( 'imports', $file, |
|
77 | - 'import-' . time() . '.' . $file->getClientOriginalExtension() ); |
|
76 | + $path = Storage::putFileAs('imports', $file, |
|
77 | + 'import-'.time().'.'.$file->getClientOriginalExtension()); |
|
78 | 78 | |
79 | 79 | return ['status' => 'ready', 'path' => $path]; |
80 | 80 | }); |
@@ -89,9 +89,9 @@ discard block |
||
89 | 89 | public function showImportStatus($id) |
90 | 90 | { |
91 | 91 | // Map import instance |
92 | - $import = ModelImport::findOrFail( $id ); |
|
92 | + $import = ModelImport::findOrFail($id); |
|
93 | 93 | |
94 | - return view( 'importexport::import.progress', compact( 'id' ) ); |
|
94 | + return view('importexport::import.progress', compact('id')); |
|
95 | 95 | } |
96 | 96 | |
97 | 97 | /** |
@@ -103,22 +103,22 @@ discard block |
||
103 | 103 | public function showColumnsMapForm($id) |
104 | 104 | { |
105 | 105 | // Map import instance |
106 | - $import = ModelImport::findOrFail( $id ); |
|
106 | + $import = ModelImport::findOrFail($id); |
|
107 | 107 | |
108 | 108 | // Read 5 rows from csv |
109 | 109 | $read_line = 1; |
110 | 110 | |
111 | - $file = fopen( storage_path( 'app/' . $import->file ), 'r' ); |
|
111 | + $file = fopen(storage_path('app/'.$import->file), 'r'); |
|
112 | 112 | |
113 | - while ( $csv_line = fgetcsv( $file ) ) { |
|
113 | + while ($csv_line = fgetcsv($file)) { |
|
114 | 114 | $csv_data[] = $csv_line; |
115 | 115 | |
116 | - if ( $read_line > 5 ) break; |
|
116 | + if ($read_line > 5) break; |
|
117 | 117 | |
118 | 118 | $read_line++; |
119 | 119 | } |
120 | 120 | |
121 | - fclose( $file ); |
|
121 | + fclose($file); |
|
122 | 122 | |
123 | 123 | // Get fillable columns |
124 | 124 | $db_columns = $import->db_cols; |
@@ -126,7 +126,7 @@ discard block |
||
126 | 126 | // Set post route |
127 | 127 | $route = $this->import_map_route; |
128 | 128 | |
129 | - return view( 'importexport::import.data_map', compact( 'db_columns', 'csv_data', 'id', 'route' ) ); |
|
129 | + return view('importexport::import.data_map', compact('db_columns', 'csv_data', 'id', 'route')); |
|
130 | 130 | } |
131 | 131 | |
132 | 132 | /** |
@@ -139,7 +139,7 @@ discard block |
||
139 | 139 | public function storeColumnsMap(Request $request, $id) |
140 | 140 | { |
141 | 141 | // Map import instance |
142 | - $import = ModelImport::findOrFail( $id ); |
|
142 | + $import = ModelImport::findOrFail($id); |
|
143 | 143 | |
144 | 144 | // Get fillable columns |
145 | 145 | $db_columns = $import->db_cols; |
@@ -149,25 +149,25 @@ discard block |
||
149 | 149 | 'db_column' => [ |
150 | 150 | 'required', |
151 | 151 | 'array', |
152 | - Rule::in( $db_columns ), |
|
153 | - 'size:' . count( $db_columns ), |
|
152 | + Rule::in($db_columns), |
|
153 | + 'size:'.count($db_columns), |
|
154 | 154 | ], |
155 | 155 | 'db_column.*' => 'distinct', |
156 | 156 | 'ignore_col' => 'sometimes|array', |
157 | 157 | ], |
158 | 158 | [ |
159 | - 'required' => implode(', ', $db_columns) . ' are the mandatory columns', |
|
160 | - 'in' => implode(', ', $db_columns) . ' are the mandatory columns', |
|
161 | - 'size' => implode(', ', $db_columns) . ' are the mandatory columns', |
|
159 | + 'required' => implode(', ', $db_columns).' are the mandatory columns', |
|
160 | + 'in' => implode(', ', $db_columns).' are the mandatory columns', |
|
161 | + 'size' => implode(', ', $db_columns).' are the mandatory columns', |
|
162 | 162 | 'distinct' => 'You can not select one column more than one time', |
163 | 163 | ])->validate(); |
164 | 164 | |
165 | 165 | $db_column = $request->db_column; |
166 | 166 | |
167 | 167 | // Push ignore column to db_column if exists |
168 | - if ( ! is_null( $request->ignore_col ) ) { |
|
169 | - foreach ( $request->ignore_col as $col_no ) { |
|
170 | - array_splice( $db_column, $col_no, 0, ''); |
|
168 | + if (!is_null($request->ignore_col)) { |
|
169 | + foreach ($request->ignore_col as $col_no) { |
|
170 | + array_splice($db_column, $col_no, 0, ''); |
|
171 | 171 | } |
172 | 172 | } |
173 | 173 | |
@@ -179,10 +179,10 @@ discard block |
||
179 | 179 | return $import; |
180 | 180 | } |
181 | 181 | |
182 | - public function dispatchImportJob( $job, ModelImport $import) |
|
182 | + public function dispatchImportJob($job, ModelImport $import) |
|
183 | 183 | { |
184 | 184 | // Dispatch import corn job |
185 | - $job::dispatch( $import )->onQueue( 'importing' ); |
|
185 | + $job::dispatch($import)->onQueue('importing'); |
|
186 | 186 | |
187 | 187 | } |
188 | 188 | |
@@ -195,7 +195,7 @@ discard block |
||
195 | 195 | public function removeImport($id) |
196 | 196 | { |
197 | 197 | // Remove a import from db |
198 | - return ModelImport::findOrFail( $id )->delete(); |
|
198 | + return ModelImport::findOrFail($id)->delete(); |
|
199 | 199 | } |
200 | 200 | |
201 | 201 | /** |
@@ -207,17 +207,17 @@ discard block |
||
207 | 207 | public function returnImportProgress($id) |
208 | 208 | { |
209 | 209 | // Map import instance |
210 | - $import = ModelImport::findOrFail( $id ); |
|
210 | + $import = ModelImport::findOrFail($id); |
|
211 | 211 | |
212 | 212 | $data['status'] = 200; |
213 | - $data['progress'] = round( ( $import->row_processed / $import->file_rows ) * 100 ); |
|
213 | + $data['progress'] = round(($import->row_processed / $import->file_rows) * 100); |
|
214 | 214 | |
215 | 215 | // If progress completed return successful imported rows count |
216 | - if ( $data['progress'] == 100 ) { |
|
216 | + if ($data['progress'] == 100) { |
|
217 | 217 | $data['imported'] = $import->row_imported; |
218 | 218 | } |
219 | 219 | |
220 | - return response()->json( $data ); |
|
220 | + return response()->json($data); |
|
221 | 221 | } |
222 | 222 | |
223 | 223 | /** |
@@ -227,7 +227,7 @@ discard block |
||
227 | 227 | */ |
228 | 228 | public function getImportErrors() |
229 | 229 | { |
230 | - return [ 'errors' => $this->import_errors ]; |
|
230 | + return ['errors' => $this->import_errors]; |
|
231 | 231 | } |
232 | 232 | |
233 | 233 | /** |
@@ -113,7 +113,9 @@ |
||
113 | 113 | while ( $csv_line = fgetcsv( $file ) ) { |
114 | 114 | $csv_data[] = $csv_line; |
115 | 115 | |
116 | - if ( $read_line > 5 ) break; |
|
116 | + if ( $read_line > 5 ) { |
|
117 | + break; |
|
118 | + } |
|
117 | 119 | |
118 | 120 | $read_line++; |
119 | 121 | } |