diff --git a/src/controllers/ExportImportController.php b/src/controllers/ExportImportController.php index d42510f..2977584 100755 --- a/src/controllers/ExportImportController.php +++ b/src/controllers/ExportImportController.php @@ -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) { diff --git a/src/database/migrations/2014_12_02_152920_create_password_reminders_table.php b/src/database/migrations/2014_12_02_152920_create_password_reminders_table.php index dfbcf83..fe98e4f 100644 --- a/src/database/migrations/2014_12_02_152920_create_password_reminders_table.php +++ b/src/database/migrations/2014_12_02_152920_create_password_reminders_table.php @@ -17,6 +17,7 @@ public function up() $table->string('email')->index(); $table->string('token')->index(); $table->timestamp('created_at'); + $table->engine = 'InnoDB'; }); } diff --git a/src/database/migrations/2016_02_10_181651_create_roles_tables.php b/src/database/migrations/2016_02_10_181651_create_roles_tables.php index fb4c5dc..f74f418 100644 --- a/src/database/migrations/2016_02_10_181651_create_roles_tables.php +++ b/src/database/migrations/2016_02_10_181651_create_roles_tables.php @@ -17,6 +17,7 @@ public function up() $table->string('name'); $table->string('label')->nullable(); $table->timestamps(); + $table->engine = 'InnoDB'; }); Schema::create('permissions', function (Blueprint $table) { @@ -24,11 +25,13 @@ public function up() $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') @@ -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') diff --git a/src/models/EntityExport.php b/src/models/EntityExport.php new file mode 100644 index 0000000..f0166ba --- /dev/null +++ b/src/models/EntityExport.php @@ -0,0 +1,26 @@ +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; + } +} \ No newline at end of file