26
26
* @license http://centurion-project.org/license/new-bsd New BSD License
27
27
* @author Florent Messa <[email protected] >
28
28
*/
29
- class Centurion_Controller_Action_Helper_Csv extends Centurion_Controller_Action_Helper_Extract
29
+ class Centurion_Controller_Action_Helper_Csv extends Zend_Controller_Action_Helper_Abstract
30
30
{
31
- const DEFAULT_FILENAME = 'export .csv ' ;
31
+ const DEFAULT_FILENAME = 'export_columns .csv ' ;
32
32
const DEFAULT_DELIMITER = '; ' ;
33
33
const DEFAULT_ENCODING = 'UTF-16LE ' ;
34
34
@@ -43,39 +43,31 @@ class Centurion_Controller_Action_Helper_Csv extends Centurion_Controller_Action
43
43
'encoding ' => self ::DEFAULT_ENCODING
44
44
);
45
45
46
-
47
46
/**
48
47
* Convert a rowset to a CSV view.
49
48
*
50
49
* @param Centurion_Db_Table_Rowset_Abstract|array $rowset
51
50
* @param array $columns Header of the file
52
51
* @param string $options Override default options
53
- * @param boolean $noSend : To return the file name rather than sending it to the client
54
52
*/
55
- public function direct ($ rowset , array $ columns , $ options = array (), $ header = null , $ sendToBrowser = true )
53
+ public function direct ($ rowset , array $ columns , $ options = array (), $ header = null )
56
54
{
57
55
if (!$ rowset instanceof Centurion_Db_Table_Rowset_Abstract && !is_array ($ rowset )) {
58
56
throw new Centurion_Exception ('First parameter must be a Centurion valid rowset or an array ' );
59
57
}
60
58
61
59
$ options = array_merge (self ::$ options , $ options );
62
- $ handler = null ;
63
- $ tmpName = null ;
64
-
65
- //To create a temp file
66
- $ tmpName = tempnam (sys_get_temp_dir (), 'csv-export_ ' . md5 (rand ()) . '.csv ' );
67
- $ handler = fopen ($ tmpName , 'w ' );
68
-
60
+ $ handler = tmpfile ();
69
61
if (null !== $ header ) {
70
62
fputcsv ($ handler , $ this ->_convertFields ($ header , $ options ['encoding ' ]), $ options ['delimiter ' ]);
71
63
}
72
64
73
65
$ keys = array_keys ($ columns );
74
66
fputcsv ($ handler , $ this ->_convertFields (array_values ($ columns ), $ options ['encoding ' ]), $ options ['delimiter ' ]);
75
- foreach ($ rowset as $ row ) {
67
+ foreach ($ rowset as $ key => $ row ) {
76
68
$ fields = array ();
77
69
if ($ row instanceof Centurion_Db_Table_Row_Abstract) {
78
- foreach ($ keys as $ value ) {
70
+ foreach ($ keys as $ key => $ value ) {
79
71
$ fields [] = $ row ->{$ value };
80
72
}
81
73
} else {
@@ -87,11 +79,6 @@ public function direct($rowset, array $columns, $options = array(), $header = nu
87
79
fputcsv ($ handler , $ fields , $ options ['delimiter ' ]);
88
80
}
89
81
90
- if (!$ sendToBrowser ){
91
- fclose ($ handler );
92
- return $ tmpName ;
93
- }
94
-
95
82
$ this ->getActionController ()->getHelper ('layout ' )->disableLayout ();
96
83
$ this ->getActionController ()->getHelper ('viewRenderer ' )->setNoRender (true );
97
84
@@ -100,14 +87,28 @@ public function direct($rowset, array $columns, $options = array(), $header = nu
100
87
fseek ($ handler , 0 );
101
88
102
89
$ this ->getResponse ()->setHeader ('Content-disposition ' , sprintf ('attachment; filename=%s ' , $ options ['filename ' ]), true )
103
- ->setHeader ('Content-Type ' , sprintf ('application/force-download; charset=%s ' , $ options ['encoding ' ]), true )
104
- ->setHeader ('Content-Transfer-Encoding ' , 'application/octet-stream\n ' , true )
105
- ->setHeader ('Content-Length ' , $ size, true )
106
- ->setHeader ('Pragma ' , 'no-cache ' , true )
107
- ->setHeader ('Cache-Control ' , 'must-revalidate, post-check=0, pre-check=0, public ' , true )
108
- ->setHeader ('Expires ' , '0 ' , true )
90
+ ->setHeader ('Content-Type ' , sprintf ('application/force-download; charset=%s ' , $ options ['encoding ' ]))
91
+ ->setHeader ('Content-Transfer-Encoding ' , 'application/octet-stream\n ' )
92
+ ->setHeader ('Content-Length ' , $ size )
93
+ ->setHeader ('Pragma ' , 'no-cache ' )
94
+ ->setHeader ('Cache-Control ' , 'must-revalidate, post-check=0, pre-check=0, public ' )
95
+ ->setHeader ('Expires ' , '0 ' )
109
96
->sendHeaders ();
110
97
fpassthru ($ handler );
111
98
fclose ($ handler );
112
99
}
113
- }
100
+
101
+ protected function _convertEncoding ($ string , $ encoding )
102
+ {
103
+ return mb_convert_encoding ($ string , $ encoding , 'UTF-8 ' );
104
+ }
105
+
106
+ protected function _convertFields (array $ fields , $ encoding )
107
+ {
108
+ foreach ($ fields as $ key => &$ value ) {
109
+ $ value = $ this ->_convertEncoding ($ value , $ encoding );
110
+ }
111
+
112
+ return $ fields ;
113
+ }
114
+ }
0 commit comments