Skip to content

Commit

Permalink
Merge pull request #401 from RezaAb/master
Browse files Browse the repository at this point in the history
Upgrade ImportExportController
  • Loading branch information
AlirezaAlgo authored Mar 1, 2020
2 parents 76e6da0 + b8170d6 commit 75a7192
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 44 deletions.
82 changes: 38 additions & 44 deletions src/controllers/ExportImportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,62 +4,56 @@

use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Input;
use Maatwebsite\Excel\Facades\Excel;

class ExportImportController extends Controller {

protected $failed = false;

public function export($entity, $fileType) {

$appHelper = new libs\AppHelper();

$className = $appHelper->getModel($entity);
$data = $className::get();
if (strcmp($fileType, "excel") == 0) {
$excel = \App::make('Excel');
\Excel::create($entity, function($excel) use ($data) {
$excel->sheet('Sheet1', function($sheet) use ($data) {
$sheet->fromModel($data);
});
})->export('xls');
}
if (strcmp($fileType, "excel") == 0) {
$export = new EntityExport($entity);
return Excel::download($export, $entity.'.xlsx');
}
return \Redirect::to('panel/' . $entity . '/all')->with('export_message', "File type is not excel");
}

public function import($entity) {

$appHelper = new libs\AppHelper();

$className = $appHelper->getModel($entity);
$model = new $className;
$table = $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);
});
}

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

return \Redirect::to('panel/' . $entity . '/all')->with('import_message', $importMessage);
$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);
});
}

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

return \Redirect::to('panel/' . $entity . '/all')->with('import_message', $importMessage);
}

public function importDataToDB($reader, $model, $columns, $key, $status, $notNullColumnNames) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public function up()
$table->string('email')->index();
$table->string('token')->index();
$table->timestamp('created_at');
$table->engine = 'InnoDB';
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,21 @@ public function up()
$table->string('name');
$table->string('label')->nullable();
$table->timestamps();
$table->engine = 'InnoDB';
});

Schema::create('permissions', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('label')->nullable();
$table->timestamps();
$table->engine = 'InnoDB';
});

Schema::create('permission_role', function (Blueprint $table) {
$table->integer('permission_id')->unsigned();
$table->integer('role_id')->unsigned();
$table->engine = 'InnoDB';

$table->foreign('permission_id')
->references('id')
Expand All @@ -46,6 +49,7 @@ public function up()
Schema::create('admin_role', function (Blueprint $table) {
$table->integer('role_id')->unsigned();
$table->integer('admin_id')->unsigned();
$table->engine = 'InnoDB';

$table->foreign('role_id')
->references('id')
Expand Down
26 changes: 26 additions & 0 deletions src/models/EntityExport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php


namespace Serverfireteam\Panel;

use Maatwebsite\Excel\Concerns\FromArray;
use Serverfireteam\Panel\libs\AppHelper;

class EntityExport implements FromArray
{
protected $entity;

public function __construct($entity)
{
$this->entity = $entity;
}

public function array(): array
{
$appHelper = new libs\AppHelper();
$className = $appHelper->getModel($this->entity);
$data = $className::all()->ToArray();
$data= json_decode( json_encode($data), true);
return $data;
}
}

0 comments on commit 75a7192

Please sign in to comment.