|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Serverfireteam\Panel; |
|
5
|
|
|
|
|
6
|
|
|
use Illuminate\Support\Facades\Hash; |
|
7
|
|
|
use Maatwebsite\Excel\Concerns\ToModel; |
|
8
|
|
|
use Serverfireteam\Panel\libs\AppHelper; |
|
9
|
|
|
use Maatwebsite\Excel\Row; |
|
10
|
|
|
use Maatwebsite\Excel\Concerns\OnEachRow; |
|
11
|
|
|
use Maatwebsite\Excel\Concerns\WithHeadingRow; |
|
12
|
|
|
|
|
13
|
|
|
class EntityImport implements OnEachRow, WithHeadingRow |
|
14
|
|
|
{ |
|
15
|
|
|
protected $entity; |
|
16
|
|
|
protected $notNullColumnNames; |
|
17
|
|
|
protected $model; |
|
18
|
|
|
protected $columns; |
|
19
|
|
|
protected $key; |
|
20
|
|
|
protected $status; |
|
21
|
|
|
|
|
22
|
|
|
public function __construct($entity, $status) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->entity = $entity; |
|
25
|
|
|
$appHelper = new libs\AppHelper(); |
|
26
|
|
|
|
|
27
|
|
|
$className = $appHelper->getModel($entity); |
|
28
|
|
|
$model = new $className; |
|
29
|
|
|
$tablePrefix = \DB::getTablePrefix(); |
|
30
|
|
|
$table = $model->getTable(); |
|
31
|
|
|
$columns = \Schema::getColumnListing($table); |
|
32
|
|
|
$key = $model->getKeyName(); |
|
33
|
|
|
|
|
34
|
|
|
$notNullColumnNames = array(); |
|
35
|
|
|
$notNullColumnsList = \DB::select(\DB::raw("SHOW COLUMNS FROM `" . $tablePrefix.$table . "` where `Null` = 'no'")); |
|
36
|
|
|
if (!empty($notNullColumnsList)) { |
|
37
|
|
|
foreach ($notNullColumnsList as $notNullColumn) { |
|
38
|
|
|
$notNullColumnNames[] = $notNullColumn->Field; |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
$this->notNullColumnNames = $notNullColumnNames; |
|
42
|
|
|
$this->model = $model; |
|
43
|
|
|
$this->columns = $columns; |
|
44
|
|
|
$this->key = $key; |
|
45
|
|
|
$this->status = $status; |
|
46
|
|
|
|
|
47
|
|
|
if ($this->status == 1) { |
|
48
|
|
|
$this->model->truncate(); |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param array $row |
|
54
|
|
|
* |
|
55
|
|
|
* @return User|null |
|
56
|
|
|
*/ |
|
57
|
|
|
public function onRow(Row $row) |
|
58
|
|
|
{ |
|
59
|
|
|
$rowIndex = $row->getIndex(); |
|
|
|
|
|
|
60
|
|
|
$row = $row->toArray(); |
|
61
|
|
|
|
|
62
|
|
|
$newData = array(); |
|
63
|
|
|
$updatedData = array(); |
|
64
|
|
|
|
|
65
|
|
|
$hasNotNULL = false; |
|
66
|
|
|
//check not NULL columns |
|
67
|
|
|
foreach ($this->notNullColumnNames as $notNullColumn) { |
|
68
|
|
|
//if there is not the column in the row |
|
69
|
|
|
if (!isset($row[$notNullColumn])) { |
|
70
|
|
|
$hasNotNULL = true; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
if (!empty($row[$this->key])) { |
|
75
|
|
|
$exists = $this->model->where($this->key, '=', $row[$this->key])->count(); |
|
76
|
|
|
if (!$exists && !$hasNotNULL) { |
|
77
|
|
|
$values = array(); |
|
78
|
|
View Code Duplication |
foreach ($this->columns as $col) { |
|
|
|
|
|
|
79
|
|
|
if ($col != $this->key && array_key_exists($col, $row)) { |
|
80
|
|
|
$values[$col] = $row[$col]; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
$newData[] = $values; |
|
84
|
|
|
} else if ($this->status == 2 && $exists) { |
|
85
|
|
|
$values = array(); |
|
86
|
|
View Code Duplication |
foreach ($this->columns as $col) { |
|
|
|
|
|
|
87
|
|
|
if (array_key_exists($col, $row)) |
|
88
|
|
|
$values[$col] = $row[$col]; |
|
89
|
|
|
} |
|
90
|
|
|
$updatedData[] = $values; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
// insert data into table |
|
95
|
|
|
if (!empty($newData)) { |
|
96
|
|
|
$this->model->insert($newData); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
// update available data |
|
100
|
|
|
if (!empty($updatedData)) { |
|
101
|
|
|
foreach ($updatedData as $data) { |
|
102
|
|
|
$keyValue = (int)$data[$this->key]; |
|
103
|
|
|
unset($data[$this->key]); |
|
104
|
|
|
try{ |
|
105
|
|
|
$this->model->where($this->key, $keyValue)->update($data); |
|
106
|
|
|
}catch (\Exception $e){} |
|
|
|
|
|
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
} |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.