9
9
10
10
namespace phpnt \exportFile \controllers ;
11
11
12
+ use common \models \search \UserSearch ;
12
13
use Yii ;
13
14
use Dompdf \Dompdf ;
14
15
use Dompdf \Options ;
@@ -23,12 +24,12 @@ public function actionExcel()
23
24
$ searchModel = $ data ['searchModel ' ];
24
25
$ dataProvider = $ data ['dataProvider ' ];
25
26
$ title = $ data ['title ' ];
26
- $ modelName = $ data ['modelName ' ];
27
+ $ tableName = $ data ['tableName ' ];
27
28
$ fields = $ this ->getFieldsKeys ($ searchModel ->exportFields ());
28
29
29
30
$ objPHPExcel = new \PHPExcel ();
30
31
$ objPHPExcel ->setActiveSheetIndex (0 );
31
- $ objPHPExcel ->getActiveSheet ()->setTitle ($ title ? $ title : $ modelName );
32
+ $ objPHPExcel ->getActiveSheet ()->setTitle ($ title ? $ title : $ tableName );
32
33
$ letter = 65 ;
33
34
foreach ($ fields as $ one ) {
34
35
$ objPHPExcel ->getActiveSheet ()->getColumnDimension (chr ($ letter ))->setAutoSize (true );
@@ -61,7 +62,7 @@ public function actionExcel()
61
62
}
62
63
63
64
header ('Content-Type: application/vnd.ms-excel ' );
64
- $ filename = $ modelName . ' _ ' . time () .".xls " ;
65
+ $ filename = $ tableName .".xls " ;
65
66
header ('Content-Disposition: attachment;filename= ' .$ filename );
66
67
header ('Cache-Control: max-age=0 ' );
67
68
$ objWriter = \PHPExcel_IOFactory::createWriter ($ objPHPExcel , 'Excel5 ' );
@@ -73,7 +74,7 @@ public function actionCsv()
73
74
$ data = $ this ->getData ();
74
75
$ searchModel = $ data ['searchModel ' ];
75
76
$ dataProvider = $ data ['dataProvider ' ];
76
- $ modelName = $ data ['modelName ' ];
77
+ $ tableName = $ data ['tableName ' ];
77
78
$ fields = $ this ->getFieldsKeys ($ searchModel ->exportFields ());
78
79
$ csvCharset = \Yii::$ app ->request ->post ('csvCharset ' );
79
80
@@ -83,7 +84,7 @@ public function actionCsv()
83
84
header ('Cache-Control: must-revalidate, post-check=0, pre-check=0 ' );
84
85
header ('Content-Description: File Transfer ' );
85
86
header ('Content-Type: text/csv ' );
86
- $ filename = $ modelName . ' _ ' . time () .".csv " ;
87
+ $ filename = $ tableName .".csv " ;
87
88
header ('Content-Disposition: attachment;filename= ' .$ filename );
88
89
header ('Content-Transfer-Encoding: binary ' );
89
90
@@ -123,7 +124,7 @@ public function actionCsv()
123
124
header ('Cache-Control: must-revalidate, post-check=0, pre-check=0 ' );
124
125
header ('Content-Description: File Transfer ' );
125
126
header ('Content-Type: text/csv ' );
126
- $ filename = $ modelName . ' _ ' . time () .".csv " ;
127
+ $ filename = $ tableName .".csv " ;
127
128
header ('Content-Disposition: attachment;filename= ' .$ filename );
128
129
header ('Content-Transfer-Encoding: binary ' );
129
130
@@ -165,7 +166,7 @@ public function actionWord()
165
166
$ searchModel = $ data ['searchModel ' ];
166
167
$ dataProvider = $ data ['dataProvider ' ];
167
168
$ title = $ data ['title ' ];
168
- $ modelName = $ data ['modelName ' ];
169
+ $ tableName = $ data ['tableName ' ];
169
170
$ fields = $ this ->getFieldsKeys ($ searchModel ->exportFields ());
170
171
171
172
$ phpWord = new \PhpOffice \PhpWord \PhpWord ();
@@ -178,7 +179,7 @@ public function actionWord()
178
179
$ sectionStyle ->setMarginBottom (300 );
179
180
$ sectionStyle ->setMarginLeft (300 );
180
181
$ phpWord ->addTitleStyle (1 , ['name ' =>'HelveticaNeueLT Std Med ' , 'size ' =>16 ], ['align ' =>'center ' ]); //h
181
- $ section ->addTitle ('<p style="font-size: 24px; text-align: center;"> ' .$ title ? $ title : $ modelName .'</p> ' );
182
+ $ section ->addTitle ('<p style="font-size: 24px; text-align: center;"> ' .$ title ? $ title : $ tableName .'</p> ' );
182
183
183
184
$ table = $ section ->addTable (
184
185
[
@@ -224,7 +225,7 @@ public function actionWord()
224
225
}
225
226
226
227
header ('Content-Type: application/vnd.ms-word ' );
227
- $ filename = $ modelName . ' _ ' . time () .".docx " ;
228
+ $ filename = $ tableName .".docx " ;
228
229
header ('Content-Disposition: attachment;filename= ' .$ filename .' ' );
229
230
header ('Cache-Control: max-age=0 ' );
230
231
$ objWriter = \PhpOffice \PhpWord \IOFactory::createWriter ($ phpWord , 'Word2007 ' );
@@ -237,12 +238,12 @@ public function actionHtml()
237
238
$ searchModel = $ data ['searchModel ' ];
238
239
$ dataProvider = $ data ['dataProvider ' ];
239
240
$ title = $ data ['title ' ];
240
- $ modelName = $ data ['modelName ' ];
241
+ $ tableName = $ data ['tableName ' ];
241
242
$ fields = $ this ->getFieldsKeys ($ searchModel ->exportFields ());
242
243
243
244
$ phpWord = new \PhpOffice \PhpWord \PhpWord ();
244
245
$ section = $ phpWord ->addSection ();
245
- $ section ->addTitle ($ title ? $ title : $ modelName );
246
+ $ section ->addTitle ($ title ? $ title : $ tableName );
246
247
$ table = $ section ->addTable (
247
248
[
248
249
'name ' => 'Tahoma ' ,
@@ -284,7 +285,7 @@ public function actionHtml()
284
285
}
285
286
286
287
header ('Content-Type: application/html ' );
287
- $ filename = $ modelName . ' _ ' . time () .".html " ;
288
+ $ filename = $ tableName .".html " ;
288
289
header ('Content-Disposition: attachment;filename= ' .$ filename .' ' );
289
290
header ('Cache-Control: max-age=0 ' );
290
291
$ objWriter = \PhpOffice \PhpWord \IOFactory::createWriter ($ phpWord , 'HTML ' );
@@ -297,14 +298,14 @@ public function actionPdf()
297
298
$ searchModel = $ data ['searchModel ' ];
298
299
$ dataProvider = $ data ['dataProvider ' ];
299
300
$ title = $ data ['title ' ];
300
- $ modelName = $ data ['modelName ' ];
301
+ $ tableName = $ data ['tableName ' ];
301
302
$ fields = $ this ->getFieldsKeys ($ searchModel ->exportFields ());
302
303
303
304
$ options = new Options ();
304
305
$ options ->set ('defaultFont ' , 'times ' );
305
306
$ dompdf = new Dompdf ($ options );
306
307
$ html = '<html><body> ' ;
307
- $ html .= '<h1> ' .$ title ? $ title : $ modelName .'</h1> ' ;
308
+ $ html .= '<h1> ' .$ title ? $ title : $ tableName .'</h1> ' ;
308
309
$ html .= '<table width="100%" cellspacing="0" cellpadding="0"> ' ;
309
310
$ html .= '<tr style="background-color: #ececec;"> ' ;
310
311
foreach ($ fields as $ one ) {
@@ -328,15 +329,14 @@ public function actionPdf()
328
329
$ dompdf ->loadHtml ($ html );
329
330
$ dompdf ->setPaper ('A4 ' , 'landscape ' );
330
331
$ dompdf ->render ();
331
- $ dompdf ->stream ($ modelName .'_ ' .time ());
332
+ $ dompdf ->stream ($ tableName .'_ ' .time ());
332
333
}
333
334
334
335
private function getData () {
335
336
$ queryParams = Json::decode (\Yii::$ app ->request ->post ('queryParams ' ));
336
337
$ searchModel = \Yii::$ app ->request ->post ('model ' );
337
- $ array = explode ("\\" , $ searchModel );
338
- $ modelName = end ($ array );
339
338
$ searchModel = new $ searchModel ;
339
+ $ tableName = $ searchModel ->tableName ();
340
340
$ dataProvider = $ searchModel ->search ($ queryParams );
341
341
$ title = \Yii::$ app ->request ->post ('title ' );
342
342
$ getAll = \Yii::$ app ->request ->post ('getAll ' );
@@ -347,7 +347,7 @@ private function getData() {
347
347
'dataProvider ' => $ dataProvider ,
348
348
'searchModel ' => $ searchModel ,
349
349
'title ' => $ title ,
350
- 'modelName ' => $ modelName
350
+ 'tableName ' => $ tableName
351
351
];
352
352
}
353
353
0 commit comments