blob: 5f1a42671f969199bb5a9b6aebcd4463a655536f (
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
|
<?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$
*
* @category Piwik
* @package Piwik
*/
/**
* A DataTable Renderer can produce an output given a DataTable object.
* All new Renderers must be copied in DataTable/Renderer and added to the factory() method.
* To use a renderer, simply do:
* $render = new Piwik_DataTable_Renderer_Xml();
* $render->setTable($dataTable);
* echo $render;
*
* @package Piwik
* @subpackage Piwik_DataTable
*/
abstract class Piwik_DataTable_Renderer
{
protected $table;
protected $exception;
protected $renderSubTables = false;
public function __construct()
{
}
public function setRenderSubTables($enableRenderSubTable)
{
$this->renderSubTables = (bool)$enableRenderSubTable;
}
protected function isRenderSubtables()
{
return $this->renderSubTables;
}
/**
* Computes the dataTable output and returns the string/binary
*
* @return string
*/
abstract public function render();
/**
* Computes the exception output and returns the string/binary
*
* @return string
*/
abstract public function renderException();
/**
* @see render()
* @return string
*/
public function __toString()
{
return $this->render();
}
/**
* Set the DataTable to be rendered
* @param Piwik_DataTable|Piwik_DataTable_Simple|Piwik_DataTable_Array $table to be rendered
*/
public function setTable($table)
{
if(!($table instanceof Piwik_DataTable)
&& !($table instanceof Piwik_DataTable_Array))
{
throw new Exception("The renderer accepts only a Piwik_DataTable or an array of DataTable (Piwik_DataTable_Array) object.");
}
$this->table = $table;
}
/**
* Set the Exception to be rendered
* @param Exception $exception to be rendered
*/
public function setException($exception)
{
if(!($exception instanceof Exception))
{
throw new Exception("The exception renderer accepts only an Exception object.");
}
$this->exception = $exception;
}
/**
* Returns the DataTable associated to the output format $name
*
* @throws exception If the renderer is unknown
* @return Piwik_DataTable_Renderer
*/
static public function factory( $name )
{
$name = ucfirst(strtolower($name));
$className = 'Piwik_DataTable_Renderer_' . $name;
try {
Piwik_Loader::autoload($className);
return new $className;
} catch(Exception $e) {
$availableRenderers = 'xml, json, csv, tsv, html, php, original';
throw new Exception(Piwik_TranslateException('General_ExceptionInvalidRendererFormat', array($name, $availableRenderers)));
}
}
/**
* Returns $rawData after all applicable characters have been converted to HTML entities.
*
* @param String $rawData to be converted
* @return String
*/
static protected function renderHtmlEntities( $rawData )
{
return htmlentities($rawData, ENT_COMPAT, "UTF-8");
}
}
|