Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Csv.php « Renderer « DataTable « modules - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: be35d4a270eb77c8c6cbdb7cfa77ae4e07fb2b31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
/**
 * Piwik - Open source web analytics
 * 
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html Gpl v3 or later
 * @version $Id$
 * 
 * @package Piwik_DataTable
 */

require_once "DataTable/Renderer/Php.php";
/**
 * 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.
 * 
 * @package Piwik_DataTable
 * @subpackage Piwik_DataTable_Renderer
 * 
 */

class Piwik_DataTable_Renderer_Csv extends Piwik_DataTable_Renderer
{
	public $separator = ',';
	public $exportDetail = true;
	public $exportIdSubtable = true;

	function __construct($table = null)
	{
		parent::__construct($table);
	}
	
	function render()
	{
		return $this->renderTable($this->table);
	}
	
	protected function renderTable($table)
	{
		$csv = array();		

		// keep track of all the existing columns in the csv file
		$allColumns = array();
		
		foreach($table->getRows() as $row)
		{
			$csvRow = array();
			
			// COLUMNS
			$columns = $row->getColumns();
			foreach($columns as $name => $value)
			{
				if(!isset($allColumns[$name]))
				{
					$allColumns[$name] = true;
				}
				$csvRow[$name] = $value;
			}
			
			if($this->exportDetail)
			{
				// DETAILS
				$details = $row->getDetails();
				foreach($details as $name => $value)
				{
					//if a detail and a column have the same name make sure they dont overwrite
					$name = 'detail_'.$name;
					
					$allColumns[$name] = true;
					$csvRow[$name] = $value;
				}
			}		
			
			if($this->exportIdSubtable)
			{
				// ID SUB DATATABLE
				$idsubdatatable = $row->getIdSubDataTable();
				if($idsubdatatable !== false)
				{
					$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] = '';
				}
			}
		}
//		var_dump($csv);exit;
		$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);
//			foreach($keys as &$key)
//			{
//				$key = '"' . $key . '"';
//			}
			$str .= implode($this->separator, $keys);
		}
		
		// we render the CSV
		foreach($csv as $theRow)
		{
			$rowStr = "\n";
			foreach($allColumns as $columnName => $true)
			{
				$rowStr .= $theRow[$columnName] . $this->separator;
			}
			// remove the last separator
			$rowStr = substr_replace($rowStr,"",-strlen($this->separator));
			
			$str .= $rowStr;
		}
		
		// silent fail otherwise unit tests fail
		@header("Content-type: application/vnd.ms-excel");
		@header("Content-Disposition: attachment; filename=piwik-report-export.csv");			
		return $str;
	}
}