|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Serverfireteam\Panel; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Routing\Controller; |
|
6
|
|
|
use Illuminate\Support\Facades\Input; |
|
7
|
|
|
use Maatwebsite\Excel\Facades\Excel; |
|
8
|
|
|
|
|
9
|
|
|
class ExportImportController extends Controller { |
|
10
|
|
|
|
|
11
|
|
|
protected $failed = false; |
|
12
|
|
|
|
|
13
|
|
|
public function export($entity, $fileType) { |
|
14
|
|
|
if (strcmp($fileType, "excel") == 0) { |
|
15
|
|
|
$export = new EntityExport($entity); |
|
16
|
|
|
return Excel::download($export, $entity.'.xlsx'); |
|
17
|
|
|
} |
|
18
|
|
|
return \Redirect::to('panel/' . $entity . '/all')->with('export_message', "File type is not excel"); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
public function import($entity) { |
|
22
|
|
|
|
|
23
|
|
|
$status = Input::get('status'); |
|
24
|
|
|
|
|
25
|
|
|
$filePath = null; |
|
26
|
|
|
if (Input::hasFile('import_file') && Input::file('import_file')->isValid()) { |
|
27
|
|
|
$pathTemp = Input::file('import_file')->store('temp'); |
|
28
|
|
|
$filePath = storage_path('app').'/'.$pathTemp; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
if ($filePath) |
|
|
|
|
|
|
32
|
|
|
{ |
|
33
|
|
|
$import = new EntityImport($entity, $status); |
|
34
|
|
|
Excel::import($import, $filePath); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
$importMessage = ($this->failed == true) ? \Lang::get('panel::fields.importDataFailure') : \Lang::get('panel::fields.importDataSuccess'); |
|
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
return \Redirect::to('panel/' . $entity . '/all')->with('import_message', $importMessage); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function importDataToDB($reader, $model, $columns, $key, $status, $notNullColumnNames) { |
|
43
|
|
|
|
|
44
|
|
|
$rows = $reader->toArray(); |
|
45
|
|
|
$newData = array(); |
|
46
|
|
|
$updatedData = array(); |
|
47
|
|
|
|
|
48
|
|
|
// Check validation of values |
|
49
|
|
|
foreach ($rows as $i => $row) { |
|
50
|
|
|
foreach ($notNullColumnNames as $notNullColumn) { |
|
51
|
|
|
if (!isset($row[$notNullColumn])) { |
|
52
|
|
|
unset($rows[$i]); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
if (!$this->failed) { |
|
58
|
|
|
if ($status == 1) { |
|
59
|
|
|
$model->truncate(); |
|
60
|
|
|
} |
|
61
|
|
|
foreach ($rows as $row) { |
|
62
|
|
|
if (!empty($row[$key])) { |
|
63
|
|
|
$exists = $model->where($key, '=', $row[$key])->count(); |
|
64
|
|
|
if (!$exists) { |
|
65
|
|
|
$values = array(); |
|
66
|
|
|
foreach ($columns as $col) { |
|
67
|
|
|
if ($col != $key) { |
|
68
|
|
|
$values[$col] = $row[$col]; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
$newData[] = $values; |
|
72
|
|
|
} else if ($status == 2 && $exists) { |
|
73
|
|
|
$values = array(); |
|
74
|
|
|
foreach ($columns as $col) { |
|
75
|
|
|
$values[$col] = $row[$col]; |
|
76
|
|
|
} |
|
77
|
|
|
$updatedData[] = $values; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
// insert data into table |
|
84
|
|
|
if (!empty($newData)) { |
|
85
|
|
|
$model->insert($newData); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
// update available data |
|
89
|
|
|
if (!empty($updatedData)) { |
|
90
|
|
|
foreach ($updatedData as $data) { |
|
91
|
|
|
$keyValue = $data[$key]; |
|
92
|
|
|
unset($data[$key]); |
|
93
|
|
|
$model->where($key, $keyValue)->update($data); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: