diff options
Diffstat (limited to 'core/DataTable/Renderer/Csv.php')
-rw-r--r-- | core/DataTable/Renderer/Csv.php | 164 |
1 files changed, 98 insertions, 66 deletions
diff --git a/core/DataTable/Renderer/Csv.php b/core/DataTable/Renderer/Csv.php index f75ef8d215..6f37a660bd 100644 --- a/core/DataTable/Renderer/Csv.php +++ b/core/DataTable/Renderer/Csv.php @@ -84,11 +84,7 @@ class Csv extends Renderer $this->renderHeader(); - if ($this->convertToUnicode - && function_exists('mb_convert_encoding') - ) { - $str = chr(255) . chr(254) . mb_convert_encoding($str, 'UTF-16LE', 'UTF-8'); - } + $str = $this->convertToUnicode($str); return $str; } @@ -193,42 +189,7 @@ class Csv extends Renderer } } - $csv = array(); - foreach ($table->getRows() as $row) { - $csvRow = $this->flattenColumnArray($row->getColumns()); - - if ($this->exportMetadata) { - $metadata = $row->getMetadata(); - foreach ($metadata as $name => $value) { - if ($name == 'idsubdatatable_in_db') { - continue; - } - //if a metadata and a column have the same name make sure they dont overwrite - if ($this->translateColumnNames) { - $name = Piwik::translate('General_Metadata') . ': ' . $name; - } else { - $name = 'metadata_' . $name; - } - - $csvRow[$name] = $value; - } - } - - foreach ($csvRow as $name => $value) { - $allColumns[$name] = true; - } - - if ($this->exportIdSubtable) { - $idsubdatatable = $row->getIdSubDataTable(); - if ($idsubdatatable !== false - && $this->hideIdSubDatatable === false - ) { - $csvRow['idsubdatatable'] = $idsubdatatable; - } - } - - $csv[] = $csvRow; - } + $csv = $this->makeArrayFromDataTable($table, $allColumns); // now we make sure that all the rows in the CSV array have all the columns foreach ($csv as &$row) { @@ -239,31 +200,7 @@ class Csv extends Renderer } } - $str = ''; - - // specific case, we have only one column and this column wasn't named properly (indexed by a number) - // we don't print anything in the CSV file => an empty line - if (sizeof($allColumns) == 1 - && reset($allColumns) - && !is_string(key($allColumns)) - ) { - $str .= ''; - } else { - // render row names - $str .= $this->getHeaderLine(array_keys($allColumns)) . $this->lineEnd; - } - - // we render the CSV - foreach ($csv as $theRow) { - $rowStr = ''; - foreach ($allColumns as $columnName => $true) { - $rowStr .= $this->formatValue($theRow[$columnName]) . $this->separator; - } - // remove the last separator - $rowStr = substr_replace($rowStr, "", -strlen($this->separator)); - $str .= $rowStr . $this->lineEnd; - } - $str = substr($str, 0, -strlen($this->lineEnd)); + $str = $this->buildCsvString($allColumns, $csv); return $str; } @@ -392,4 +329,99 @@ class Csv extends Renderer return $name; } } + + /** + * @param $allColumns + * @param $csv + * @return array + */ + private function buildCsvString($allColumns, $csv) + { + $str = ''; + + // specific case, we have only one column and this column wasn't named properly (indexed by a number) + // we don't print anything in the CSV file => an empty line + if (sizeof($allColumns) == 1 + && reset($allColumns) + && !is_string(key($allColumns)) + ) { + $str .= ''; + } else { + // render row names + $str .= $this->getHeaderLine(array_keys($allColumns)) . $this->lineEnd; + } + + // we render the CSV + foreach ($csv as $theRow) { + $rowStr = ''; + foreach ($allColumns as $columnName => $true) { + $rowStr .= $this->formatValue($theRow[$columnName]) . $this->separator; + } + // remove the last separator + $rowStr = substr_replace($rowStr, "", -strlen($this->separator)); + $str .= $rowStr . $this->lineEnd; + } + $str = substr($str, 0, -strlen($this->lineEnd)); + return $str; + } + + /** + * @param $table + * @param $allColumns + * @return array of csv data + */ + private function makeArrayFromDataTable($table, &$allColumns) + { + $csv = array(); + foreach ($table->getRows() as $row) { + $csvRow = $this->flattenColumnArray($row->getColumns()); + + if ($this->exportMetadata) { + $metadata = $row->getMetadata(); + foreach ($metadata as $name => $value) { + if ($name == 'idsubdatatable_in_db') { + continue; + } + //if a metadata and a column have the same name make sure they dont overwrite + if ($this->translateColumnNames) { + $name = Piwik::translate('General_Metadata') . ': ' . $name; + } else { + $name = 'metadata_' . $name; + } + + $csvRow[$name] = $value; + } + } + + foreach ($csvRow as $name => $value) { + $allColumns[$name] = true; + } + + if ($this->exportIdSubtable) { + $idsubdatatable = $row->getIdSubDataTable(); + if ($idsubdatatable !== false + && $this->hideIdSubDatatable === false + ) { + $csvRow['idsubdatatable'] = $idsubdatatable; + } + } + + $csv[] = $csvRow; + } + return $csv; + } + + /** + * @param $str + * @return string + */ + private function convertToUnicode($str) + { + if ($this->convertToUnicode + && function_exists('mb_convert_encoding') + ) { + $str = chr(255) . chr(254) . mb_convert_encoding($str, 'UTF-16LE', 'UTF-8'); + } + return $str; + } } |