1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Crocodicstudio\Crudbooster\Controllers\Helpers; |
4
|
|
|
|
5
|
|
|
use Crocodicstudio\Crudbooster\Helpers\CRUDBooster; |
6
|
|
|
use Crocodicstudio\Crudbooster\Helpers\DbInspector; |
7
|
|
|
use Crocodicstudio\Crudbooster\Modules\ModuleGenerator\ModulesRepo; |
8
|
|
|
use Illuminate\Support\Facades\Cache; |
9
|
|
|
use Illuminate\Support\Facades\DB; |
10
|
|
|
use Illuminate\Support\Facades\Storage; |
11
|
|
|
use Illuminate\Support\Facades\Validator; |
12
|
|
|
use Maatwebsite\Excel\Facades\Excel; |
13
|
|
|
|
14
|
|
|
class IndexImport |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @return mixed |
18
|
|
|
*/ |
19
|
|
|
public function doneImport() |
20
|
|
|
{ |
21
|
|
|
$data = []; |
22
|
|
|
$data['page_title'] = trans('crudbooster.import_page_title', ['module' => \CRUDBooster::getCurrentModule()->name]); |
|
|
|
|
23
|
|
|
session()->put('select_column', request('select_column')); |
24
|
|
|
|
25
|
|
|
return view('crudbooster::import', $data); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param $file_md5 |
30
|
|
|
* @return mixed |
31
|
|
|
* @throws \Exception |
32
|
|
|
*/ |
33
|
|
|
public function handleImportProgress($file_md5) |
34
|
|
|
{ |
35
|
|
|
$total = session('total_data_import') * 100; |
36
|
|
|
$prog = intval(cache('success_'.$file_md5)) / $total; |
37
|
|
|
$prog = round($prog, 2); |
38
|
|
|
|
39
|
|
|
if ($prog >= 100) { |
40
|
|
|
cache()->forget('success_'.$file_md5); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return response()->json(['progress' => $prog, 'last_error' => cache('error_'.$file_md5)]); |
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param $file |
48
|
|
|
* @return string |
49
|
|
|
*/ |
50
|
|
|
public function uploadImportData($file) |
51
|
|
|
{ |
52
|
|
|
$dir = 'uploads/'.date('Y-m'); |
53
|
|
|
$filename = md5(str_random(5)).'.'.$file->getClientOriginalExtension(); |
54
|
|
|
|
55
|
|
|
//Create Directory Monthly |
56
|
|
|
Storage::makeDirectory($dir); |
57
|
|
|
|
58
|
|
|
//Move file to storage |
59
|
|
|
Storage::putFileAs($dir, $file, $filename); |
60
|
|
|
|
61
|
|
|
return CRUDBooster::mainpath('import-data').'?file='.base64_encode($dir.'/'.$filename); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param $file |
66
|
|
|
* @return \Illuminate\Contracts\Validation\Validator |
67
|
|
|
*/ |
68
|
|
|
public function validateForImport($file) |
69
|
|
|
{ |
70
|
|
|
return Validator::make(['extension' => $file->getClientOriginalExtension(),], ['extension' => 'in:xls,xlsx,csv']); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param $file_md5 |
75
|
|
|
* @param $table |
76
|
|
|
* @param $titleField |
77
|
|
|
*/ |
78
|
|
|
public function InsertToDB($file_md5, $table, $titleField) |
79
|
|
|
{ |
80
|
|
|
$select_column = array_filter(session('select_column')); |
|
|
|
|
81
|
|
|
$table_columns = DB::getSchemaBuilder()->getColumnListing($table); |
82
|
|
|
|
83
|
|
|
$file = base64_decode(request('file')); |
|
|
|
|
84
|
|
|
$file = 'storage'.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.$file; |
85
|
|
|
$rows = Excel::load($file, function ($reader) { |
|
|
|
|
86
|
|
|
})->get(); |
87
|
|
|
|
88
|
|
|
//$data_import_column = []; |
89
|
|
|
foreach ($rows as $value) { |
90
|
|
|
$a = $this->readAndInsert($select_column, $table_columns, $value); |
91
|
|
|
|
92
|
|
|
$has_title_field = true; |
93
|
|
|
foreach ($a as $k => $v) { |
94
|
|
|
if ($k == $titleField && $v == '') { |
95
|
|
|
$has_title_field = false; |
96
|
|
|
break; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
if ($has_title_field == false) { |
|
|
|
|
101
|
|
|
continue; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
try { |
105
|
|
|
if (\Schema::hasColumn($table, 'created_at')) { |
106
|
|
|
$a['created_at'] = YmdHis(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
\DB::table($table)->insert($a); |
110
|
|
|
Cache::increment('success_'.$file_md5); |
111
|
|
|
} catch (\Exception $e) { |
112
|
|
|
$e = (string) $e; |
113
|
|
|
Cache::put('error_'.$file_md5, $e, 500); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param $selectColumn |
120
|
|
|
* @param $table_columns |
121
|
|
|
* @param $value |
122
|
|
|
* @return array |
123
|
|
|
*/ |
124
|
|
|
private function readAndInsert($selectColumn, $table_columns, $value) |
125
|
|
|
{ |
126
|
|
|
$a = []; |
127
|
|
|
foreach ($selectColumn as $sk => $s) { |
128
|
|
|
$colname = $table_columns[$sk]; |
129
|
|
|
|
130
|
|
|
if (! DbInspector::isForeignKey($colname) || intval($value->$s)) { |
131
|
|
|
$a[$colname] = $value->$s; |
132
|
|
|
continue; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
//Skip if value is empty |
136
|
|
|
if ($value->$s == '') { |
137
|
|
|
continue; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$relation_table = DbInspector::getRelatedTableName($colname); |
141
|
|
|
$relation_class = $this->resolveController($relation_table); |
142
|
|
|
$relation_class->genericLoader(); |
143
|
|
|
|
144
|
|
|
$titleField = $relation_class->titleField; |
145
|
|
|
|
146
|
|
|
$relation_insert_data = []; |
147
|
|
|
$relation_insert_data[$titleField] = $value->$s; |
148
|
|
|
|
149
|
|
|
if (\Schema::hasColumn($relation_table, 'created_at')) { |
150
|
|
|
$relation_insert_data['created_at'] = YmdHis(); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
try { |
154
|
|
|
$relation_exists = DB::table($relation_table)->where($titleField, $value->$s)->first(); |
155
|
|
|
if ($relation_exists) { |
156
|
|
|
$relation_primary_key = $relation_class->primaryKey; |
157
|
|
|
$relation_id = $relation_exists->$relation_primary_key; |
158
|
|
|
} else { |
159
|
|
|
$relation_id = DB::table($relation_table)->insertGetId($relation_insert_data); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
$a[$colname] = $relation_id; |
163
|
|
|
} catch (\Exception $e) { |
164
|
|
|
//exit($e); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return $a; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param $table |
173
|
|
|
*/ |
174
|
|
|
private function resolveController($table) |
175
|
|
|
{ |
176
|
|
|
$module = ModulesRepo::getByTableName($table); |
177
|
|
|
if (is_null($module)) { |
178
|
|
|
return ; |
179
|
|
|
} |
180
|
|
|
$relation_class = __NAMESPACE__.'\\'.$module->controller; |
181
|
|
|
if (! class_exists($relation_class)) { |
182
|
|
|
$relation_class = ctrlNamespace().'\\'.$module->controller; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
return new $relation_class; |
186
|
|
|
} |
187
|
|
|
} |
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