|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Crocodicstudio\Crudbooster\controllers\CBController; |
|
4
|
|
|
|
|
5
|
|
|
use Crocodicstudio\Crudbooster\controllers\Helpers\IndexImport; |
|
6
|
|
|
use Illuminate\Support\Facades\DB; |
|
7
|
|
|
use Maatwebsite\Excel\Facades\Excel; |
|
8
|
|
|
use Illuminate\Support\Facades\Route; |
|
9
|
|
|
|
|
10
|
|
|
trait ImportData |
|
11
|
|
|
{ |
|
12
|
|
|
public function getImportData() |
|
13
|
|
|
{ |
|
14
|
|
|
$this->genericLoader(); |
|
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
$data = []; |
|
17
|
|
|
$data['page_menu'] = Route::getCurrentRoute()->getActionName(); |
|
18
|
|
|
$data['page_title'] = 'Import Data '.CRUDBooster::getCurrentModule()->name; |
|
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
if (! request('file') || request('import')) { |
|
21
|
|
|
return $this->cbView('crudbooster::import', $data); |
|
|
|
|
|
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
$file = 'storage'.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.base64_decode(request('file')); |
|
|
|
|
|
|
25
|
|
|
$rows = Excel::load($file, function ($reader) { |
|
|
|
|
|
|
26
|
|
|
})->get(); |
|
27
|
|
|
|
|
28
|
|
|
session()->put('total_data_import', count($rows)); |
|
29
|
|
|
|
|
30
|
|
|
$import_column = []; |
|
31
|
|
|
foreach ($rows as $value) { |
|
32
|
|
|
$a = []; |
|
33
|
|
|
foreach ($value as $k => $v) { |
|
34
|
|
|
$a[] = $k; |
|
35
|
|
|
} |
|
36
|
|
|
if (count($a)) { |
|
37
|
|
|
$import_column = $a; |
|
38
|
|
|
} |
|
39
|
|
|
break; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
$data['table_columns'] = DB::getSchemaBuilder()->getColumnListing($this->table); |
|
43
|
|
|
$data['data_import_column'] = $import_column; |
|
44
|
|
|
|
|
45
|
|
|
return $this->cbView('crudbooster::import', $data); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function postDoImportChunk() |
|
49
|
|
|
{ |
|
50
|
|
|
$import = app(IndexImport::class); |
|
51
|
|
|
$this->genericLoader(); |
|
52
|
|
|
$fileMD5 = md5(request('file')); |
|
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
if (request('file') && request('resume') == 1) { |
|
55
|
|
|
return $import->handleImportProgress($fileMD5); |
|
|
|
|
|
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$import->InsertToDB($fileMD5, $this->table, $this->titleField); |
|
59
|
|
|
|
|
60
|
|
|
return response()->json(['status' => true]); |
|
|
|
|
|
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
} |