Newer
Older
mattpiwik
a validé
<?php
/**
* Piwik - Open source web analytics
*
* @link http://piwik.org
mattpiwik
a validé
*
mattpiwik
a validé
*/
/**
* 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:
mattpiwik
a validé
* $render = new Piwik_DataTable_Renderer_Xml();
* $render->setTable($dataTable);
mattpiwik
a validé
* echo $render;
*
mattpiwik
a validé
*/
abstract class Piwik_DataTable_Renderer
{
protected $table;
mattpiwik
a validé
protected $renderSubTables = false;
mattpiwik
a validé
/**
* Whether to translate column names (i.e. metric names) or not
* @var bool
*/
public $translateColumnNames = false;
/**
* Column translations
* @var array
*/
private $columnTranslations = false;
/**
* The API method that has returned the data that should be rendered
* @var string
*/
public $apiMethod = false;
/**
* API metadata for the current report
* @var array
*/
private $apiMetaData = null;
/**
* The current idSite
* @var int
*/
public $idSite = 'all';
public function __construct()
{
}
mattpiwik
a validé
public function setRenderSubTables($enableRenderSubTable)
mattpiwik
a validé
{
mattpiwik
a validé
$this->renderSubTables = (bool)$enableRenderSubTable;
mattpiwik
a validé
}
public function setHideIdSubDatableFromResponse($bool)
{
$this->hideIdSubDatatable = (bool)$bool;
}
mattpiwik
a validé
protected function isRenderSubtables()
{
return $this->renderSubTables;
}
/**
* Output HTTP Content-Type header
*/
protected static function renderHeader()
{
@header('Content-Type: text/html; charset=utf-8');
}
mattpiwik
a validé
/**
* 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();
mattpiwik
a validé
/**
* @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;
}
static protected $availableRenderers = array( 'xml',
'json',
'csv',
'tsv',
'html',
'php'
);
static public function getRenderers()
{
return self::$availableRenderers;
}
mattpiwik
a validé
/**
* 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;
mattpiwik
a validé
return new $className;
} catch(Exception $e) {
$availableRenderers = implode(', ', self::getRenderers());
throw new Exception(Piwik_TranslateException('General_ExceptionInvalidRendererFormat', array($name, $availableRenderers)));
mattpiwik
a validé
}
}
/**
* 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 self::formatValueXml($rawData);
}
public static function formatValueXml($value)
{
if(is_string($value)
&& !is_numeric($value))
{
$value = html_entity_decode($value, ENT_COMPAT, 'UTF-8');
$value = htmlspecialchars($value, ENT_COMPAT, 'UTF-8');
$htmlentities = array( " ","¡","¢","£","¤","¥","¦","§","¨","©","ª","«","¬","­","®","¯","°","±","²","³","´","µ","¶","·","¸","¹","º","»","¼","½","¾","¿","À","Á","Â","Ã","Ä","Å","Æ","Ç","È","É","Ê","Ë","Ì","Í","Î","Ï","Ð","Ñ","Ò","Ó","Ô","Õ","Ö","×","Ø","Ù","Ú","Û","Ü","Ý","Þ","ß","à","á","â","ã","ä","å","æ","ç","è","é","ê","ë","ì","í","î","ï","ð","ñ","ò","ó","ô","õ","ö","÷","ø","ù","ú","û","ü","ý","þ","ÿ","€");
$xmlentities = array( "¢","£","¤","¥","¦","§","¨","©","ª","«","¬","­","®","¯","°","±","²","³","´","µ","¶","·","¸","¹","º","»","¼","½","¾","¿","À","Á","Â","Ã","Ä","Å","Æ","Ç","È","É","Ê","Ë","Ì","Í","Î","Ï","Ð","Ñ","Ò","Ó","Ô","Õ","Ö","×","Ø","Ù","Ú","Û","Ü","Ý","Þ","ß","à","á","â","ã","ä","å","æ","ç","è","é","ê","ë","ì","í","î","ï","ð","ñ","ò","ó","ô","õ","ö","÷","ø","ù","ú","û","ü","ý","þ","ÿ","€" );
$value = str_replace($htmlentities,$xmlentities,$value);
}
elseif($value===false)
{
$value = 0;
}
return $value;
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/**
* Translate column names to the current language.
* Used in subclasses.
*/
protected function translateColumnNames($names)
{
if (!$this->apiMethod)
{
return $names;
}
// load the translations only once
// when multiple dates are requested (date=...,...&period=day), the meta data would
// be loaded lots of times otherwise
if ($this->columnTranslations === false)
{
$meta = $this->getApiMetaData();
if ($meta === false)
{
return $names;
}
$t = Piwik_API_API::getDefaultMetricTranslations();
foreach (array('metrics', 'processedMetrics', 'metricsGoal', 'processedMetricsGoal') as $index)
{
if (isset($meta[$index]) && is_array($meta[$index]))
{
$t = array_merge($t, $meta[$index]);
}
}
$this->columnTranslations = &$t;
}
foreach ($names as &$name)
{
if (isset($this->columnTranslations[$name]))
{
$name = $this->columnTranslations[$name];
}
}
return $names;
}
protected function getApiMetaData()
{
if ($this->apiMetaData === null)
{
list($apiModule, $apiAction) = explode('.', $this->apiMethod);
if(!$apiModule || !$apiAction)
{
$this->apiMetaData = false;
}
$api = Piwik_API_API::getInstance();
$meta = $api->getMetadata($this->idSite, $apiModule, $apiAction);
if (is_array($meta[0]))
{
$meta = $meta[0];
}
$this->apiMetaData = &$meta;
}
return $this->apiMetaData;
}
protected function translateColumnName($column)
{
$columns = array($column);
$columns = $this->translateColumnNames($columns);
return $columns[0];
}
public function setTranslateColumnNames($bool)
{
$this->translateColumnNames = $bool;
}
public function setApiMethod($method)
{
$this->apiMethod = $method;
}
public function setIdSite($idSite)
{
$this->idSite = $idSite;
}