Skip to content

Commit b95b38c

Browse files
committed
Merge pull request #98 from FreakDev/master
import fixes from other octave's projects
2 parents 6acc095 + 3012336 commit b95b38c

File tree

2 files changed

+29
-26
lines changed
  • library/Centurion

2 files changed

+29
-26
lines changed

library/Centurion/Contrib/translation/configs/module.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
[production]
22

3+
resources.view.helperPath.Translation_View_Helper_ = APPLICATION_PATH "/../library/Centurion/Contrib/translation/views/helpers"
4+
35
[pre-prod : production]
46

57
[development : production]

library/Centurion/Controller/Action/Helper/Csv.php

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
* @license http://centurion-project.org/license/new-bsd New BSD License
2727
* @author Florent Messa <[email protected]>
2828
*/
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
3030
{
31-
const DEFAULT_FILENAME = 'export.csv';
31+
const DEFAULT_FILENAME = 'export_columns.csv';
3232
const DEFAULT_DELIMITER = ';';
3333
const DEFAULT_ENCODING = 'UTF-16LE';
3434

@@ -43,39 +43,31 @@ class Centurion_Controller_Action_Helper_Csv extends Centurion_Controller_Action
4343
'encoding' => self::DEFAULT_ENCODING
4444
);
4545

46-
4746
/**
4847
* Convert a rowset to a CSV view.
4948
*
5049
* @param Centurion_Db_Table_Rowset_Abstract|array $rowset
5150
* @param array $columns Header of the file
5251
* @param string $options Override default options
53-
* @param boolean $noSend : To return the file name rather than sending it to the client
5452
*/
55-
public function direct($rowset, array $columns, $options = array(), $header = null, $sendToBrowser = true)
53+
public function direct($rowset, array $columns, $options = array(), $header = null)
5654
{
5755
if (!$rowset instanceof Centurion_Db_Table_Rowset_Abstract && !is_array($rowset)) {
5856
throw new Centurion_Exception('First parameter must be a Centurion valid rowset or an array');
5957
}
6058

6159
$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();
6961
if (null !== $header) {
7062
fputcsv($handler, $this->_convertFields($header, $options['encoding']), $options['delimiter']);
7163
}
7264

7365
$keys = array_keys($columns);
7466
fputcsv($handler, $this->_convertFields(array_values($columns), $options['encoding']), $options['delimiter']);
75-
foreach ($rowset as $row) {
67+
foreach ($rowset as $key => $row) {
7668
$fields = array();
7769
if ($row instanceof Centurion_Db_Table_Row_Abstract) {
78-
foreach ($keys as $value) {
70+
foreach ($keys as $key => $value) {
7971
$fields[] = $row->{$value};
8072
}
8173
} else {
@@ -87,11 +79,6 @@ public function direct($rowset, array $columns, $options = array(), $header = nu
8779
fputcsv($handler, $fields, $options['delimiter']);
8880
}
8981

90-
if (!$sendToBrowser){
91-
fclose($handler);
92-
return $tmpName;
93-
}
94-
9582
$this->getActionController()->getHelper('layout')->disableLayout();
9683
$this->getActionController()->getHelper('viewRenderer')->setNoRender(true);
9784

@@ -100,14 +87,28 @@ public function direct($rowset, array $columns, $options = array(), $header = nu
10087
fseek($handler, 0);
10188

10289
$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')
10996
->sendHeaders();
11097
fpassthru($handler);
11198
fclose($handler);
11299
}
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

Comments
 (0)