forked from matomo-org/matomo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refs matomo-org#4701 Add CSV renderer
- Loading branch information
Showing
1 changed file
with
144 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
<?php | ||
namespace Piwik\ReportRenderer; | ||
|
||
use Piwik\ReportRenderer; | ||
use Piwik\DataTable\Renderer\Csv as CsvDataTableRenderer; | ||
use Piwik\DataTable\DataTableInterface; | ||
|
||
class Csv extends ReportRenderer | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
protected $rendered; | ||
|
||
/** | ||
* Initialize locale settings. | ||
* If not called, locale settings defaults to 'en' | ||
* | ||
* @param string $locale | ||
*/ | ||
public function setLocale($locale) | ||
{ | ||
return; | ||
} | ||
|
||
/** | ||
* Save rendering to disk | ||
* | ||
* @param string $filename without path & without format extension | ||
* @return string path of file | ||
*/ | ||
public function sendToDisk($filename) | ||
{ | ||
return ReportRenderer::writeFile( | ||
$filename, | ||
ReportRenderer::CSV_FORMAT, | ||
$this->rendered | ||
); | ||
} | ||
|
||
/** | ||
* Send rendering to browser with a 'download file' prompt | ||
* | ||
* @param string $filename without path & without format extension | ||
*/ | ||
public function sendToBrowserDownload($filename) | ||
{ | ||
ReportRenderer::sendToBrowser( | ||
$filename, | ||
ReportRenderer::CSV_FORMAT, | ||
"text/" . ReportRenderer::CSV_FORMAT, | ||
$this->rendered | ||
); | ||
} | ||
|
||
/** | ||
* Output rendering to browser | ||
* | ||
* @param string $filename without path & without format extension | ||
*/ | ||
public function sendToBrowserInline($filename) | ||
{ | ||
ReportRenderer::sendToBrowser( | ||
$filename, | ||
ReportRenderer::CSV_FORMAT, | ||
"application/" . ReportRenderer::CSV_FORMAT, | ||
$this->rendered | ||
); | ||
} | ||
|
||
/** | ||
* Get rendered report | ||
*/ | ||
public function getRenderedReport() | ||
{ | ||
return $this->rendered; | ||
} | ||
|
||
/** | ||
* Generate the first page. | ||
* | ||
* @param string $reportTitle | ||
* @param string $prettyDate formatted date | ||
* @param string $description | ||
* @param array $reportMetadata metadata for all reports | ||
* @param array $segment segment applied to all reports | ||
*/ | ||
public function renderFrontPage($reportTitle, $prettyDate, $description, $reportMetadata, $segment) | ||
{ | ||
return; | ||
} | ||
|
||
/** | ||
* Render the provided report. | ||
* Multiple calls to this method before calling outputRendering appends each report content. | ||
* | ||
* @param array $processedReport @see API::getProcessedReport() | ||
*/ | ||
public function renderReport($processedReport) | ||
{ | ||
|
||
$csvRenderer = $this->getRenderer( | ||
$processedReport['reportData'], | ||
$processedReport['metadata']['uniqueId'] | ||
); | ||
|
||
$this->rendered .= implode( | ||
'', | ||
array( | ||
$processedReport['metadata']['name'], | ||
$csvRenderer->lineEnd, | ||
$csvRenderer->render($processedReport), | ||
$csvRenderer->lineEnd, | ||
$csvRenderer->lineEnd, | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* @param DataTableInterface $table | ||
* @param string $uniqueId | ||
* @return \Piwik\DataTable\Renderer\Csv | ||
*/ | ||
protected function getRenderer(DataTableInterface $table, $uniqueId) | ||
{ | ||
$csvRenderer = new CsvDataTableRenderer(); | ||
$csvRenderer->setTable($table); | ||
$csvRenderer->setConvertToUnicode(false); | ||
$csvRenderer->setApiMethod( | ||
$this->getApiMethodNameFromUniqueId($uniqueId) | ||
); | ||
|
||
return $csvRenderer; | ||
} | ||
|
||
/** | ||
* @param $uniqueId | ||
* @return string | ||
*/ | ||
protected function getApiMethodNameFromUniqueId($uniqueId) | ||
{ | ||
return str_replace("_", ".", $uniqueId); | ||
} | ||
} |