|
4 | 4 |
|
5 | 5 | use Illuminate\Routing\Controller;
|
6 | 6 | use Illuminate\Support\Facades\Input;
|
| 7 | +use Maatwebsite\Excel\Facades\Excel; |
7 | 8 |
|
8 | 9 | class ExportImportController extends Controller {
|
9 | 10 |
|
10 | 11 | protected $failed = false;
|
11 | 12 |
|
12 | 13 | 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"); |
26 | 19 | }
|
27 | 20 |
|
28 | 21 | public function import($entity) {
|
29 | 22 |
|
30 | 23 | $appHelper = new libs\AppHelper();
|
31 | 24 |
|
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); |
63 | 57 | }
|
64 | 58 |
|
65 | 59 | public function importDataToDB($reader, $model, $columns, $key, $status, $notNullColumnNames) {
|
|
0 commit comments