Newer
Older
mattpiwik
a validé
<?php
/**
* Piwik - Open source web analytics
*
* @link http://piwik.org
mattpiwik
a validé
*
mattpiwik
a validé
*/
/**
* CSV export
*
* When rendered using the default settings, a CSV report has the following characteristics:
* The first record contains headers for all the columns in the report.
* All rows have the same number of columns.
* The default field delimiter string is a comma (,).
* Formatting and layout are ignored.
*
* Note that CSV output doesn't handle recursive dataTable. It will output only the first parent level of the tables.
*
mattpiwik
a validé
*/
class Piwik_DataTable_Renderer_Csv extends Piwik_DataTable_Renderer
{
/**
* Column separator
*
* @var string
*/
public $separator = ",";
mattpiwik
a validé
/**
* Line end
*
* @var string
*/
public $lineEnd = "\n";
/**
* 'metadata' columns will be exported, prefixed by 'metadata_'
*
* @var bool
*/
public $exportMetadata = true;
/**
* Converts the content to unicode so that UTF8 characters (eg. chinese) can be imported in Excel
*
* @var bool
*/
public $convertToUnicode = true;
/**
* idSubtable will be exported in a column called 'idsubdatatable'
*
* @var bool
*/
public $exportIdSubtable = true;
public function render()
{
return $this->output($this->renderTable($this->table));
}
function renderException()
{
$exceptionMessage = self::renderHtmlEntities($this->exception->getMessage());
return 'Error: '.$exceptionMessage;
}
public function setConvertToUnicode($bool)
public function setSeparator($separator)
mattpiwik
a validé
{
$this->separator = $separator;
mattpiwik
a validé
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
}
protected function renderTable($table)
{
if($table instanceof Piwik_DataTable_Array)
{
$str = $header = '';
$prefixColumns = $table->getKeyName() . $this->separator;
foreach($table->getArray() as $currentLinePrefix => $dataTable)
{
$returned = explode("\n",$this->renderTable($dataTable));
// get the columns names
if(empty($header))
{
$header = $returned[0];
}
$returned = array_slice($returned,1);
// case empty datatable we dont print anything in the CSV export
// when in xml we would output <result date="2008-01-15" />
if(!empty($returned))
{
foreach($returned as &$row)
{
$row = $currentLinePrefix . $this->separator . $row;
}
$str .= "\n" . implode("\n", $returned);
}
}
if(!empty($header))
{
$str = $prefixColumns . $header . $str;
}
}
else
{
$str = $this->renderDataTable($table);
}
return $str;
mattpiwik
a validé
}
protected function renderDataTable( $table )
{
if($table instanceof Piwik_DataTable_Simple)
mattpiwik
a validé
{
$row = $table->getFirstRow();
if($row !== false)
{
$columnNameToValue = $row->getColumns();
if(count($columnNameToValue) == 1)
{
$value = array_values($columnNameToValue);
$str = 'value' . $this->lineEnd . $this->formatValue($value[0]);
return $str;
}
}
mattpiwik
a validé
}
$csv = $allColumns = array();
mattpiwik
a validé
foreach($table->getRows() as $row)
{
$csvRow = array();
$columns = $row->getColumns();
foreach($columns as $name => $value)
{
//goals => array( 'idgoal=1' =>array(..), 'idgoal=2' => array(..))
if(is_array($value))
{
foreach($value as $key => $subValues)
{
if(is_array($subValues))
{
foreach($subValues as $subKey => $subValue)
{
// goals_idgoal=1
$columnName = $name . "_" . $key . "_" . $subKey;
$allColumns[$columnName] = true;
$csvRow[$columnName] = $subValue;
}
}
}
}
else
mattpiwik
a validé
{
$allColumns[$name] = true;
$csvRow[$name] = $value;
mattpiwik
a validé
}
}
mattpiwik
a validé
if($this->exportMetadata)
{
$metadata = $row->getMetadata();
foreach($metadata as $name => $value)
{
//if a metadata and a column have the same name make sure they dont overwrite
$name = 'metadata_'.$name;
$allColumns[$name] = true;
$csvRow[$name] = $value;
}
}
if($this->exportIdSubtable)
{
$idsubdatatable = $row->getIdSubDataTable();
if($idsubdatatable !== false
&& $this->hideIdSubDatatable === false)
mattpiwik
a validé
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
{
$csvRow['idsubdatatable'] = $idsubdatatable;
}
}
$csv[] = $csvRow;
}
// now we make sure that all the rows in the CSV array have all the columns
foreach($csv as &$row)
{
foreach($allColumns as $columnName => $true)
{
if(!isset($row[$columnName]))
{
$row[$columnName] = '';
}
}
}
$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
{
$keys = array_keys($allColumns);
$str .= implode($this->separator, $keys);
$str .= $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;
}
protected function formatValue($value)
{
if(is_string($value)
&& !is_numeric($value))
{
$value = html_entity_decode($value, ENT_COMPAT, 'UTF-8');
}
elseif($value === false)
{
$value = 0;
}
if(strpos($value, '"') !== false
|| strpos($value, $this->separator) !== false )
{
$value = '"'. str_replace('"', '""', $value). '"';
}
mattpiwik
a validé
return $value;
}
protected function output( $str )
{
if(empty($str))
{
return 'No data available';
}
// silent fail otherwise unit tests fail
@header('Content-Type: application/vnd.ms-excel');
@header('Content-Disposition: attachment; filename=piwik-report-export.csv');
Piwik::overrideCacheControlHeaders();
mattpiwik
a validé
if($this->convertToUnicode
&& function_exists('mb_convert_encoding'))
{
$str = chr(255) . chr(254) . mb_convert_encoding($str, 'UTF-16LE', 'UTF-8');
}
return $str;
}