Skip to content

Commit 75a7192

Browse files
authored
Merge pull request #401 from RezaAb/master
Upgrade ImportExportController
2 parents 76e6da0 + b8170d6 commit 75a7192

File tree

4 files changed

+69
-44
lines changed

4 files changed

+69
-44
lines changed

src/controllers/ExportImportController.php

Lines changed: 38 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,62 +4,56 @@
44

55
use Illuminate\Routing\Controller;
66
use Illuminate\Support\Facades\Input;
7+
use Maatwebsite\Excel\Facades\Excel;
78

89
class ExportImportController extends Controller {
910

1011
protected $failed = false;
1112

1213
public function export($entity, $fileType) {
13-
14-
$appHelper = new libs\AppHelper();
15-
16-
$className = $appHelper->getModel($entity);
17-
$data = $className::get();
18-
if (strcmp($fileType, "excel") == 0) {
19-
$excel = \App::make('Excel');
20-
\Excel::create($entity, function($excel) use ($data) {
21-
$excel->sheet('Sheet1', function($sheet) use ($data) {
22-
$sheet->fromModel($data);
23-
});
24-
})->export('xls');
25-
}
14+
if (strcmp($fileType, "excel") == 0) {
15+
$export = new EntityExport($entity);
16+
return Excel::download($export, $entity.'.xlsx');
17+
}
18+
return \Redirect::to('panel/' . $entity . '/all')->with('export_message', "File type is not excel");
2619
}
2720

2821
public function import($entity) {
2922

3023
$appHelper = new libs\AppHelper();
3124

32-
$className = $appHelper->getModel($entity);
33-
$model = new $className;
34-
$table = $model->getTable();
35-
$columns = \Schema::getColumnListing($table);
36-
$key = $model->getKeyName();
37-
38-
$notNullColumnNames = array();
39-
$notNullColumnsList = \DB::select(\DB::raw("SHOW COLUMNS FROM `" . $table . "` where `Null` = 'no'"));
40-
if (!empty($notNullColumnsList)) {
41-
foreach ($notNullColumnsList as $notNullColumn) {
42-
$notNullColumnNames[] = $notNullColumn->Field;
43-
}
44-
}
45-
46-
$status = Input::get('status');
47-
48-
$filePath = null;
49-
if (Input::hasFile('import_file') && Input::file('import_file')->isValid()) {
50-
$filePath = Input::file('import_file')->getRealPath();
51-
}
52-
53-
if ($filePath) {
54-
55-
\Excel::load($filePath, function($reader) use ($model, $columns, $key, $status, $notNullColumnNames) {
56-
$this->importDataToDB($reader, $model, $columns, $key, $status, $notNullColumnNames);
57-
});
58-
}
59-
60-
$importMessage = ($this->failed == true) ? \Lang::get('panel::fields.importDataFailure') : \Lang::get('panel::fields.importDataSuccess');
61-
62-
return \Redirect::to('panel/' . $entity . '/all')->with('import_message', $importMessage);
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+
40+
$status = Input::get('status');
41+
42+
$filePath = null;
43+
if (Input::hasFile('import_file') && Input::file('import_file')->isValid()) {
44+
$filePath = Input::file('import_file')->getRealPath();
45+
}
46+
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+
});
52+
}
53+
54+
$importMessage = ($this->failed == true) ? \Lang::get('panel::fields.importDataFailure') : \Lang::get('panel::fields.importDataSuccess');
55+
56+
return \Redirect::to('panel/' . $entity . '/all')->with('import_message', $importMessage);
6357
}
6458

6559
public function importDataToDB($reader, $model, $columns, $key, $status, $notNullColumnNames) {

src/database/migrations/2014_12_02_152920_create_password_reminders_table.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public function up()
1717
$table->string('email')->index();
1818
$table->string('token')->index();
1919
$table->timestamp('created_at');
20+
$table->engine = 'InnoDB';
2021
});
2122
}
2223

src/database/migrations/2016_02_10_181651_create_roles_tables.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,21 @@ public function up()
1717
$table->string('name');
1818
$table->string('label')->nullable();
1919
$table->timestamps();
20+
$table->engine = 'InnoDB';
2021
});
2122

2223
Schema::create('permissions', function (Blueprint $table) {
2324
$table->increments('id');
2425
$table->string('name');
2526
$table->string('label')->nullable();
2627
$table->timestamps();
28+
$table->engine = 'InnoDB';
2729
});
2830

2931
Schema::create('permission_role', function (Blueprint $table) {
3032
$table->integer('permission_id')->unsigned();
3133
$table->integer('role_id')->unsigned();
34+
$table->engine = 'InnoDB';
3235

3336
$table->foreign('permission_id')
3437
->references('id')
@@ -46,6 +49,7 @@ public function up()
4649
Schema::create('admin_role', function (Blueprint $table) {
4750
$table->integer('role_id')->unsigned();
4851
$table->integer('admin_id')->unsigned();
52+
$table->engine = 'InnoDB';
4953

5054
$table->foreign('role_id')
5155
->references('id')

src/models/EntityExport.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
4+
namespace Serverfireteam\Panel;
5+
6+
use Maatwebsite\Excel\Concerns\FromArray;
7+
use Serverfireteam\Panel\libs\AppHelper;
8+
9+
class EntityExport implements FromArray
10+
{
11+
protected $entity;
12+
13+
public function __construct($entity)
14+
{
15+
$this->entity = $entity;
16+
}
17+
18+
public function array(): array
19+
{
20+
$appHelper = new libs\AppHelper();
21+
$className = $appHelper->getModel($this->entity);
22+
$data = $className::all()->ToArray();
23+
$data= json_decode( json_encode($data), true);
24+
return $data;
25+
}
26+
}

0 commit comments

Comments
 (0)