Skip to content

Commit

Permalink
Merge pull request #402 from RezaAb/master
Browse files Browse the repository at this point in the history
Some changes for previous PR
  • Loading branch information
AlirezaAlgo authored Mar 1, 2020
2 parents 75a7192 + 832d010 commit ebb95af
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 23 deletions.
26 changes: 4 additions & 22 deletions src/controllers/ExportImportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,17 @@ 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;
if (Input::hasFile('import_file') && Input::file('import_file')->isValid()) {
$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');
Expand Down
13 changes: 12 additions & 1 deletion src/models/EntityExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}
}
105 changes: 105 additions & 0 deletions src/models/EntityImport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php


namespace Serverfireteam\Panel;

use Illuminate\Support\Facades\Hash;
use Maatwebsite\Excel\Concerns\ToModel;
use Serverfireteam\Panel\libs\AppHelper;
use Maatwebsite\Excel\Row;
use Maatwebsite\Excel\Concerns\OnEachRow;
use Maatwebsite\Excel\Concerns\WithHeadingRow;

class EntityImport implements OnEachRow, WithHeadingRow
{
protected $entity;
protected $notNullColumnNames;
protected $model;
protected $columns;
protected $key;
protected $status;

public function __construct($entity, $status)
{
$this->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);
}
}
}
}

0 comments on commit ebb95af

Please sign in to comment.