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 $button_import = true; |
13
|
|
|
|
14
|
|
|
public function getImportData() |
15
|
|
|
{ |
16
|
|
|
$this->genericLoader(); |
|
|
|
|
17
|
|
|
|
18
|
|
|
$data = []; |
19
|
|
|
$data['page_menu'] = Route::getCurrentRoute()->getActionName(); |
20
|
|
|
$data['page_title'] = 'Import Data '.CB::getCurrentModule()->name; |
21
|
|
|
|
22
|
|
|
if (! request('file') || request('import')) { |
|
|
|
|
23
|
|
|
return $this->cbView('crudbooster::import', $data); |
|
|
|
|
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
$file = 'storage'.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.base64_decode(request('file')); |
27
|
|
|
$rows = Excel::load($file, function ($reader) { |
|
|
|
|
28
|
|
|
})->get(); |
29
|
|
|
|
30
|
|
|
session()->put('total_data_import', count($rows)); |
|
|
|
|
31
|
|
|
|
32
|
|
|
$data_import_column = []; |
33
|
|
|
foreach ($rows as $value) { |
34
|
|
|
$a = []; |
35
|
|
|
foreach ($value as $k => $v) { |
36
|
|
|
$a[] = $k; |
37
|
|
|
} |
38
|
|
|
if (count($a)) { |
39
|
|
|
$data_import_column = $a; |
40
|
|
|
} |
41
|
|
|
break; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$data['table_columns'] = DB::getSchemaBuilder()->getColumnListing($this->table); |
45
|
|
|
$data['data_import_column'] = $data_import_column; |
46
|
|
|
|
47
|
|
|
return $this->cbView('crudbooster::import', $data); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function postDoImportChunk() |
51
|
|
|
{ |
52
|
|
|
$import = app(IndexImport::class); |
|
|
|
|
53
|
|
|
$this->genericLoader(); |
54
|
|
|
$fileMD5 = md5(request('file')); |
|
|
|
|
55
|
|
|
|
56
|
|
|
if (request('file') && request('resume') == 1) { |
57
|
|
|
return $import->handleImportProgress($fileMD5); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$import->InsertToDB($fileMD5, $this->table, $this->title_field); |
61
|
|
|
|
62
|
|
|
return response()->json(['status' => true]); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
} |