From 832d0107f413847f2564c3598f4b863653231fe3 Mon Sep 17 00:00:00 2001 From: Reza Date: Sun, 1 Mar 2020 14:26:47 +0330 Subject: [PATCH] Fix previous commit (add EntityImport class and changes for import method) --- src/controllers/ExportImportController.php | 26 +---- src/models/EntityExport.php | 13 ++- src/models/EntityImport.php | 105 +++++++++++++++++++++ 3 files changed, 121 insertions(+), 23 deletions(-) create mode 100644 src/models/EntityImport.php diff --git a/src/controllers/ExportImportController.php b/src/controllers/ExportImportController.php index 2977584..ab205b4 100755 --- a/src/controllers/ExportImportController.php +++ b/src/controllers/ExportImportController.php @@ -20,23 +20,6 @@ public function export($entity, $fileType) { public function import($entity) { - $appHelper = new libs\AppHelper(); - - $className = $appHelper->getModel($entity); - $model = new $className; - $tablePrefix = \DB::getTablePrefix(); - $table = $tablePrefix.$model->getTable(); - $columns = \Schema::getColumnListing($table); - $key = $model->getKeyName(); - - $notNullColumnNames = array(); - $notNullColumnsList = \DB::select(\DB::raw("SHOW COLUMNS FROM `" . $table . "` where `Null` = 'no'")); - if (!empty($notNullColumnsList)) { - foreach ($notNullColumnsList as $notNullColumn) { - $notNullColumnNames[] = $notNullColumn->Field; - } - } - $status = Input::get('status'); $filePath = null; @@ -44,11 +27,10 @@ public function import($entity) { $filePath = Input::file('import_file')->getRealPath(); } - if ($filePath) { - - \Excel::load($filePath, function($reader) use ($model, $columns, $key, $status, $notNullColumnNames) { - $this->importDataToDB($reader, $model, $columns, $key, $status, $notNullColumnNames); - }); + if ($filePath) + { + $import = new EntityImport($entity, $status); + Excel::import($import, $filePath); } $importMessage = ($this->failed == true) ? \Lang::get('panel::fields.importDataFailure') : \Lang::get('panel::fields.importDataSuccess'); diff --git a/src/models/EntityExport.php b/src/models/EntityExport.php index f0166ba..8dbf4ef 100644 --- a/src/models/EntityExport.php +++ b/src/models/EntityExport.php @@ -5,8 +5,9 @@ use Maatwebsite\Excel\Concerns\FromArray; use Serverfireteam\Panel\libs\AppHelper; +use Maatwebsite\Excel\Concerns\WithHeadings; -class EntityExport implements FromArray +class EntityExport implements FromArray, WithHeadings { protected $entity; @@ -23,4 +24,14 @@ public function array(): array $data= json_decode( json_encode($data), true); return $data; } + public function headings(): array + { + $appHelper = new libs\AppHelper(); + $className = $appHelper->getModel($this->entity); + $model = new $className; + $tablePrefix = \DB::getTablePrefix(); + $table = $model->getTable(); + $columns = \Schema::getColumnListing($table); + return (array)$columns; + } } \ No newline at end of file diff --git a/src/models/EntityImport.php b/src/models/EntityImport.php new file mode 100644 index 0000000..9b24f35 --- /dev/null +++ b/src/models/EntityImport.php @@ -0,0 +1,105 @@ +entity = $entity; + $appHelper = new libs\AppHelper(); + + $className = $appHelper->getModel($entity); + $model = new $className; + $tablePrefix = \DB::getTablePrefix(); + $table = $model->getTable(); + $columns = \Schema::getColumnListing($table); + $key = $model->getKeyName(); + + $notNullColumnNames = array(); + $notNullColumnsList = \DB::select(\DB::raw("SHOW COLUMNS FROM `" . $tablePrefix.$table . "` where `Null` = 'no'")); + if (!empty($notNullColumnsList)) { + foreach ($notNullColumnsList as $notNullColumn) { + $notNullColumnNames[] = $notNullColumn->Field; + } + } + $this->notNullColumnNames = $notNullColumnNames; + $this->model = $model; + $this->columns = $columns; + $this->key = $key; + $this->status = $status; + + if ($this->status == 1) { + $this->model->truncate(); + } + } + + /** + * @param array $row + * + * @return User|null + */ + public function onRow(Row $row) + { + $rowIndex = $row->getIndex(); + $row = $row->toArray(); + + $newData = array(); + $updatedData = array(); + + foreach ($this->notNullColumnNames as $notNullColumn) { + if (!isset($row[$notNullColumn])) { + unset($row); + } + } + + if (!empty($row[$this->key])) { + $exists = $this->model->where($this->key, '=', $row[$this->key])->count(); + if (!$exists) { + $values = array(); + foreach ($this->columns as $col) { + if ($col != $this->key && array_key_exists($col, $row)) { + $values[$col] = $row[$col]; + } + } + $newData[] = $values; + } else if ($this->status == 2 && $exists) { + $values = array(); + foreach ($this->columns as $col) { + if (array_key_exists($col, $row)) + $values[$col] = $row[$col]; + } + $updatedData[] = $values; + } + } + + // insert data into table + if (!empty($newData)) { + $this->model->insert($newData); + } + + // update available data + if (!empty($updatedData)) { + foreach ($updatedData as $data) { + $keyValue = $data[$this->key]; + unset($data[$this->key]); + $this->model->where($this->key, $keyValue)->update($data); + } + } + } +} \ No newline at end of file