Skip to content

Commit ebb95af

Browse files
authoredMar 1, 2020
Merge pull request #402 from RezaAb/master
Some changes for previous PR
2 parents 75a7192 + 832d010 commit ebb95af

File tree

3 files changed

+121
-23
lines changed

3 files changed

+121
-23
lines changed
 

‎src/controllers/ExportImportController.php

+4-22
Original file line numberDiff line numberDiff line change
@@ -20,35 +20,17 @@ public function export($entity, $fileType) {
2020

2121
public function import($entity) {
2222

23-
$appHelper = new libs\AppHelper();
24-
25-
$className = $appHelper->getModel($entity);
26-
$model = new $className;
27-
$tablePrefix = \DB::getTablePrefix();
28-
$table = $tablePrefix.$model->getTable();
29-
$columns = \Schema::getColumnListing($table);
30-
$key = $model->getKeyName();
31-
32-
$notNullColumnNames = array();
33-
$notNullColumnsList = \DB::select(\DB::raw("SHOW COLUMNS FROM `" . $table . "` where `Null` = 'no'"));
34-
if (!empty($notNullColumnsList)) {
35-
foreach ($notNullColumnsList as $notNullColumn) {
36-
$notNullColumnNames[] = $notNullColumn->Field;
37-
}
38-
}
39-
4023
$status = Input::get('status');
4124

4225
$filePath = null;
4326
if (Input::hasFile('import_file') && Input::file('import_file')->isValid()) {
4427
$filePath = Input::file('import_file')->getRealPath();
4528
}
4629

47-
if ($filePath) {
48-
49-
\Excel::load($filePath, function($reader) use ($model, $columns, $key, $status, $notNullColumnNames) {
50-
$this->importDataToDB($reader, $model, $columns, $key, $status, $notNullColumnNames);
51-
});
30+
if ($filePath)
31+
{
32+
$import = new EntityImport($entity, $status);
33+
Excel::import($import, $filePath);
5234
}
5335

5436
$importMessage = ($this->failed == true) ? \Lang::get('panel::fields.importDataFailure') : \Lang::get('panel::fields.importDataSuccess');

‎src/models/EntityExport.php

+12-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55

66
use Maatwebsite\Excel\Concerns\FromArray;
77
use Serverfireteam\Panel\libs\AppHelper;
8+
use Maatwebsite\Excel\Concerns\WithHeadings;
89

9-
class EntityExport implements FromArray
10+
class EntityExport implements FromArray, WithHeadings
1011
{
1112
protected $entity;
1213

@@ -23,4 +24,14 @@ public function array(): array
2324
$data= json_decode( json_encode($data), true);
2425
return $data;
2526
}
27+
public function headings(): array
28+
{
29+
$appHelper = new libs\AppHelper();
30+
$className = $appHelper->getModel($this->entity);
31+
$model = new $className;
32+
$tablePrefix = \DB::getTablePrefix();
33+
$table = $model->getTable();
34+
$columns = \Schema::getColumnListing($table);
35+
return (array)$columns;
36+
}
2637
}

‎src/models/EntityImport.php

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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+
foreach ($this->notNullColumnNames as $notNullColumn) {
66+
if (!isset($row[$notNullColumn])) {
67+
unset($row);
68+
}
69+
}
70+
71+
if (!empty($row[$this->key])) {
72+
$exists = $this->model->where($this->key, '=', $row[$this->key])->count();
73+
if (!$exists) {
74+
$values = array();
75+
foreach ($this->columns as $col) {
76+
if ($col != $this->key && array_key_exists($col, $row)) {
77+
$values[$col] = $row[$col];
78+
}
79+
}
80+
$newData[] = $values;
81+
} else if ($this->status == 2 && $exists) {
82+
$values = array();
83+
foreach ($this->columns as $col) {
84+
if (array_key_exists($col, $row))
85+
$values[$col] = $row[$col];
86+
}
87+
$updatedData[] = $values;
88+
}
89+
}
90+
91+
// insert data into table
92+
if (!empty($newData)) {
93+
$this->model->insert($newData);
94+
}
95+
96+
// update available data
97+
if (!empty($updatedData)) {
98+
foreach ($updatedData as $data) {
99+
$keyValue = $data[$this->key];
100+
unset($data[$this->key]);
101+
$this->model->where($this->key, $keyValue)->update($data);
102+
}
103+
}
104+
}
105+
}

0 commit comments

Comments
 (0)
Please sign in to comment.